2022-04-06 14:13:54 -04:00
|
|
|
defmodule Aggiedit.Rooms do
|
|
|
|
@moduledoc """
|
|
|
|
The Rooms context.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Ecto.Query, warn: false
|
|
|
|
alias Aggiedit.Repo
|
|
|
|
|
|
|
|
alias Aggiedit.Rooms.Room
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of rooms.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> list_rooms()
|
|
|
|
[%Room{}, ...]
|
|
|
|
|
|
|
|
"""
|
|
|
|
def list_rooms do
|
|
|
|
Repo.all(Room)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single room.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Room does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> get_room!(123)
|
|
|
|
%Room{}
|
|
|
|
|
|
|
|
iex> get_room!(456)
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
|
|
|
def get_room!(id), do: Repo.get!(Room, id)
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a room.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> create_room(%{field: value})
|
|
|
|
{:ok, %Room{}}
|
|
|
|
|
|
|
|
iex> create_room(%{field: bad_value})
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def create_room(attrs \\ %{}) do
|
|
|
|
%Room{}
|
|
|
|
|> Room.changeset(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates a room.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> update_room(room, %{field: new_value})
|
|
|
|
{:ok, %Room{}}
|
|
|
|
|
|
|
|
iex> update_room(room, %{field: bad_value})
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def update_room(%Room{} = room, attrs) do
|
|
|
|
room
|
|
|
|
|> Room.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes a room.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> delete_room(room)
|
|
|
|
{:ok, %Room{}}
|
|
|
|
|
|
|
|
iex> delete_room(room)
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def delete_room(%Room{} = room) do
|
|
|
|
Repo.delete(room)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns an `%Ecto.Changeset{}` for tracking room changes.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> change_room(room)
|
|
|
|
%Ecto.Changeset{data: %Room{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def change_room(%Room{} = room, attrs \\ %{}) do
|
|
|
|
Room.changeset(room, attrs)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_or_find_room_with_domain(domain) do
|
|
|
|
case Repo.get_by(Room, domain: domain) do
|
|
|
|
room=%Room{} -> {:ok, room}
|
|
|
|
nil -> create_room(%{domain: domain})
|
|
|
|
end
|
|
|
|
end
|
2022-04-06 14:55:12 -04:00
|
|
|
|
|
|
|
alias Aggiedit.Rooms.Post
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of posts.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> list_posts()
|
|
|
|
[%Post{}, ...]
|
|
|
|
|
|
|
|
"""
|
|
|
|
def list_posts do
|
|
|
|
Repo.all(Post)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single post.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Post does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> get_post!(123)
|
|
|
|
%Post{}
|
|
|
|
|
|
|
|
iex> get_post!(456)
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
|
|
|
def get_post!(id), do: Repo.get!(Post, id)
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a post.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> create_post(%{field: value})
|
|
|
|
{:ok, %Post{}}
|
|
|
|
|
|
|
|
iex> create_post(%{field: bad_value})
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def create_post(attrs \\ %{}) do
|
|
|
|
%Post{}
|
|
|
|
|> Post.changeset(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates a post.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> update_post(post, %{field: new_value})
|
|
|
|
{:ok, %Post{}}
|
|
|
|
|
|
|
|
iex> update_post(post, %{field: bad_value})
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def update_post(%Post{} = post, attrs) do
|
|
|
|
post
|
|
|
|
|> Post.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes a post.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> delete_post(post)
|
|
|
|
{:ok, %Post{}}
|
|
|
|
|
|
|
|
iex> delete_post(post)
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def delete_post(%Post{} = post) do
|
|
|
|
Repo.delete(post)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns an `%Ecto.Changeset{}` for tracking post changes.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> change_post(post)
|
|
|
|
%Ecto.Changeset{data: %Post{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def change_post(%Post{} = post, attrs \\ %{}) do
|
|
|
|
Post.changeset(post, attrs)
|
|
|
|
end
|
2022-04-06 14:13:54 -04:00
|
|
|
end
|