philomena/lib/philomena_web/controllers/admin/badge_controller.ex

62 lines
1.8 KiB
Elixir
Raw Normal View History

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
{:ok, %{badge: _badge}} ->
conn
|> put_flash(:info, "Badge created successfully.")
|> redirect(to: Routes.admin_badge_path(conn, :index))
{: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
{:ok, %{badge: _badge}} ->
conn
|> put_flash(:info, "Badge updated successfully.")
|> redirect(to: Routes.admin_badge_path(conn, :index))
{: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