From 9802454a48de851510f1b37fa6b8203570b8e4d2 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 30 Sep 2019 20:50:34 -0400 Subject: [PATCH] fix forums associations --- lib/philomena/forums.ex | 576 ------------------ lib/philomena/forums/poll_vote.ex | 15 - lib/philomena/poll_options.ex | 104 ++++ lib/philomena/poll_options/poll_option.ex | 18 + lib/philomena/poll_votes.ex | 104 ++++ lib/philomena/poll_votes/poll_vote.ex | 20 + lib/philomena/polls.ex | 104 ++++ .../{forums/polls.ex => polls/poll.ex} | 8 +- lib/philomena/posts.ex | 104 ++++ lib/philomena/{forums => posts}/post.ex | 4 +- lib/philomena/topics.ex | 200 ++++++ .../subscription.ex} | 8 +- lib/philomena/{forums => topics}/topic.ex | 4 +- test/philomena/poll_options_test.exs | 62 ++ test/philomena/poll_votes_test.exs | 62 ++ test/philomena/polls_test.exs | 62 ++ test/philomena/posts_test.exs | 62 ++ test/philomena/topics_test.exs | 119 ++++ 18 files changed, 1033 insertions(+), 603 deletions(-) delete mode 100644 lib/philomena/forums/poll_vote.ex create mode 100644 lib/philomena/poll_options.ex create mode 100644 lib/philomena/poll_options/poll_option.ex create mode 100644 lib/philomena/poll_votes.ex create mode 100644 lib/philomena/poll_votes/poll_vote.ex create mode 100644 lib/philomena/polls.ex rename lib/philomena/{forums/polls.ex => polls/poll.ex} (79%) create mode 100644 lib/philomena/posts.ex rename lib/philomena/{forums => posts}/post.ex (91%) create mode 100644 lib/philomena/topics.ex rename lib/philomena/{forums/topic_subscription.ex => topics/subscription.ex} (57%) rename lib/philomena/{forums => topics}/topic.ex (90%) create mode 100644 test/philomena/poll_options_test.exs create mode 100644 test/philomena/poll_votes_test.exs create mode 100644 test/philomena/polls_test.exs create mode 100644 test/philomena/posts_test.exs create mode 100644 test/philomena/topics_test.exs diff --git a/lib/philomena/forums.ex b/lib/philomena/forums.ex index 00132382..374d0bab 100644 --- a/lib/philomena/forums.ex +++ b/lib/philomena/forums.ex @@ -101,580 +101,4 @@ defmodule Philomena.Forums do def change_forum(%Forum{} = forum) do Forum.changeset(forum, %{}) end - - alias Philomena.Forums.Topic - - @doc """ - Returns the list of topics. - - ## Examples - - iex> list_topics() - [%Topic{}, ...] - - """ - def list_topics do - Repo.all(Topic) - end - - @doc """ - Gets a single topic. - - Raises `Ecto.NoResultsError` if the Topic does not exist. - - ## Examples - - iex> get_topic!(123) - %Topic{} - - iex> get_topic!(456) - ** (Ecto.NoResultsError) - - """ - def get_topic!(id), do: Repo.get!(Topic, id) - - @doc """ - Creates a topic. - - ## Examples - - iex> create_topic(%{field: value}) - {:ok, %Topic{}} - - iex> create_topic(%{field: bad_value}) - {:error, %Ecto.Changeset{}} - - """ - def create_topic(attrs \\ %{}) do - %Topic{} - |> Topic.changeset(attrs) - |> Repo.insert() - end - - @doc """ - Updates a topic. - - ## Examples - - iex> update_topic(topic, %{field: new_value}) - {:ok, %Topic{}} - - iex> update_topic(topic, %{field: bad_value}) - {:error, %Ecto.Changeset{}} - - """ - def update_topic(%Topic{} = topic, attrs) do - topic - |> Topic.changeset(attrs) - |> Repo.update() - end - - @doc """ - Deletes a Topic. - - ## Examples - - iex> delete_topic(topic) - {:ok, %Topic{}} - - iex> delete_topic(topic) - {:error, %Ecto.Changeset{}} - - """ - def delete_topic(%Topic{} = topic) do - Repo.delete(topic) - end - - @doc """ - Returns an `%Ecto.Changeset{}` for tracking topic changes. - - ## Examples - - iex> change_topic(topic) - %Ecto.Changeset{source: %Topic{}} - - """ - def change_topic(%Topic{} = topic) do - Topic.changeset(topic, %{}) - end - - alias Philomena.Forums.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{source: %Post{}} - - """ - def change_post(%Post{} = post) do - Post.changeset(post, %{}) - end - - alias Philomena.Forums.Subscription - - @doc """ - Returns the list of forum_subscriptions. - - ## Examples - - iex> list_forum_subscriptions() - [%Subscription{}, ...] - - """ - def list_forum_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 - - alias Philomena.Forums.Polls - - @doc """ - Returns the list of polls. - - ## Examples - - iex> list_polls() - [%Polls{}, ...] - - """ - def list_polls do - Repo.all(Polls) - end - - @doc """ - Gets a single polls. - - Raises `Ecto.NoResultsError` if the Polls does not exist. - - ## Examples - - iex> get_polls!(123) - %Polls{} - - iex> get_polls!(456) - ** (Ecto.NoResultsError) - - """ - def get_polls!(id), do: Repo.get!(Polls, id) - - @doc """ - Creates a polls. - - ## Examples - - iex> create_polls(%{field: value}) - {:ok, %Polls{}} - - iex> create_polls(%{field: bad_value}) - {:error, %Ecto.Changeset{}} - - """ - def create_polls(attrs \\ %{}) do - %Polls{} - |> Polls.changeset(attrs) - |> Repo.insert() - end - - @doc """ - Updates a polls. - - ## Examples - - iex> update_polls(polls, %{field: new_value}) - {:ok, %Polls{}} - - iex> update_polls(polls, %{field: bad_value}) - {:error, %Ecto.Changeset{}} - - """ - def update_polls(%Polls{} = polls, attrs) do - polls - |> Polls.changeset(attrs) - |> Repo.update() - end - - @doc """ - Deletes a Polls. - - ## Examples - - iex> delete_polls(polls) - {:ok, %Polls{}} - - iex> delete_polls(polls) - {:error, %Ecto.Changeset{}} - - """ - def delete_polls(%Polls{} = polls) do - Repo.delete(polls) - end - - @doc """ - Returns an `%Ecto.Changeset{}` for tracking polls changes. - - ## Examples - - iex> change_polls(polls) - %Ecto.Changeset{source: %Polls{}} - - """ - def change_polls(%Polls{} = polls) do - Polls.changeset(polls, %{}) - end - - alias Philomena.Forums.PollVote - - @doc """ - Returns the list of poll_votes. - - ## Examples - - iex> list_poll_votes() - [%PollVote{}, ...] - - """ - def list_poll_votes do - Repo.all(PollVote) - end - - @doc """ - Gets a single poll_vote. - - Raises `Ecto.NoResultsError` if the Poll vote does not exist. - - ## Examples - - iex> get_poll_vote!(123) - %PollVote{} - - iex> get_poll_vote!(456) - ** (Ecto.NoResultsError) - - """ - def get_poll_vote!(id), do: Repo.get!(PollVote, id) - - @doc """ - Creates a poll_vote. - - ## Examples - - iex> create_poll_vote(%{field: value}) - {:ok, %PollVote{}} - - iex> create_poll_vote(%{field: bad_value}) - {:error, %Ecto.Changeset{}} - - """ - def create_poll_vote(attrs \\ %{}) do - %PollVote{} - |> PollVote.changeset(attrs) - |> Repo.insert() - end - - @doc """ - Updates a poll_vote. - - ## Examples - - iex> update_poll_vote(poll_vote, %{field: new_value}) - {:ok, %PollVote{}} - - iex> update_poll_vote(poll_vote, %{field: bad_value}) - {:error, %Ecto.Changeset{}} - - """ - def update_poll_vote(%PollVote{} = poll_vote, attrs) do - poll_vote - |> PollVote.changeset(attrs) - |> Repo.update() - end - - @doc """ - Deletes a PollVote. - - ## Examples - - iex> delete_poll_vote(poll_vote) - {:ok, %PollVote{}} - - iex> delete_poll_vote(poll_vote) - {:error, %Ecto.Changeset{}} - - """ - def delete_poll_vote(%PollVote{} = poll_vote) do - Repo.delete(poll_vote) - end - - @doc """ - Returns an `%Ecto.Changeset{}` for tracking poll_vote changes. - - ## Examples - - iex> change_poll_vote(poll_vote) - %Ecto.Changeset{source: %PollVote{}} - - """ - 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 diff --git a/lib/philomena/forums/poll_vote.ex b/lib/philomena/forums/poll_vote.ex deleted file mode 100644 index 57a13f6a..00000000 --- a/lib/philomena/forums/poll_vote.ex +++ /dev/null @@ -1,15 +0,0 @@ -defmodule Philomena.Forums.PollVote do - use Ecto.Schema - import Ecto.Changeset - - schema "poll_votes" do - timestamps() - end - - @doc false - def changeset(poll_vote, attrs) do - poll_vote - |> cast(attrs, []) - |> validate_required([]) - end -end diff --git a/lib/philomena/poll_options.ex b/lib/philomena/poll_options.ex new file mode 100644 index 00000000..f9b28d88 --- /dev/null +++ b/lib/philomena/poll_options.ex @@ -0,0 +1,104 @@ +defmodule Philomena.PollOptions do + @moduledoc """ + The PollOptions context. + """ + + import Ecto.Query, warn: false + alias Philomena.Repo + + alias Philomena.PollOptions.PollOption + + @doc """ + Returns the list of poll_options. + + ## Examples + + iex> list_poll_options() + [%PollOption{}, ...] + + """ + def list_poll_options do + Repo.all(PollOption) + end + + @doc """ + Gets a single poll_option. + + Raises `Ecto.NoResultsError` if the Poll option does not exist. + + ## Examples + + iex> get_poll_option!(123) + %PollOption{} + + iex> get_poll_option!(456) + ** (Ecto.NoResultsError) + + """ + def get_poll_option!(id), do: Repo.get!(PollOption, id) + + @doc """ + Creates a poll_option. + + ## Examples + + iex> create_poll_option(%{field: value}) + {:ok, %PollOption{}} + + iex> create_poll_option(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_poll_option(attrs \\ %{}) do + %PollOption{} + |> PollOption.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a poll_option. + + ## Examples + + iex> update_poll_option(poll_option, %{field: new_value}) + {:ok, %PollOption{}} + + iex> update_poll_option(poll_option, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_poll_option(%PollOption{} = poll_option, attrs) do + poll_option + |> PollOption.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a PollOption. + + ## Examples + + iex> delete_poll_option(poll_option) + {:ok, %PollOption{}} + + iex> delete_poll_option(poll_option) + {:error, %Ecto.Changeset{}} + + """ + def delete_poll_option(%PollOption{} = poll_option) do + Repo.delete(poll_option) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking poll_option changes. + + ## Examples + + iex> change_poll_option(poll_option) + %Ecto.Changeset{source: %PollOption{}} + + """ + def change_poll_option(%PollOption{} = poll_option) do + PollOption.changeset(poll_option, %{}) + end +end diff --git a/lib/philomena/poll_options/poll_option.ex b/lib/philomena/poll_options/poll_option.ex new file mode 100644 index 00000000..9deef755 --- /dev/null +++ b/lib/philomena/poll_options/poll_option.ex @@ -0,0 +1,18 @@ +defmodule Philomena.PollOptions.PollOption do + use Ecto.Schema + import Ecto.Changeset + + schema "poll_options" do + belongs_to :poll, Philomena.Polls.Poll + + field :label, :string + field :vote_count, :integer, default: 0 + end + + @doc false + def changeset(poll_option, attrs) do + poll_option + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/lib/philomena/poll_votes.ex b/lib/philomena/poll_votes.ex new file mode 100644 index 00000000..33fc07bd --- /dev/null +++ b/lib/philomena/poll_votes.ex @@ -0,0 +1,104 @@ +defmodule Philomena.PollVotes do + @moduledoc """ + The PollVotes context. + """ + + import Ecto.Query, warn: false + alias Philomena.Repo + + alias Philomena.PollVotes.PollVote + + @doc """ + Returns the list of poll_votes. + + ## Examples + + iex> list_poll_votes() + [%PollVote{}, ...] + + """ + def list_poll_votes do + Repo.all(PollVote) + end + + @doc """ + Gets a single poll_vote. + + Raises `Ecto.NoResultsError` if the Poll vote does not exist. + + ## Examples + + iex> get_poll_vote!(123) + %PollVote{} + + iex> get_poll_vote!(456) + ** (Ecto.NoResultsError) + + """ + def get_poll_vote!(id), do: Repo.get!(PollVote, id) + + @doc """ + Creates a poll_vote. + + ## Examples + + iex> create_poll_vote(%{field: value}) + {:ok, %PollVote{}} + + iex> create_poll_vote(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_poll_vote(attrs \\ %{}) do + %PollVote{} + |> PollVote.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a poll_vote. + + ## Examples + + iex> update_poll_vote(poll_vote, %{field: new_value}) + {:ok, %PollVote{}} + + iex> update_poll_vote(poll_vote, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_poll_vote(%PollVote{} = poll_vote, attrs) do + poll_vote + |> PollVote.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a PollVote. + + ## Examples + + iex> delete_poll_vote(poll_vote) + {:ok, %PollVote{}} + + iex> delete_poll_vote(poll_vote) + {:error, %Ecto.Changeset{}} + + """ + def delete_poll_vote(%PollVote{} = poll_vote) do + Repo.delete(poll_vote) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking poll_vote changes. + + ## Examples + + iex> change_poll_vote(poll_vote) + %Ecto.Changeset{source: %PollVote{}} + + """ + def change_poll_vote(%PollVote{} = poll_vote) do + PollVote.changeset(poll_vote, %{}) + end +end diff --git a/lib/philomena/poll_votes/poll_vote.ex b/lib/philomena/poll_votes/poll_vote.ex new file mode 100644 index 00000000..037d2dda --- /dev/null +++ b/lib/philomena/poll_votes/poll_vote.ex @@ -0,0 +1,20 @@ +defmodule Philomena.PollVotes.PollVote do + use Ecto.Schema + import Ecto.Changeset + + schema "poll_votes" do + belongs_to :poll_option, Philomena.PollOptions.PollOption + belongs_to :user, Philomena.Users.User + + field :rank, :integer + + timestamps(inserted_at: :created_at, updated_at: false) + end + + @doc false + def changeset(poll_vote, attrs) do + poll_vote + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/lib/philomena/polls.ex b/lib/philomena/polls.ex new file mode 100644 index 00000000..0d80a454 --- /dev/null +++ b/lib/philomena/polls.ex @@ -0,0 +1,104 @@ +defmodule Philomena.Polls do + @moduledoc """ + The Polls context. + """ + + import Ecto.Query, warn: false + alias Philomena.Repo + + alias Philomena.Polls.Poll + + @doc """ + Returns the list of polls. + + ## Examples + + iex> list_polls() + [%Poll{}, ...] + + """ + def list_polls do + Repo.all(Poll) + end + + @doc """ + Gets a single poll. + + Raises `Ecto.NoResultsError` if the Poll does not exist. + + ## Examples + + iex> get_poll!(123) + %Poll{} + + iex> get_poll!(456) + ** (Ecto.NoResultsError) + + """ + def get_poll!(id), do: Repo.get!(Poll, id) + + @doc """ + Creates a poll. + + ## Examples + + iex> create_poll(%{field: value}) + {:ok, %Poll{}} + + iex> create_poll(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_poll(attrs \\ %{}) do + %Poll{} + |> Poll.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a poll. + + ## Examples + + iex> update_poll(poll, %{field: new_value}) + {:ok, %Poll{}} + + iex> update_poll(poll, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_poll(%Poll{} = poll, attrs) do + poll + |> Poll.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a Poll. + + ## Examples + + iex> delete_poll(poll) + {:ok, %Poll{}} + + iex> delete_poll(poll) + {:error, %Ecto.Changeset{}} + + """ + def delete_poll(%Poll{} = poll) do + Repo.delete(poll) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking poll changes. + + ## Examples + + iex> change_poll(poll) + %Ecto.Changeset{source: %Poll{}} + + """ + def change_poll(%Poll{} = poll) do + Poll.changeset(poll, %{}) + end +end diff --git a/lib/philomena/forums/polls.ex b/lib/philomena/polls/poll.ex similarity index 79% rename from lib/philomena/forums/polls.ex rename to lib/philomena/polls/poll.ex index 2c2c46ad..0e795511 100644 --- a/lib/philomena/forums/polls.ex +++ b/lib/philomena/polls/poll.ex @@ -1,9 +1,9 @@ -defmodule Philomena.Forums.Polls do +defmodule Philomena.Polls.Poll do use Ecto.Schema import Ecto.Changeset schema "polls" do - belongs_to :topic, Philomena.Forums.Topic + belongs_to :topic, Philomena.Topics.Topic belongs_to :deleted_by, Philomena.Users.User field :title, :string @@ -17,8 +17,8 @@ defmodule Philomena.Forums.Polls do end @doc false - def changeset(polls, attrs) do - polls + def changeset(poll, attrs) do + poll |> cast(attrs, []) |> validate_required([]) end diff --git a/lib/philomena/posts.ex b/lib/philomena/posts.ex new file mode 100644 index 00000000..61e201f3 --- /dev/null +++ b/lib/philomena/posts.ex @@ -0,0 +1,104 @@ +defmodule Philomena.Posts do + @moduledoc """ + The Posts context. + """ + + import Ecto.Query, warn: false + alias Philomena.Repo + + alias Philomena.Posts.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{source: %Post{}} + + """ + def change_post(%Post{} = post) do + Post.changeset(post, %{}) + end +end diff --git a/lib/philomena/forums/post.ex b/lib/philomena/posts/post.ex similarity index 91% rename from lib/philomena/forums/post.ex rename to lib/philomena/posts/post.ex index 60664ec7..08ca746c 100644 --- a/lib/philomena/forums/post.ex +++ b/lib/philomena/posts/post.ex @@ -1,10 +1,10 @@ -defmodule Philomena.Forums.Post do +defmodule Philomena.Posts.Post do use Ecto.Schema import Ecto.Changeset schema "posts" do belongs_to :user, Philomena.Users.User - belongs_to :topic, Philomena.Forums.Topic + belongs_to :topic, Philomena.Topics.Topic belongs_to :deleted_by, Philomena.Users.User field :body, :string diff --git a/lib/philomena/topics.ex b/lib/philomena/topics.ex new file mode 100644 index 00000000..dff0de19 --- /dev/null +++ b/lib/philomena/topics.ex @@ -0,0 +1,200 @@ +defmodule Philomena.Topics do + @moduledoc """ + The Topics context. + """ + + import Ecto.Query, warn: false + alias Philomena.Repo + + alias Philomena.Topics.Topic + + @doc """ + Returns the list of topics. + + ## Examples + + iex> list_topics() + [%Topic{}, ...] + + """ + def list_topics do + Repo.all(Topic) + end + + @doc """ + Gets a single topic. + + Raises `Ecto.NoResultsError` if the Topic does not exist. + + ## Examples + + iex> get_topic!(123) + %Topic{} + + iex> get_topic!(456) + ** (Ecto.NoResultsError) + + """ + def get_topic!(id), do: Repo.get!(Topic, id) + + @doc """ + Creates a topic. + + ## Examples + + iex> create_topic(%{field: value}) + {:ok, %Topic{}} + + iex> create_topic(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_topic(attrs \\ %{}) do + %Topic{} + |> Topic.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a topic. + + ## Examples + + iex> update_topic(topic, %{field: new_value}) + {:ok, %Topic{}} + + iex> update_topic(topic, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_topic(%Topic{} = topic, attrs) do + topic + |> Topic.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a Topic. + + ## Examples + + iex> delete_topic(topic) + {:ok, %Topic{}} + + iex> delete_topic(topic) + {:error, %Ecto.Changeset{}} + + """ + def delete_topic(%Topic{} = topic) do + Repo.delete(topic) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking topic changes. + + ## Examples + + iex> change_topic(topic) + %Ecto.Changeset{source: %Topic{}} + + """ + def change_topic(%Topic{} = topic) do + Topic.changeset(topic, %{}) + end + + alias Philomena.Topics.Subscription + + @doc """ + Returns the list of topic_subscriptions. + + ## Examples + + iex> list_topic_subscriptions() + [%Subscription{}, ...] + + """ + def list_topic_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 diff --git a/lib/philomena/forums/topic_subscription.ex b/lib/philomena/topics/subscription.ex similarity index 57% rename from lib/philomena/forums/topic_subscription.ex rename to lib/philomena/topics/subscription.ex index 6c559abc..42e11751 100644 --- a/lib/philomena/forums/topic_subscription.ex +++ b/lib/philomena/topics/subscription.ex @@ -1,17 +1,17 @@ -defmodule Philomena.Forums.TopicSubscription do +defmodule Philomena.Topics.Subscription 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 :topic, Philomena.Topics.Topic, primary_key: true belongs_to :user, Philomena.Users.User, primary_key: true end @doc false - def changeset(topic_subscription, attrs) do - topic_subscription + def changeset(subscription, attrs) do + subscription |> cast(attrs, []) |> validate_required([]) end diff --git a/lib/philomena/forums/topic.ex b/lib/philomena/topics/topic.ex similarity index 90% rename from lib/philomena/forums/topic.ex rename to lib/philomena/topics/topic.ex index e2da0d09..de74f1ee 100644 --- a/lib/philomena/forums/topic.ex +++ b/lib/philomena/topics/topic.ex @@ -1,4 +1,4 @@ -defmodule Philomena.Forums.Topic do +defmodule Philomena.Topics.Topic do use Ecto.Schema import Ecto.Changeset @@ -6,7 +6,7 @@ defmodule Philomena.Forums.Topic do belongs_to :user, Philomena.Users.User belongs_to :deleted_by, Philomena.Users.User belongs_to :locked_by, Philomena.Users.User - belongs_to :last_post, Philomena.Forums.Post + belongs_to :last_post, Philomena.Posts.Post field :title, :string field :post_count, :integer, default: 0 diff --git a/test/philomena/poll_options_test.exs b/test/philomena/poll_options_test.exs new file mode 100644 index 00000000..441a7291 --- /dev/null +++ b/test/philomena/poll_options_test.exs @@ -0,0 +1,62 @@ +defmodule Philomena.PollOptionsTest do + use Philomena.DataCase + + alias Philomena.PollOptions + + describe "poll_options" do + alias Philomena.PollOptions.PollOption + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def poll_option_fixture(attrs \\ %{}) do + {:ok, poll_option} = + attrs + |> Enum.into(@valid_attrs) + |> PollOptions.create_poll_option() + + poll_option + end + + test "list_poll_options/0 returns all poll_options" do + poll_option = poll_option_fixture() + assert PollOptions.list_poll_options() == [poll_option] + end + + test "get_poll_option!/1 returns the poll_option with given id" do + poll_option = poll_option_fixture() + assert PollOptions.get_poll_option!(poll_option.id) == poll_option + end + + test "create_poll_option/1 with valid data creates a poll_option" do + assert {:ok, %PollOption{} = poll_option} = PollOptions.create_poll_option(@valid_attrs) + end + + test "create_poll_option/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = PollOptions.create_poll_option(@invalid_attrs) + end + + test "update_poll_option/2 with valid data updates the poll_option" do + poll_option = poll_option_fixture() + assert {:ok, %PollOption{} = poll_option} = PollOptions.update_poll_option(poll_option, @update_attrs) + end + + test "update_poll_option/2 with invalid data returns error changeset" do + poll_option = poll_option_fixture() + assert {:error, %Ecto.Changeset{}} = PollOptions.update_poll_option(poll_option, @invalid_attrs) + assert poll_option == PollOptions.get_poll_option!(poll_option.id) + end + + test "delete_poll_option/1 deletes the poll_option" do + poll_option = poll_option_fixture() + assert {:ok, %PollOption{}} = PollOptions.delete_poll_option(poll_option) + assert_raise Ecto.NoResultsError, fn -> PollOptions.get_poll_option!(poll_option.id) end + end + + test "change_poll_option/1 returns a poll_option changeset" do + poll_option = poll_option_fixture() + assert %Ecto.Changeset{} = PollOptions.change_poll_option(poll_option) + end + end +end diff --git a/test/philomena/poll_votes_test.exs b/test/philomena/poll_votes_test.exs new file mode 100644 index 00000000..20e372b7 --- /dev/null +++ b/test/philomena/poll_votes_test.exs @@ -0,0 +1,62 @@ +defmodule Philomena.PollVotesTest do + use Philomena.DataCase + + alias Philomena.PollVotes + + describe "poll_votes" do + alias Philomena.PollVotes.PollVote + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def poll_vote_fixture(attrs \\ %{}) do + {:ok, poll_vote} = + attrs + |> Enum.into(@valid_attrs) + |> PollVotes.create_poll_vote() + + poll_vote + end + + test "list_poll_votes/0 returns all poll_votes" do + poll_vote = poll_vote_fixture() + assert PollVotes.list_poll_votes() == [poll_vote] + end + + test "get_poll_vote!/1 returns the poll_vote with given id" do + poll_vote = poll_vote_fixture() + assert PollVotes.get_poll_vote!(poll_vote.id) == poll_vote + end + + test "create_poll_vote/1 with valid data creates a poll_vote" do + assert {:ok, %PollVote{} = poll_vote} = PollVotes.create_poll_vote(@valid_attrs) + end + + test "create_poll_vote/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = PollVotes.create_poll_vote(@invalid_attrs) + end + + test "update_poll_vote/2 with valid data updates the poll_vote" do + poll_vote = poll_vote_fixture() + assert {:ok, %PollVote{} = poll_vote} = PollVotes.update_poll_vote(poll_vote, @update_attrs) + end + + test "update_poll_vote/2 with invalid data returns error changeset" do + poll_vote = poll_vote_fixture() + assert {:error, %Ecto.Changeset{}} = PollVotes.update_poll_vote(poll_vote, @invalid_attrs) + assert poll_vote == PollVotes.get_poll_vote!(poll_vote.id) + end + + test "delete_poll_vote/1 deletes the poll_vote" do + poll_vote = poll_vote_fixture() + assert {:ok, %PollVote{}} = PollVotes.delete_poll_vote(poll_vote) + assert_raise Ecto.NoResultsError, fn -> PollVotes.get_poll_vote!(poll_vote.id) end + end + + test "change_poll_vote/1 returns a poll_vote changeset" do + poll_vote = poll_vote_fixture() + assert %Ecto.Changeset{} = PollVotes.change_poll_vote(poll_vote) + end + end +end diff --git a/test/philomena/polls_test.exs b/test/philomena/polls_test.exs new file mode 100644 index 00000000..4c36cfab --- /dev/null +++ b/test/philomena/polls_test.exs @@ -0,0 +1,62 @@ +defmodule Philomena.PollsTest do + use Philomena.DataCase + + alias Philomena.Polls + + describe "polls" do + alias Philomena.Polls.Poll + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def poll_fixture(attrs \\ %{}) do + {:ok, poll} = + attrs + |> Enum.into(@valid_attrs) + |> Polls.create_poll() + + poll + end + + test "list_polls/0 returns all polls" do + poll = poll_fixture() + assert Polls.list_polls() == [poll] + end + + test "get_poll!/1 returns the poll with given id" do + poll = poll_fixture() + assert Polls.get_poll!(poll.id) == poll + end + + test "create_poll/1 with valid data creates a poll" do + assert {:ok, %Poll{} = poll} = Polls.create_poll(@valid_attrs) + end + + test "create_poll/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Polls.create_poll(@invalid_attrs) + end + + test "update_poll/2 with valid data updates the poll" do + poll = poll_fixture() + assert {:ok, %Poll{} = poll} = Polls.update_poll(poll, @update_attrs) + end + + test "update_poll/2 with invalid data returns error changeset" do + poll = poll_fixture() + assert {:error, %Ecto.Changeset{}} = Polls.update_poll(poll, @invalid_attrs) + assert poll == Polls.get_poll!(poll.id) + end + + test "delete_poll/1 deletes the poll" do + poll = poll_fixture() + assert {:ok, %Poll{}} = Polls.delete_poll(poll) + assert_raise Ecto.NoResultsError, fn -> Polls.get_poll!(poll.id) end + end + + test "change_poll/1 returns a poll changeset" do + poll = poll_fixture() + assert %Ecto.Changeset{} = Polls.change_poll(poll) + end + end +end diff --git a/test/philomena/posts_test.exs b/test/philomena/posts_test.exs new file mode 100644 index 00000000..130a6ee7 --- /dev/null +++ b/test/philomena/posts_test.exs @@ -0,0 +1,62 @@ +defmodule Philomena.PostsTest do + use Philomena.DataCase + + alias Philomena.Posts + + describe "posts" do + alias Philomena.Posts.Post + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def post_fixture(attrs \\ %{}) do + {:ok, post} = + attrs + |> Enum.into(@valid_attrs) + |> Posts.create_post() + + post + end + + test "list_posts/0 returns all posts" do + post = post_fixture() + assert Posts.list_posts() == [post] + end + + test "get_post!/1 returns the post with given id" do + post = post_fixture() + assert Posts.get_post!(post.id) == post + end + + test "create_post/1 with valid data creates a post" do + assert {:ok, %Post{} = post} = Posts.create_post(@valid_attrs) + end + + test "create_post/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Posts.create_post(@invalid_attrs) + end + + test "update_post/2 with valid data updates the post" do + post = post_fixture() + assert {:ok, %Post{} = post} = Posts.update_post(post, @update_attrs) + end + + test "update_post/2 with invalid data returns error changeset" do + post = post_fixture() + assert {:error, %Ecto.Changeset{}} = Posts.update_post(post, @invalid_attrs) + assert post == Posts.get_post!(post.id) + end + + test "delete_post/1 deletes the post" do + post = post_fixture() + assert {:ok, %Post{}} = Posts.delete_post(post) + assert_raise Ecto.NoResultsError, fn -> Posts.get_post!(post.id) end + end + + test "change_post/1 returns a post changeset" do + post = post_fixture() + assert %Ecto.Changeset{} = Posts.change_post(post) + end + end +end diff --git a/test/philomena/topics_test.exs b/test/philomena/topics_test.exs new file mode 100644 index 00000000..08e3ca97 --- /dev/null +++ b/test/philomena/topics_test.exs @@ -0,0 +1,119 @@ +defmodule Philomena.TopicsTest do + use Philomena.DataCase + + alias Philomena.Topics + + describe "topics" do + alias Philomena.Topics.Topic + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def topic_fixture(attrs \\ %{}) do + {:ok, topic} = + attrs + |> Enum.into(@valid_attrs) + |> Topics.create_topic() + + topic + end + + test "list_topics/0 returns all topics" do + topic = topic_fixture() + assert Topics.list_topics() == [topic] + end + + test "get_topic!/1 returns the topic with given id" do + topic = topic_fixture() + assert Topics.get_topic!(topic.id) == topic + end + + test "create_topic/1 with valid data creates a topic" do + assert {:ok, %Topic{} = topic} = Topics.create_topic(@valid_attrs) + end + + test "create_topic/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Topics.create_topic(@invalid_attrs) + end + + test "update_topic/2 with valid data updates the topic" do + topic = topic_fixture() + assert {:ok, %Topic{} = topic} = Topics.update_topic(topic, @update_attrs) + end + + test "update_topic/2 with invalid data returns error changeset" do + topic = topic_fixture() + assert {:error, %Ecto.Changeset{}} = Topics.update_topic(topic, @invalid_attrs) + assert topic == Topics.get_topic!(topic.id) + end + + test "delete_topic/1 deletes the topic" do + topic = topic_fixture() + assert {:ok, %Topic{}} = Topics.delete_topic(topic) + assert_raise Ecto.NoResultsError, fn -> Topics.get_topic!(topic.id) end + end + + test "change_topic/1 returns a topic changeset" do + topic = topic_fixture() + assert %Ecto.Changeset{} = Topics.change_topic(topic) + end + end + + describe "topic_subscriptions" do + alias Philomena.Topics.Subscription + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def subscription_fixture(attrs \\ %{}) do + {:ok, subscription} = + attrs + |> Enum.into(@valid_attrs) + |> Topics.create_subscription() + + subscription + end + + test "list_topic_subscriptions/0 returns all topic_subscriptions" do + subscription = subscription_fixture() + assert Topics.list_topic_subscriptions() == [subscription] + end + + test "get_subscription!/1 returns the subscription with given id" do + subscription = subscription_fixture() + assert Topics.get_subscription!(subscription.id) == subscription + end + + test "create_subscription/1 with valid data creates a subscription" do + assert {:ok, %Subscription{} = subscription} = Topics.create_subscription(@valid_attrs) + end + + test "create_subscription/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Topics.create_subscription(@invalid_attrs) + end + + test "update_subscription/2 with valid data updates the subscription" do + subscription = subscription_fixture() + assert {:ok, %Subscription{} = subscription} = Topics.update_subscription(subscription, @update_attrs) + end + + test "update_subscription/2 with invalid data returns error changeset" do + subscription = subscription_fixture() + assert {:error, %Ecto.Changeset{}} = Topics.update_subscription(subscription, @invalid_attrs) + assert subscription == Topics.get_subscription!(subscription.id) + end + + test "delete_subscription/1 deletes the subscription" do + subscription = subscription_fixture() + assert {:ok, %Subscription{}} = Topics.delete_subscription(subscription) + assert_raise Ecto.NoResultsError, fn -> Topics.get_subscription!(subscription.id) end + end + + test "change_subscription/1 returns a subscription changeset" do + subscription = subscription_fixture() + assert %Ecto.Changeset{} = Topics.change_subscription(subscription) + end + end +end