From ff6553e875fd0292dd4c9392a3132e5e3a17bdd4 Mon Sep 17 00:00:00 2001 From: "Liam P. White" Date: Wed, 28 Aug 2019 12:57:06 -0400 Subject: [PATCH] context: duplicate_reports --- config/config.exs | 3 + lib/philomena/duplicate_reports.ex | 104 ++++++++++++++++++ .../duplicate_reports/duplicate_report.ex | 23 ++++ test/philomena/duplicate_reports_test.exs | 62 +++++++++++ 4 files changed, 192 insertions(+) create mode 100644 lib/philomena/duplicate_reports.ex create mode 100644 lib/philomena/duplicate_reports/duplicate_report.ex create mode 100644 test/philomena/duplicate_reports_test.exs diff --git a/config/config.exs b/config/config.exs index e8268ab5..dba99a01 100644 --- a/config/config.exs +++ b/config/config.exs @@ -32,6 +32,9 @@ config :philomena, PhilomenaWeb.Endpoint, render_errors: [view: PhilomenaWeb.ErrorView, accepts: ~w(html json)], pubsub: [name: Philomena.PubSub, adapter: Phoenix.PubSub.PG2] +config :philomena, :generators, + migration: false + config :phoenix, :template_engines, slim: PhoenixSlime.Engine, slime: PhoenixSlime.Engine, diff --git a/lib/philomena/duplicate_reports.ex b/lib/philomena/duplicate_reports.ex new file mode 100644 index 00000000..56a9b38e --- /dev/null +++ b/lib/philomena/duplicate_reports.ex @@ -0,0 +1,104 @@ +defmodule Philomena.DuplicateReports do + @moduledoc """ + The DuplicateReports context. + """ + + import Ecto.Query, warn: false + alias Philomena.Repo + + alias Philomena.DuplicateReports.DuplicateReport + + @doc """ + Returns the list of duplicate_reports. + + ## Examples + + iex> list_duplicate_reports() + [%DuplicateReport{}, ...] + + """ + def list_duplicate_reports do + Repo.all(DuplicateReport) + end + + @doc """ + Gets a single duplicate_report. + + Raises `Ecto.NoResultsError` if the Duplicate report does not exist. + + ## Examples + + iex> get_duplicate_report!(123) + %DuplicateReport{} + + iex> get_duplicate_report!(456) + ** (Ecto.NoResultsError) + + """ + def get_duplicate_report!(id), do: Repo.get!(DuplicateReport, id) + + @doc """ + Creates a duplicate_report. + + ## Examples + + iex> create_duplicate_report(%{field: value}) + {:ok, %DuplicateReport{}} + + iex> create_duplicate_report(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_duplicate_report(attrs \\ %{}) do + %DuplicateReport{} + |> DuplicateReport.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a duplicate_report. + + ## Examples + + iex> update_duplicate_report(duplicate_report, %{field: new_value}) + {:ok, %DuplicateReport{}} + + iex> update_duplicate_report(duplicate_report, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_duplicate_report(%DuplicateReport{} = duplicate_report, attrs) do + duplicate_report + |> DuplicateReport.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a DuplicateReport. + + ## Examples + + iex> delete_duplicate_report(duplicate_report) + {:ok, %DuplicateReport{}} + + iex> delete_duplicate_report(duplicate_report) + {:error, %Ecto.Changeset{}} + + """ + def delete_duplicate_report(%DuplicateReport{} = duplicate_report) do + Repo.delete(duplicate_report) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking duplicate_report changes. + + ## Examples + + iex> change_duplicate_report(duplicate_report) + %Ecto.Changeset{source: %DuplicateReport{}} + + """ + def change_duplicate_report(%DuplicateReport{} = duplicate_report) do + DuplicateReport.changeset(duplicate_report, %{}) + end +end diff --git a/lib/philomena/duplicate_reports/duplicate_report.ex b/lib/philomena/duplicate_reports/duplicate_report.ex new file mode 100644 index 00000000..9dc0f7be --- /dev/null +++ b/lib/philomena/duplicate_reports/duplicate_report.ex @@ -0,0 +1,23 @@ +defmodule Philomena.DuplicateReports.DuplicateReport do + use Ecto.Schema + import Ecto.Changeset + + schema "duplicate_reports" do + belongs_to :image, Philomena.Images.Image + belongs_to :duplicate_of_image, Philomena.Images.Image + belongs_to :user, Philomena.Users.User + belongs_to :modifier, Philomena.Users.User + + field :reason, :string + field :state, :string, default: "open" + + timestamps(inserted_at: :created_at) + end + + @doc false + def changeset(duplicate_report, attrs) do + duplicate_report + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/test/philomena/duplicate_reports_test.exs b/test/philomena/duplicate_reports_test.exs new file mode 100644 index 00000000..a443952e --- /dev/null +++ b/test/philomena/duplicate_reports_test.exs @@ -0,0 +1,62 @@ +defmodule Philomena.DuplicateReportsTest do + use Philomena.DataCase + + alias Philomena.DuplicateReports + + describe "duplicate_reports" do + alias Philomena.DuplicateReports.DuplicateReport + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def duplicate_report_fixture(attrs \\ %{}) do + {:ok, duplicate_report} = + attrs + |> Enum.into(@valid_attrs) + |> DuplicateReports.create_duplicate_report() + + duplicate_report + end + + test "list_duplicate_reports/0 returns all duplicate_reports" do + duplicate_report = duplicate_report_fixture() + assert DuplicateReports.list_duplicate_reports() == [duplicate_report] + end + + test "get_duplicate_report!/1 returns the duplicate_report with given id" do + duplicate_report = duplicate_report_fixture() + assert DuplicateReports.get_duplicate_report!(duplicate_report.id) == duplicate_report + end + + test "create_duplicate_report/1 with valid data creates a duplicate_report" do + assert {:ok, %DuplicateReport{} = duplicate_report} = DuplicateReports.create_duplicate_report(@valid_attrs) + end + + test "create_duplicate_report/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = DuplicateReports.create_duplicate_report(@invalid_attrs) + end + + test "update_duplicate_report/2 with valid data updates the duplicate_report" do + duplicate_report = duplicate_report_fixture() + assert {:ok, %DuplicateReport{} = duplicate_report} = DuplicateReports.update_duplicate_report(duplicate_report, @update_attrs) + end + + test "update_duplicate_report/2 with invalid data returns error changeset" do + duplicate_report = duplicate_report_fixture() + assert {:error, %Ecto.Changeset{}} = DuplicateReports.update_duplicate_report(duplicate_report, @invalid_attrs) + assert duplicate_report == DuplicateReports.get_duplicate_report!(duplicate_report.id) + end + + test "delete_duplicate_report/1 deletes the duplicate_report" do + duplicate_report = duplicate_report_fixture() + assert {:ok, %DuplicateReport{}} = DuplicateReports.delete_duplicate_report(duplicate_report) + assert_raise Ecto.NoResultsError, fn -> DuplicateReports.get_duplicate_report!(duplicate_report.id) end + end + + test "change_duplicate_report/1 returns a duplicate_report changeset" do + duplicate_report = duplicate_report_fixture() + assert %Ecto.Changeset{} = DuplicateReports.change_duplicate_report(duplicate_report) + end + end +end