philomena/lib/philomena_web/controllers/profile/award_controller.ex

75 lines
2.2 KiB
Elixir
Raw Permalink Normal View History

2019-12-15 21:02:13 +01:00
defmodule PhilomenaWeb.Profile.AwardController do
use PhilomenaWeb, :controller
alias Philomena.Badges.Award
alias Philomena.Badges.Badge
alias Philomena.Users.User
alias Philomena.Badges
alias Philomena.Repo
import Ecto.Query
plug :verify_authorized
plug :load_resource, model: User, id_name: "profile_id", id_field: "slug", persisted: true
plug :load_resource, model: Award, only: [:edit, :update, :delete]
plug :load_badges when action in [:new, :create, :edit, :update]
def new(conn, _params) do
changeset = Badges.change_badge_award(%Award{})
2019-12-16 20:24:38 +01:00
render(conn, "new.html", title: "New Award", changeset: changeset)
2019-12-15 21:02:13 +01:00
end
def create(conn, %{"award" => award_params}) do
case Badges.create_badge_award(conn.assigns.current_user, conn.assigns.user, award_params) do
{:ok, _award} ->
conn
|> put_flash(:info, "Award successfully created.")
|> redirect(to: ~p"/profiles/#{conn.assigns.user}")
2019-12-15 21:02:13 +01:00
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def edit(conn, _params) do
changeset = Badges.change_badge_award(conn.assigns.award)
2019-12-16 20:24:38 +01:00
render(conn, "edit.html", title: "Editing Award", changeset: changeset)
2019-12-15 21:02:13 +01:00
end
def update(conn, %{"award" => award_params}) do
case Badges.update_badge_award(conn.assigns.award, award_params) do
{:ok, _award} ->
conn
|> put_flash(:info, "Award successfully updated.")
|> redirect(to: ~p"/profiles/#{conn.assigns.user}")
2019-12-15 21:02:13 +01:00
{:error, changeset} ->
render(conn, "edit.html", changeset: changeset)
end
end
def delete(conn, _params) do
{:ok, _award} = Badges.delete_badge_award(conn.assigns.award)
conn
|> put_flash(:info, "Award successfully destroyed. By cruel and unusual means.")
|> redirect(to: ~p"/profiles/#{conn.assigns.user}")
2019-12-15 21:02:13 +01:00
end
defp verify_authorized(conn, _opts) do
case Canada.Can.can?(conn.assigns.current_user, :create, Award) 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
defp load_badges(conn, _opts) do
badges =
Badge
|> where(disable_award: false)
|> order_by(asc: :title)
|> Repo.all()
assign(conn, :badges, badges)
end
end