context: notifications

This commit is contained in:
Liam P. White 2019-08-28 13:25:41 -04:00
parent 38cc279d97
commit a9885190e3
3 changed files with 189 additions and 0 deletions

View file

@ -0,0 +1,104 @@
defmodule Philomena.Notifications do
@moduledoc """
The Notifications context.
"""
import Ecto.Query, warn: false
alias Philomena.Repo
alias Philomena.Notifications.Notification
@doc """
Returns the list of notifications.
## Examples
iex> list_notifications()
[%Notification{}, ...]
"""
def list_notifications do
Repo.all(Notification)
end
@doc """
Gets a single notification.
Raises `Ecto.NoResultsError` if the Notification does not exist.
## Examples
iex> get_notification!(123)
%Notification{}
iex> get_notification!(456)
** (Ecto.NoResultsError)
"""
def get_notification!(id), do: Repo.get!(Notification, id)
@doc """
Creates a notification.
## Examples
iex> create_notification(%{field: value})
{:ok, %Notification{}}
iex> create_notification(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_notification(attrs \\ %{}) do
%Notification{}
|> Notification.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a notification.
## Examples
iex> update_notification(notification, %{field: new_value})
{:ok, %Notification{}}
iex> update_notification(notification, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_notification(%Notification{} = notification, attrs) do
notification
|> Notification.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Notification.
## Examples
iex> delete_notification(notification)
{:ok, %Notification{}}
iex> delete_notification(notification)
{:error, %Ecto.Changeset{}}
"""
def delete_notification(%Notification{} = notification) do
Repo.delete(notification)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking notification changes.
## Examples
iex> change_notification(notification)
%Ecto.Changeset{source: %Notification{}}
"""
def change_notification(%Notification{} = notification) do
Notification.changeset(notification, %{})
end
end

View file

@ -0,0 +1,23 @@
defmodule Philomena.Notifications.Notification do
use Ecto.Schema
import Ecto.Changeset
schema "notifications" do
field :action, :string
# fixme: rails polymorphic relation
field :actor_id, :integer
field :actor_type, :string
field :actor_child_id, :integer
field :actor_child_type, :string
timestamps(inserted_at: :created_at)
end
@doc false
def changeset(notification, attrs) do
notification
|> cast(attrs, [])
|> validate_required([])
end
end

View file

@ -0,0 +1,62 @@
defmodule Philomena.NotificationsTest do
use Philomena.DataCase
alias Philomena.Notifications
describe "notifications" do
alias Philomena.Notifications.Notification
@valid_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
def notification_fixture(attrs \\ %{}) do
{:ok, notification} =
attrs
|> Enum.into(@valid_attrs)
|> Notifications.create_notification()
notification
end
test "list_notifications/0 returns all notifications" do
notification = notification_fixture()
assert Notifications.list_notifications() == [notification]
end
test "get_notification!/1 returns the notification with given id" do
notification = notification_fixture()
assert Notifications.get_notification!(notification.id) == notification
end
test "create_notification/1 with valid data creates a notification" do
assert {:ok, %Notification{} = notification} = Notifications.create_notification(@valid_attrs)
end
test "create_notification/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Notifications.create_notification(@invalid_attrs)
end
test "update_notification/2 with valid data updates the notification" do
notification = notification_fixture()
assert {:ok, %Notification{} = notification} = Notifications.update_notification(notification, @update_attrs)
end
test "update_notification/2 with invalid data returns error changeset" do
notification = notification_fixture()
assert {:error, %Ecto.Changeset{}} = Notifications.update_notification(notification, @invalid_attrs)
assert notification == Notifications.get_notification!(notification.id)
end
test "delete_notification/1 deletes the notification" do
notification = notification_fixture()
assert {:ok, %Notification{}} = Notifications.delete_notification(notification)
assert_raise Ecto.NoResultsError, fn -> Notifications.get_notification!(notification.id) end
end
test "change_notification/1 returns a notification changeset" do
notification = notification_fixture()
assert %Ecto.Changeset{} = Notifications.change_notification(notification)
end
end
end