diff --git a/lib/philomena/notifications.ex b/lib/philomena/notifications.ex index 7bf97951..f6c1dc64 100644 --- a/lib/philomena/notifications.ex +++ b/lib/philomena/notifications.ex @@ -101,4 +101,100 @@ defmodule Philomena.Notifications do def change_notification(%Notification{} = notification) do Notification.changeset(notification, %{}) end + + alias Philomena.Notifications.UnreadNotification + + @doc """ + Returns the list of unread_notifications. + + ## Examples + + iex> list_unread_notifications() + [%UnreadNotification{}, ...] + + """ + def list_unread_notifications do + Repo.all(UnreadNotification) + end + + @doc """ + Gets a single unread_notification. + + Raises `Ecto.NoResultsError` if the Unread notification does not exist. + + ## Examples + + iex> get_unread_notification!(123) + %UnreadNotification{} + + iex> get_unread_notification!(456) + ** (Ecto.NoResultsError) + + """ + def get_unread_notification!(id), do: Repo.get!(UnreadNotification, id) + + @doc """ + Creates a unread_notification. + + ## Examples + + iex> create_unread_notification(%{field: value}) + {:ok, %UnreadNotification{}} + + iex> create_unread_notification(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_unread_notification(attrs \\ %{}) do + %UnreadNotification{} + |> UnreadNotification.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a unread_notification. + + ## Examples + + iex> update_unread_notification(unread_notification, %{field: new_value}) + {:ok, %UnreadNotification{}} + + iex> update_unread_notification(unread_notification, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_unread_notification(%UnreadNotification{} = unread_notification, attrs) do + unread_notification + |> UnreadNotification.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a UnreadNotification. + + ## Examples + + iex> delete_unread_notification(unread_notification) + {:ok, %UnreadNotification{}} + + iex> delete_unread_notification(unread_notification) + {:error, %Ecto.Changeset{}} + + """ + def delete_unread_notification(%UnreadNotification{} = unread_notification) do + Repo.delete(unread_notification) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking unread_notification changes. + + ## Examples + + iex> change_unread_notification(unread_notification) + %Ecto.Changeset{source: %UnreadNotification{}} + + """ + def change_unread_notification(%UnreadNotification{} = unread_notification) do + UnreadNotification.changeset(unread_notification, %{}) + end end diff --git a/lib/philomena/notifications/unread_notification.ex b/lib/philomena/notifications/unread_notification.ex new file mode 100644 index 00000000..0d4e2a5d --- /dev/null +++ b/lib/philomena/notifications/unread_notification.ex @@ -0,0 +1,18 @@ +defmodule Philomena.Notifications.UnreadNotification do + use Ecto.Schema + import Ecto.Changeset + + @primary_key false + + schema "unread_notifications" do + belongs_to :user, Philomena.Users.User, primary_key: true + belongs_to :notification, Philomena.Notifications.Notification, primary_key: true + end + + @doc false + def changeset(unread_notification, attrs) do + unread_notification + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/test/philomena/notifications_test.exs b/test/philomena/notifications_test.exs index 07124be6..9a00a272 100644 --- a/test/philomena/notifications_test.exs +++ b/test/philomena/notifications_test.exs @@ -59,4 +59,61 @@ defmodule Philomena.NotificationsTest do assert %Ecto.Changeset{} = Notifications.change_notification(notification) end end + + describe "unread_notifications" do + alias Philomena.Notifications.UnreadNotification + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def unread_notification_fixture(attrs \\ %{}) do + {:ok, unread_notification} = + attrs + |> Enum.into(@valid_attrs) + |> Notifications.create_unread_notification() + + unread_notification + end + + test "list_unread_notifications/0 returns all unread_notifications" do + unread_notification = unread_notification_fixture() + assert Notifications.list_unread_notifications() == [unread_notification] + end + + test "get_unread_notification!/1 returns the unread_notification with given id" do + unread_notification = unread_notification_fixture() + assert Notifications.get_unread_notification!(unread_notification.id) == unread_notification + end + + test "create_unread_notification/1 with valid data creates a unread_notification" do + assert {:ok, %UnreadNotification{} = unread_notification} = Notifications.create_unread_notification(@valid_attrs) + end + + test "create_unread_notification/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Notifications.create_unread_notification(@invalid_attrs) + end + + test "update_unread_notification/2 with valid data updates the unread_notification" do + unread_notification = unread_notification_fixture() + assert {:ok, %UnreadNotification{} = unread_notification} = Notifications.update_unread_notification(unread_notification, @update_attrs) + end + + test "update_unread_notification/2 with invalid data returns error changeset" do + unread_notification = unread_notification_fixture() + assert {:error, %Ecto.Changeset{}} = Notifications.update_unread_notification(unread_notification, @invalid_attrs) + assert unread_notification == Notifications.get_unread_notification!(unread_notification.id) + end + + test "delete_unread_notification/1 deletes the unread_notification" do + unread_notification = unread_notification_fixture() + assert {:ok, %UnreadNotification{}} = Notifications.delete_unread_notification(unread_notification) + assert_raise Ecto.NoResultsError, fn -> Notifications.get_unread_notification!(unread_notification.id) end + end + + test "change_unread_notification/1 returns a unread_notification changeset" do + unread_notification = unread_notification_fixture() + assert %Ecto.Changeset{} = Notifications.change_unread_notification(unread_notification) + end + end end