philomena/lib/philomena/galleries.ex
2019-12-04 19:11:31 -05:00

224 lines
4.1 KiB
Elixir

defmodule Philomena.Galleries do
@moduledoc """
The Galleries context.
"""
import Ecto.Query, warn: false
alias Philomena.Repo
alias Philomena.Galleries.Gallery
@doc """
Returns the list of galleries.
## Examples
iex> list_galleries()
[%Gallery{}, ...]
"""
def list_galleries do
Repo.all(Gallery)
end
@doc """
Gets a single gallery.
Raises `Ecto.NoResultsError` if the Gallery does not exist.
## Examples
iex> get_gallery!(123)
%Gallery{}
iex> get_gallery!(456)
** (Ecto.NoResultsError)
"""
def get_gallery!(id), do: Repo.get!(Gallery, id)
@doc """
Creates a gallery.
## Examples
iex> create_gallery(%{field: value})
{:ok, %Gallery{}}
iex> create_gallery(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_gallery(user, attrs \\ %{}) do
%Gallery{}
|> Gallery.creation_changeset(attrs, user)
|> Repo.insert()
end
@doc """
Updates a gallery.
## Examples
iex> update_gallery(gallery, %{field: new_value})
{:ok, %Gallery{}}
iex> update_gallery(gallery, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_gallery(%Gallery{} = gallery, attrs) do
gallery
|> Gallery.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Gallery.
## Examples
iex> delete_gallery(gallery)
{:ok, %Gallery{}}
iex> delete_gallery(gallery)
{:error, %Ecto.Changeset{}}
"""
def delete_gallery(%Gallery{} = gallery) do
Repo.delete(gallery)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking gallery changes.
## Examples
iex> change_gallery(gallery)
%Ecto.Changeset{source: %Gallery{}}
"""
def change_gallery(%Gallery{} = gallery) do
Gallery.changeset(gallery, %{})
end
def reindex_gallery(%Gallery{} = gallery) do
spawn fn ->
gallery
|> preload(^indexing_preloads())
|> where(id: ^gallery.id)
|> Repo.one()
|> Gallery.index_document()
end
gallery
end
def unindex_gallery(%Gallery{} = gallery) do
spawn fn ->
Gallery.delete_document(gallery.id)
end
gallery
end
def indexing_preloads do
[:subscribers, :creator, :interactions]
end
alias Philomena.Galleries.Subscription
@doc """
Returns the list of gallery_subscriptions.
## Examples
iex> list_gallery_subscriptions()
[%Subscription{}, ...]
"""
def list_gallery_subscriptions do
Repo.all(Subscription)
end
@doc """
Gets a single subscription.
Raises `Ecto.NoResultsError` if the Subscription does not exist.
## Examples
iex> get_subscription!(123)
%Subscription{}
iex> get_subscription!(456)
** (Ecto.NoResultsError)
"""
def get_subscription!(id), do: Repo.get!(Subscription, id)
@doc """
Creates a subscription.
## Examples
iex> create_subscription(%{field: value})
{:ok, %Subscription{}}
iex> create_subscription(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_subscription(attrs \\ %{}) do
%Subscription{}
|> Subscription.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a subscription.
## Examples
iex> update_subscription(subscription, %{field: new_value})
{:ok, %Subscription{}}
iex> update_subscription(subscription, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_subscription(%Subscription{} = subscription, attrs) do
subscription
|> Subscription.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Subscription.
## Examples
iex> delete_subscription(subscription)
{:ok, %Subscription{}}
iex> delete_subscription(subscription)
{:error, %Ecto.Changeset{}}
"""
def delete_subscription(%Subscription{} = subscription) do
Repo.delete(subscription)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking subscription changes.
## Examples
iex> change_subscription(subscription)
%Ecto.Changeset{source: %Subscription{}}
"""
def change_subscription(%Subscription{} = subscription) do
Subscription.changeset(subscription, %{})
end
end