mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-01 03:46:44 +01:00
context: unread_notifications
This commit is contained in:
parent
6920dfba25
commit
73beb449ef
3 changed files with 171 additions and 0 deletions
|
@ -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
|
||||
|
|
18
lib/philomena/notifications/unread_notification.ex
Normal file
18
lib/philomena/notifications/unread_notification.ex
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue