Aggiedit/lib/aggiedit/rooms.ex
2022-04-07 15:52:32 -06:00

197 lines
3.3 KiB
Elixir

defmodule Aggiedit.Rooms do
@moduledoc """
The Rooms context.
"""
import Ecto.Query, warn: false
alias Aggiedit.Repo
alias Aggiedit.Accounts
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
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)
def create_post(attrs, user, after_save \\ &{:ok, &1}) do
user = Repo.preload(user, :room)
%Post{}
|> Repo.preload([:user, :room])
|> Post.changeset(attrs)
|> Ecto.Changeset.put_assoc(:user, user)
|> Ecto.Changeset.put_assoc(:room, user.room)
|> Repo.insert()
|> post_saved(after_save)
end
def update_post(%Post{} = post, attrs, after_save \\ &{:ok, &1}) do
post
|> Post.changeset(attrs)
|> Repo.update()
|> post_saved(after_save)
end
defp post_saved({:ok, post}, func) do
{:ok, _post} = func.(post)
end
defp post_saved(error, _fun), do: error
@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
end