philomena/lib/philomena_web/controllers/duplicate_report_controller.ex

59 lines
2.1 KiB
Elixir
Raw Normal View History

2019-12-01 05:51:44 +01:00
defmodule PhilomenaWeb.DuplicateReportController do
use PhilomenaWeb, :controller
alias Philomena.DuplicateReports
alias Philomena.DuplicateReports.DuplicateReport
alias Philomena.Images.Image
alias Philomena.Repo
import Ecto.Query
@valid_states ~W(open rejected accepted claimed)
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:create]
2019-12-07 01:11:25 +01:00
plug PhilomenaWeb.UserAttributionPlug when action in [:create]
2019-12-01 05:58:32 +01:00
plug :load_resource, model: DuplicateReport, only: [:show], preload: [:image, :duplicate_of_image]
2019-12-01 05:51:44 +01:00
def index(conn, params) do
states =
2019-12-01 05:58:32 +01:00
(params["states"] || ~W(open claimed))
2019-12-01 05:51:44 +01:00
|> wrap()
|> Enum.filter(&Enum.member?(@valid_states, &1))
duplicate_reports =
DuplicateReport
|> where([d], d.state in ^states)
2019-12-09 05:41:35 +01:00
|> preload([:user, :modifier, image: [:user, :tags], duplicate_of_image: [:user, :tags]])
2019-12-01 05:51:44 +01:00
|> order_by(desc: :created_at)
|> Repo.paginate(conn.assigns.pagination)
2019-12-16 20:24:38 +01:00
render(conn, "index.html", title: "Duplicate Reports", duplicate_reports: duplicate_reports, layout_class: "layout--wide")
2019-12-01 05:51:44 +01:00
end
def create(conn, %{"duplicate_report" => duplicate_report_params}) do
2019-12-07 01:11:25 +01:00
attributes = conn.assigns.attributes
2019-12-01 05:51:44 +01:00
source = Repo.get!(Image, duplicate_report_params["image_id"])
target = Repo.get!(Image, duplicate_report_params["duplicate_of_image_id"])
2019-12-07 01:11:25 +01:00
case DuplicateReports.create_duplicate_report(source, target, attributes, duplicate_report_params) do
2019-12-01 05:51:44 +01:00
{:ok, duplicate_report} ->
conn
|> put_flash(:info, "Duplicate report created successfully.")
|> redirect(to: Routes.image_path(conn, :show, duplicate_report.image_id))
{:error, _changeset} ->
conn
|> put_flash(:error, "Failed to submit duplicate report")
|> redirect(external: conn.assigns.referrer)
end
end
def show(conn, _params) do
dr = conn.assigns.duplicate_report
2019-12-16 20:24:38 +01:00
render(conn, "show.html", title: "Showing Duplicate Report", duplicate_report: dr, layout_class: "layout--wide")
2019-12-01 05:51:44 +01:00
end
defp wrap(list) when is_list(list), do: list
defp wrap(not_a_list), do: [not_a_list]
end