context: mod_notes

This commit is contained in:
Liam P. White 2019-08-28 13:23:05 -04:00
parent 479783cb7d
commit 38cc279d97
3 changed files with 190 additions and 0 deletions

104
lib/philomena/mod_notes.ex Normal file
View file

@ -0,0 +1,104 @@
defmodule Philomena.ModNotes do
@moduledoc """
The ModNotes context.
"""
import Ecto.Query, warn: false
alias Philomena.Repo
alias Philomena.ModNotes.ModNote
@doc """
Returns the list of mod_notes.
## Examples
iex> list_mod_notes()
[%ModNote{}, ...]
"""
def list_mod_notes do
Repo.all(ModNote)
end
@doc """
Gets a single mod_note.
Raises `Ecto.NoResultsError` if the Mod note does not exist.
## Examples
iex> get_mod_note!(123)
%ModNote{}
iex> get_mod_note!(456)
** (Ecto.NoResultsError)
"""
def get_mod_note!(id), do: Repo.get!(ModNote, id)
@doc """
Creates a mod_note.
## Examples
iex> create_mod_note(%{field: value})
{:ok, %ModNote{}}
iex> create_mod_note(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_mod_note(attrs \\ %{}) do
%ModNote{}
|> ModNote.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a mod_note.
## Examples
iex> update_mod_note(mod_note, %{field: new_value})
{:ok, %ModNote{}}
iex> update_mod_note(mod_note, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_mod_note(%ModNote{} = mod_note, attrs) do
mod_note
|> ModNote.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a ModNote.
## Examples
iex> delete_mod_note(mod_note)
{:ok, %ModNote{}}
iex> delete_mod_note(mod_note)
{:error, %Ecto.Changeset{}}
"""
def delete_mod_note(%ModNote{} = mod_note) do
Repo.delete(mod_note)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking mod_note changes.
## Examples
iex> change_mod_note(mod_note)
%Ecto.Changeset{source: %ModNote{}}
"""
def change_mod_note(%ModNote{} = mod_note) do
ModNote.changeset(mod_note, %{})
end
end

View file

@ -0,0 +1,24 @@
defmodule Philomena.ModNotes.ModNote do
use Ecto.Schema
import Ecto.Changeset
schema "mod_notes" do
belongs_to :moderator, Philomena.Users.User
# fixme: rails polymorphic relation
field :notable_id, :integer
field :notable_type, :string
field :body, :string
field :deleted, :boolean, default: false
timestamps(inserted_at: :created_at)
end
@doc false
def changeset(mod_note, attrs) do
mod_note
|> cast(attrs, [])
|> validate_required([])
end
end

View file

@ -0,0 +1,62 @@
defmodule Philomena.ModNotesTest do
use Philomena.DataCase
alias Philomena.ModNotes
describe "mod_notes" do
alias Philomena.ModNotes.ModNote
@valid_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
def mod_note_fixture(attrs \\ %{}) do
{:ok, mod_note} =
attrs
|> Enum.into(@valid_attrs)
|> ModNotes.create_mod_note()
mod_note
end
test "list_mod_notes/0 returns all mod_notes" do
mod_note = mod_note_fixture()
assert ModNotes.list_mod_notes() == [mod_note]
end
test "get_mod_note!/1 returns the mod_note with given id" do
mod_note = mod_note_fixture()
assert ModNotes.get_mod_note!(mod_note.id) == mod_note
end
test "create_mod_note/1 with valid data creates a mod_note" do
assert {:ok, %ModNote{} = mod_note} = ModNotes.create_mod_note(@valid_attrs)
end
test "create_mod_note/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = ModNotes.create_mod_note(@invalid_attrs)
end
test "update_mod_note/2 with valid data updates the mod_note" do
mod_note = mod_note_fixture()
assert {:ok, %ModNote{} = mod_note} = ModNotes.update_mod_note(mod_note, @update_attrs)
end
test "update_mod_note/2 with invalid data returns error changeset" do
mod_note = mod_note_fixture()
assert {:error, %Ecto.Changeset{}} = ModNotes.update_mod_note(mod_note, @invalid_attrs)
assert mod_note == ModNotes.get_mod_note!(mod_note.id)
end
test "delete_mod_note/1 deletes the mod_note" do
mod_note = mod_note_fixture()
assert {:ok, %ModNote{}} = ModNotes.delete_mod_note(mod_note)
assert_raise Ecto.NoResultsError, fn -> ModNotes.get_mod_note!(mod_note.id) end
end
test "change_mod_note/1 returns a mod_note changeset" do
mod_note = mod_note_fixture()
assert %Ecto.Changeset{} = ModNotes.change_mod_note(mod_note)
end
end
end