context: topic_subscriptions

This commit is contained in:
Liam P. White 2019-08-28 18:24:19 -04:00
parent 0196d5b57e
commit 6920dfba25
3 changed files with 171 additions and 0 deletions

View file

@ -581,4 +581,100 @@ defmodule Philomena.Forums do
def change_poll_vote(%PollVote{} = poll_vote) do
PollVote.changeset(poll_vote, %{})
end
alias Philomena.Forums.TopicSubscription
@doc """
Returns the list of topic_subscriptions.
## Examples
iex> list_topic_subscriptions()
[%TopicSubscription{}, ...]
"""
def list_topic_subscriptions do
Repo.all(TopicSubscription)
end
@doc """
Gets a single topic_subscription.
Raises `Ecto.NoResultsError` if the Topic subscription does not exist.
## Examples
iex> get_topic_subscription!(123)
%TopicSubscription{}
iex> get_topic_subscription!(456)
** (Ecto.NoResultsError)
"""
def get_topic_subscription!(id), do: Repo.get!(TopicSubscription, id)
@doc """
Creates a topic_subscription.
## Examples
iex> create_topic_subscription(%{field: value})
{:ok, %TopicSubscription{}}
iex> create_topic_subscription(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_topic_subscription(attrs \\ %{}) do
%TopicSubscription{}
|> TopicSubscription.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a topic_subscription.
## Examples
iex> update_topic_subscription(topic_subscription, %{field: new_value})
{:ok, %TopicSubscription{}}
iex> update_topic_subscription(topic_subscription, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_topic_subscription(%TopicSubscription{} = topic_subscription, attrs) do
topic_subscription
|> TopicSubscription.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a TopicSubscription.
## Examples
iex> delete_topic_subscription(topic_subscription)
{:ok, %TopicSubscription{}}
iex> delete_topic_subscription(topic_subscription)
{:error, %Ecto.Changeset{}}
"""
def delete_topic_subscription(%TopicSubscription{} = topic_subscription) do
Repo.delete(topic_subscription)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking topic_subscription changes.
## Examples
iex> change_topic_subscription(topic_subscription)
%Ecto.Changeset{source: %TopicSubscription{}}
"""
def change_topic_subscription(%TopicSubscription{} = topic_subscription) do
TopicSubscription.changeset(topic_subscription, %{})
end
end

View file

@ -0,0 +1,18 @@
defmodule Philomena.Forums.TopicSubscription do
use Ecto.Schema
import Ecto.Changeset
@primary_key false
schema "topic_subscriptions" do
belongs_to :topic, Philomena.Forums.Topic, primary_key: true
belongs_to :user, Philomena.Users.User, primary_key: true
end
@doc false
def changeset(topic_subscription, attrs) do
topic_subscription
|> cast(attrs, [])
|> validate_required([])
end
end

View file

@ -344,4 +344,61 @@ defmodule Philomena.ForumsTest do
assert %Ecto.Changeset{} = Forums.change_poll_vote(poll_vote)
end
end
describe "topic_subscriptions" do
alias Philomena.Forums.TopicSubscription
@valid_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
def topic_subscription_fixture(attrs \\ %{}) do
{:ok, topic_subscription} =
attrs
|> Enum.into(@valid_attrs)
|> Forums.create_topic_subscription()
topic_subscription
end
test "list_topic_subscriptions/0 returns all topic_subscriptions" do
topic_subscription = topic_subscription_fixture()
assert Forums.list_topic_subscriptions() == [topic_subscription]
end
test "get_topic_subscription!/1 returns the topic_subscription with given id" do
topic_subscription = topic_subscription_fixture()
assert Forums.get_topic_subscription!(topic_subscription.id) == topic_subscription
end
test "create_topic_subscription/1 with valid data creates a topic_subscription" do
assert {:ok, %TopicSubscription{} = topic_subscription} = Forums.create_topic_subscription(@valid_attrs)
end
test "create_topic_subscription/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Forums.create_topic_subscription(@invalid_attrs)
end
test "update_topic_subscription/2 with valid data updates the topic_subscription" do
topic_subscription = topic_subscription_fixture()
assert {:ok, %TopicSubscription{} = topic_subscription} = Forums.update_topic_subscription(topic_subscription, @update_attrs)
end
test "update_topic_subscription/2 with invalid data returns error changeset" do
topic_subscription = topic_subscription_fixture()
assert {:error, %Ecto.Changeset{}} = Forums.update_topic_subscription(topic_subscription, @invalid_attrs)
assert topic_subscription == Forums.get_topic_subscription!(topic_subscription.id)
end
test "delete_topic_subscription/1 deletes the topic_subscription" do
topic_subscription = topic_subscription_fixture()
assert {:ok, %TopicSubscription{}} = Forums.delete_topic_subscription(topic_subscription)
assert_raise Ecto.NoResultsError, fn -> Forums.get_topic_subscription!(topic_subscription.id) end
end
test "change_topic_subscription/1 returns a topic_subscription changeset" do
topic_subscription = topic_subscription_fixture()
assert %Ecto.Changeset{} = Forums.change_topic_subscription(topic_subscription)
end
end
end