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

55 lines
1.5 KiB
Elixir
Raw Permalink Normal View History

2019-12-15 18:54:27 +01:00
defmodule PhilomenaWeb.Admin.ForumController do
use PhilomenaWeb, :controller
alias Philomena.Forums.Forum
alias Philomena.Forums
plug :verify_authorized
plug :load_resource, model: Forum, id_field: "short_name"
def index(conn, _params) do
2019-12-16 20:24:38 +01:00
render(conn, "index.html", title: "Admin - Forums")
2019-12-15 18:54:27 +01:00
end
def new(conn, _params) do
changeset = Forums.change_forum(%Forum{})
2019-12-16 20:24:38 +01:00
render(conn, "new.html", title: "New Forum", changeset: changeset)
2019-12-15 18:54:27 +01:00
end
def create(conn, %{"forum" => forum_params}) do
case Forums.create_forum(forum_params) do
2019-12-15 21:02:13 +01:00
{:ok, _forum} ->
2019-12-15 18:54:27 +01:00
conn
|> put_flash(:info, "Forum created successfully.")
|> redirect(to: ~p"/admin/forums")
2019-12-15 18:54:27 +01:00
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def edit(conn, _params) do
changeset = Forums.change_forum(conn.assigns.forum)
2019-12-16 20:24:38 +01:00
render(conn, "edit.html", title: "Editing Forum", changeset: changeset)
2019-12-15 18:54:27 +01:00
end
def update(conn, %{"forum" => forum_params}) do
case Forums.update_forum(conn.assigns.forum, forum_params) do
2019-12-15 21:02:13 +01:00
{:ok, _forum} ->
2019-12-15 18:54:27 +01:00
conn
|> put_flash(:info, "Forum updated successfully.")
|> redirect(to: ~p"/admin/forums")
2019-12-15 18:54:27 +01:00
{:error, changeset} ->
render(conn, "edit.html", changeset: changeset)
end
end
defp verify_authorized(conn, _opts) do
case Canada.Can.can?(conn.assigns.current_user, :edit, Forum) do
2020-01-11 05:20:19 +01:00
true -> conn
2019-12-15 18:54:27 +01:00
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end
end
end