context: site_notices

This commit is contained in:
Liam P. White 2019-08-28 13:36:25 -04:00
parent 282957bef5
commit 98582aeb5f
3 changed files with 191 additions and 0 deletions

View file

@ -0,0 +1,104 @@
defmodule Philomena.SiteNotices do
@moduledoc """
The SiteNotices context.
"""
import Ecto.Query, warn: false
alias Philomena.Repo
alias Philomena.SiteNotices.SiteNotice
@doc """
Returns the list of site_notices.
## Examples
iex> list_site_notices()
[%SiteNotice{}, ...]
"""
def list_site_notices do
Repo.all(SiteNotice)
end
@doc """
Gets a single site_notice.
Raises `Ecto.NoResultsError` if the Site notice does not exist.
## Examples
iex> get_site_notice!(123)
%SiteNotice{}
iex> get_site_notice!(456)
** (Ecto.NoResultsError)
"""
def get_site_notice!(id), do: Repo.get!(SiteNotice, id)
@doc """
Creates a site_notice.
## Examples
iex> create_site_notice(%{field: value})
{:ok, %SiteNotice{}}
iex> create_site_notice(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_site_notice(attrs \\ %{}) do
%SiteNotice{}
|> SiteNotice.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a site_notice.
## Examples
iex> update_site_notice(site_notice, %{field: new_value})
{:ok, %SiteNotice{}}
iex> update_site_notice(site_notice, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_site_notice(%SiteNotice{} = site_notice, attrs) do
site_notice
|> SiteNotice.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a SiteNotice.
## Examples
iex> delete_site_notice(site_notice)
{:ok, %SiteNotice{}}
iex> delete_site_notice(site_notice)
{:error, %Ecto.Changeset{}}
"""
def delete_site_notice(%SiteNotice{} = site_notice) do
Repo.delete(site_notice)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking site_notice changes.
## Examples
iex> change_site_notice(site_notice)
%Ecto.Changeset{source: %SiteNotice{}}
"""
def change_site_notice(%SiteNotice{} = site_notice) do
SiteNotice.changeset(site_notice, %{})
end
end

View file

@ -0,0 +1,25 @@
defmodule Philomena.SiteNotices.SiteNotice do
use Ecto.Schema
import Ecto.Changeset
schema "site_notices" do
belongs_to :user, Philomena.Users.User
field :title, :string
field :text, :string
field :link, :string
field :link_text, :string
field :live, :boolean, default: false
field :start_date, :naive_datetime
field :finish_date, :naive_datetime
timestamps(inserted_at: :created_At)
end
@doc false
def changeset(site_notice, attrs) do
site_notice
|> cast(attrs, [])
|> validate_required([])
end
end

View file

@ -0,0 +1,62 @@
defmodule Philomena.SiteNoticesTest do
use Philomena.DataCase
alias Philomena.SiteNotices
describe "site_notices" do
alias Philomena.SiteNotices.SiteNotice
@valid_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
def site_notice_fixture(attrs \\ %{}) do
{:ok, site_notice} =
attrs
|> Enum.into(@valid_attrs)
|> SiteNotices.create_site_notice()
site_notice
end
test "list_site_notices/0 returns all site_notices" do
site_notice = site_notice_fixture()
assert SiteNotices.list_site_notices() == [site_notice]
end
test "get_site_notice!/1 returns the site_notice with given id" do
site_notice = site_notice_fixture()
assert SiteNotices.get_site_notice!(site_notice.id) == site_notice
end
test "create_site_notice/1 with valid data creates a site_notice" do
assert {:ok, %SiteNotice{} = site_notice} = SiteNotices.create_site_notice(@valid_attrs)
end
test "create_site_notice/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = SiteNotices.create_site_notice(@invalid_attrs)
end
test "update_site_notice/2 with valid data updates the site_notice" do
site_notice = site_notice_fixture()
assert {:ok, %SiteNotice{} = site_notice} = SiteNotices.update_site_notice(site_notice, @update_attrs)
end
test "update_site_notice/2 with invalid data returns error changeset" do
site_notice = site_notice_fixture()
assert {:error, %Ecto.Changeset{}} = SiteNotices.update_site_notice(site_notice, @invalid_attrs)
assert site_notice == SiteNotices.get_site_notice!(site_notice.id)
end
test "delete_site_notice/1 deletes the site_notice" do
site_notice = site_notice_fixture()
assert {:ok, %SiteNotice{}} = SiteNotices.delete_site_notice(site_notice)
assert_raise Ecto.NoResultsError, fn -> SiteNotices.get_site_notice!(site_notice.id) end
end
test "change_site_notice/1 returns a site_notice changeset" do
site_notice = site_notice_fixture()
assert %Ecto.Changeset{} = SiteNotices.change_site_notice(site_notice)
end
end
end