context: gallery_subscriptions

This commit is contained in:
Liam P. White 2019-08-28 13:15:19 -04:00
parent e86b342fe5
commit 8473541e3e
4 changed files with 175 additions and 2 deletions

View file

@ -101,4 +101,100 @@ defmodule Philomena.Galleries do
def change_gallery(%Gallery{} = gallery) do
Gallery.changeset(gallery, %{})
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

View file

@ -2,9 +2,11 @@ defmodule Philomena.Galleries.Interaction do
use Ecto.Schema
import Ecto.Changeset
@primary_key false
schema "gallery_interactions" do
belongs_to :gallery, Philomena.Galleries.Gallery
belongs_to :image, Philomena.Images.Image
belongs_to :gallery, Philomena.Galleries.Gallery, primary_key: true
belongs_to :image, Philomena.Images.Image, primary_key: true
field :position, :integer
end

View file

@ -0,0 +1,18 @@
defmodule Philomena.Galleries.Subscription do
use Ecto.Schema
import Ecto.Changeset
@primary_key false
schema "gallery_subscriptions" do
belongs_to :gallery, Philomena.Galleries.Gallery, primary_key: true
belongs_to :user, Philomena.Users.User, primary_key: true
end
@doc false
def changeset(subscription, attrs) do
subscription
|> cast(attrs, [])
|> validate_required([])
end
end

View file

@ -59,4 +59,61 @@ defmodule Philomena.GalleriesTest do
assert %Ecto.Changeset{} = Galleries.change_gallery(gallery)
end
end
describe "gallery_subscriptions" do
alias Philomena.Galleries.Subscription
@valid_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
def subscription_fixture(attrs \\ %{}) do
{:ok, subscription} =
attrs
|> Enum.into(@valid_attrs)
|> Galleries.create_subscription()
subscription
end
test "list_gallery_subscriptions/0 returns all gallery_subscriptions" do
subscription = subscription_fixture()
assert Galleries.list_gallery_subscriptions() == [subscription]
end
test "get_subscription!/1 returns the subscription with given id" do
subscription = subscription_fixture()
assert Galleries.get_subscription!(subscription.id) == subscription
end
test "create_subscription/1 with valid data creates a subscription" do
assert {:ok, %Subscription{} = subscription} = Galleries.create_subscription(@valid_attrs)
end
test "create_subscription/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Galleries.create_subscription(@invalid_attrs)
end
test "update_subscription/2 with valid data updates the subscription" do
subscription = subscription_fixture()
assert {:ok, %Subscription{} = subscription} = Galleries.update_subscription(subscription, @update_attrs)
end
test "update_subscription/2 with invalid data returns error changeset" do
subscription = subscription_fixture()
assert {:error, %Ecto.Changeset{}} = Galleries.update_subscription(subscription, @invalid_attrs)
assert subscription == Galleries.get_subscription!(subscription.id)
end
test "delete_subscription/1 deletes the subscription" do
subscription = subscription_fixture()
assert {:ok, %Subscription{}} = Galleries.delete_subscription(subscription)
assert_raise Ecto.NoResultsError, fn -> Galleries.get_subscription!(subscription.id) end
end
test "change_subscription/1 returns a subscription changeset" do
subscription = subscription_fixture()
assert %Ecto.Changeset{} = Galleries.change_subscription(subscription)
end
end
end