2019-12-15 21:02:13 +01:00
|
|
|
defmodule PhilomenaWeb.Admin.BadgeController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Badges.Badge
|
|
|
|
alias Philomena.Badges
|
|
|
|
alias Philomena.Repo
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
plug :verify_authorized
|
|
|
|
plug :load_resource, model: Badge, only: [:edit, :update]
|
|
|
|
|
|
|
|
def index(conn, _params) do
|
|
|
|
badges =
|
|
|
|
Badge
|
|
|
|
|> order_by(asc: :title)
|
|
|
|
|> Repo.paginate(conn.assigns.scrivener)
|
|
|
|
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "index.html", title: "Admin - Badges", badges: badges)
|
2019-12-15 21:02:13 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def new(conn, _params) do
|
|
|
|
changeset = Badges.change_badge(%Badge{})
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "new.html", title: "New Badge", changeset: changeset)
|
2019-12-15 21:02:13 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"badge" => badge_params}) do
|
|
|
|
case Badges.create_badge(badge_params) do
|
2020-09-06 07:30:53 +02:00
|
|
|
{:ok, _badge} ->
|
2019-12-15 21:02:13 +01:00
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Badge created successfully.")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/admin/badges")
|
2019-12-15 21:02:13 +01:00
|
|
|
|
|
|
|
{:error, :badge, changeset, _changes} ->
|
|
|
|
render(conn, "new.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit(conn, _params) do
|
|
|
|
changeset = Badges.change_badge(conn.assigns.badge)
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "edit.html", title: "Editing Badge", changeset: changeset)
|
2019-12-15 21:02:13 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"badge" => badge_params}) do
|
|
|
|
case Badges.update_badge(conn.assigns.badge, badge_params) do
|
2020-03-30 00:36:24 +02:00
|
|
|
{:ok, _badge} ->
|
2019-12-15 21:02:13 +01:00
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Badge updated successfully.")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/admin/badges")
|
2019-12-15 21:02:13 +01:00
|
|
|
|
|
|
|
{:error, :badge, changeset, _changes} ->
|
|
|
|
render(conn, "edit.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp verify_authorized(conn, _opts) do
|
|
|
|
case Canada.Can.can?(conn.assigns.current_user, :index, Badge) do
|
2020-01-11 05:20:19 +01:00
|
|
|
true -> conn
|
2019-12-15 21:02:13 +01:00
|
|
|
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|