From 38cc279d9737ea8e0eedc3cecbe7066da10f057c Mon Sep 17 00:00:00 2001 From: "Liam P. White" Date: Wed, 28 Aug 2019 13:23:05 -0400 Subject: [PATCH] context: mod_notes --- lib/philomena/mod_notes.ex | 104 ++++++++++++++++++++++++++++ lib/philomena/mod_notes/mod_note.ex | 24 +++++++ test/philomena/mod_notes_test.exs | 62 +++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 lib/philomena/mod_notes.ex create mode 100644 lib/philomena/mod_notes/mod_note.ex create mode 100644 test/philomena/mod_notes_test.exs diff --git a/lib/philomena/mod_notes.ex b/lib/philomena/mod_notes.ex new file mode 100644 index 00000000..f74c9d8f --- /dev/null +++ b/lib/philomena/mod_notes.ex @@ -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 diff --git a/lib/philomena/mod_notes/mod_note.ex b/lib/philomena/mod_notes/mod_note.ex new file mode 100644 index 00000000..fd344947 --- /dev/null +++ b/lib/philomena/mod_notes/mod_note.ex @@ -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 diff --git a/test/philomena/mod_notes_test.exs b/test/philomena/mod_notes_test.exs new file mode 100644 index 00000000..53483f4a --- /dev/null +++ b/test/philomena/mod_notes_test.exs @@ -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