diff --git a/lib/philomena/dnp_entries.ex b/lib/philomena/dnp_entries.ex new file mode 100644 index 00000000..3bbe940e --- /dev/null +++ b/lib/philomena/dnp_entries.ex @@ -0,0 +1,104 @@ +defmodule Philomena.DnpEntries do + @moduledoc """ + The DnpEntries context. + """ + + import Ecto.Query, warn: false + alias Philomena.Repo + + alias Philomena.DnpEntries.DnpEntry + + @doc """ + Returns the list of dnp_entries. + + ## Examples + + iex> list_dnp_entries() + [%DnpEntry{}, ...] + + """ + def list_dnp_entries do + Repo.all(DnpEntry) + end + + @doc """ + Gets a single dnp_entry. + + Raises `Ecto.NoResultsError` if the Dnp entry does not exist. + + ## Examples + + iex> get_dnp_entry!(123) + %DnpEntry{} + + iex> get_dnp_entry!(456) + ** (Ecto.NoResultsError) + + """ + def get_dnp_entry!(id), do: Repo.get!(DnpEntry, id) + + @doc """ + Creates a dnp_entry. + + ## Examples + + iex> create_dnp_entry(%{field: value}) + {:ok, %DnpEntry{}} + + iex> create_dnp_entry(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_dnp_entry(attrs \\ %{}) do + %DnpEntry{} + |> DnpEntry.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a dnp_entry. + + ## Examples + + iex> update_dnp_entry(dnp_entry, %{field: new_value}) + {:ok, %DnpEntry{}} + + iex> update_dnp_entry(dnp_entry, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_dnp_entry(%DnpEntry{} = dnp_entry, attrs) do + dnp_entry + |> DnpEntry.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a DnpEntry. + + ## Examples + + iex> delete_dnp_entry(dnp_entry) + {:ok, %DnpEntry{}} + + iex> delete_dnp_entry(dnp_entry) + {:error, %Ecto.Changeset{}} + + """ + def delete_dnp_entry(%DnpEntry{} = dnp_entry) do + Repo.delete(dnp_entry) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking dnp_entry changes. + + ## Examples + + iex> change_dnp_entry(dnp_entry) + %Ecto.Changeset{source: %DnpEntry{}} + + """ + def change_dnp_entry(%DnpEntry{} = dnp_entry) do + DnpEntry.changeset(dnp_entry, %{}) + end +end diff --git a/lib/philomena/dnp_entries/dnp_entry.ex b/lib/philomena/dnp_entries/dnp_entry.ex new file mode 100644 index 00000000..dfba0b9c --- /dev/null +++ b/lib/philomena/dnp_entries/dnp_entry.ex @@ -0,0 +1,27 @@ +defmodule Philomena.DnpEntries.DnpEntry do + use Ecto.Schema + import Ecto.Changeset + + schema "dnp_entries" do + belongs_to :requesting_user, Philomena.Users.User + belongs_to :modifying_user, Philomena.Users.User + belongs_to :tag, Philomena.Tags.Tag + + field :aasm_state, :string, default: "requested" + field :dnp_type, :string + field :conditions, :string + field :reason, :string + field :hide_reason, :boolean, default: false + field :instructions, :string + field :feedback, :string + + timestamps(inserted_at: :created_at) + end + + @doc false + def changeset(dnp_entry, attrs) do + dnp_entry + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/test/philomena/dnp_entries_test.exs b/test/philomena/dnp_entries_test.exs new file mode 100644 index 00000000..81899c95 --- /dev/null +++ b/test/philomena/dnp_entries_test.exs @@ -0,0 +1,62 @@ +defmodule Philomena.DnpEntriesTest do + use Philomena.DataCase + + alias Philomena.DnpEntries + + describe "dnp_entries" do + alias Philomena.DnpEntries.DnpEntry + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def dnp_entry_fixture(attrs \\ %{}) do + {:ok, dnp_entry} = + attrs + |> Enum.into(@valid_attrs) + |> DnpEntries.create_dnp_entry() + + dnp_entry + end + + test "list_dnp_entries/0 returns all dnp_entries" do + dnp_entry = dnp_entry_fixture() + assert DnpEntries.list_dnp_entries() == [dnp_entry] + end + + test "get_dnp_entry!/1 returns the dnp_entry with given id" do + dnp_entry = dnp_entry_fixture() + assert DnpEntries.get_dnp_entry!(dnp_entry.id) == dnp_entry + end + + test "create_dnp_entry/1 with valid data creates a dnp_entry" do + assert {:ok, %DnpEntry{} = dnp_entry} = DnpEntries.create_dnp_entry(@valid_attrs) + end + + test "create_dnp_entry/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = DnpEntries.create_dnp_entry(@invalid_attrs) + end + + test "update_dnp_entry/2 with valid data updates the dnp_entry" do + dnp_entry = dnp_entry_fixture() + assert {:ok, %DnpEntry{} = dnp_entry} = DnpEntries.update_dnp_entry(dnp_entry, @update_attrs) + end + + test "update_dnp_entry/2 with invalid data returns error changeset" do + dnp_entry = dnp_entry_fixture() + assert {:error, %Ecto.Changeset{}} = DnpEntries.update_dnp_entry(dnp_entry, @invalid_attrs) + assert dnp_entry == DnpEntries.get_dnp_entry!(dnp_entry.id) + end + + test "delete_dnp_entry/1 deletes the dnp_entry" do + dnp_entry = dnp_entry_fixture() + assert {:ok, %DnpEntry{}} = DnpEntries.delete_dnp_entry(dnp_entry) + assert_raise Ecto.NoResultsError, fn -> DnpEntries.get_dnp_entry!(dnp_entry.id) end + end + + test "change_dnp_entry/1 returns a dnp_entry changeset" do + dnp_entry = dnp_entry_fixture() + assert %Ecto.Changeset{} = DnpEntries.change_dnp_entry(dnp_entry) + end + end +end