2019-12-15 23:52:51 +01:00
|
|
|
defmodule PhilomenaWeb.Admin.ModNoteController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2021-09-29 22:24:38 +02:00
|
|
|
alias PhilomenaWeb.MarkdownRenderer
|
2019-12-15 23:52:51 +01:00
|
|
|
alias Philomena.ModNotes.ModNote
|
|
|
|
alias Philomena.Polymorphic
|
|
|
|
alias Philomena.ModNotes
|
|
|
|
|
|
|
|
plug :verify_authorized
|
|
|
|
plug :load_resource, model: ModNote, only: [:edit, :update, :delete]
|
|
|
|
plug :preload_association when action in [:edit, :update, :delete]
|
|
|
|
|
2024-07-16 02:57:39 +02:00
|
|
|
def index(conn, params) do
|
|
|
|
pagination = conn.assigns.scrivener
|
|
|
|
renderer = &MarkdownRenderer.render_collection(&1, conn)
|
2019-12-15 23:52:51 +01:00
|
|
|
|
|
|
|
mod_notes =
|
2024-07-16 02:57:39 +02:00
|
|
|
case params do
|
|
|
|
%{"q" => q} ->
|
|
|
|
ModNotes.list_mod_notes_by_query_string(q, renderer, pagination)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
ModNotes.list_mod_notes(renderer, pagination)
|
|
|
|
end
|
2019-12-15 23:52:51 +01:00
|
|
|
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "index.html", title: "Admin - Mod Notes", mod_notes: mod_notes)
|
2019-12-15 23:52:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def new(conn, %{"notable_type" => type, "notable_id" => id}) do
|
|
|
|
changeset = ModNotes.change_mod_note(%ModNote{notable_type: type, notable_id: id})
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "new.html", title: "New Mod Note", changeset: changeset)
|
2019-12-15 23:52:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"mod_note" => mod_note_params}) do
|
|
|
|
case ModNotes.create_mod_note(conn.assigns.current_user, mod_note_params) do
|
|
|
|
{:ok, _mod_note} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Successfully created mod note.")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/admin/mod_notes")
|
2019-12-15 23:52:51 +01:00
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
render(conn, "new.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit(conn, _params) do
|
|
|
|
changeset = ModNotes.change_mod_note(conn.assigns.mod_note)
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "edit.html", title: "Editing Mod Note", changeset: changeset)
|
2019-12-15 23:52:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"mod_note" => mod_note_params}) do
|
|
|
|
case ModNotes.update_mod_note(conn.assigns.mod_note, mod_note_params) do
|
|
|
|
{:ok, _mod_note} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Successfully updated mod note.")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/admin/mod_notes")
|
2019-12-15 23:52:51 +01:00
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
render(conn, "edit.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, _params) do
|
|
|
|
{:ok, _mod_note} = ModNotes.delete_mod_note(conn.assigns.mod_note)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Successfully deleted mod note.")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/admin/mod_notes")
|
2019-12-15 23:52:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp verify_authorized(conn, _opts) do
|
|
|
|
case Canada.Can.can?(conn.assigns.current_user, :index, ModNote) do
|
2020-01-11 05:20:19 +01:00
|
|
|
true -> conn
|
2019-12-15 23:52:51 +01:00
|
|
|
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def preload_association(%{assigns: %{mod_note: mod_note}} = conn, _opts) do
|
|
|
|
[mod_note] = Polymorphic.load_polymorphic([mod_note], notable: [notable_id: :notable_type])
|
|
|
|
|
|
|
|
assign(conn, :mod_note, mod_note)
|
|
|
|
end
|
|
|
|
end
|