2019-12-04 18:14:41 +01:00
|
|
|
defmodule PhilomenaWeb.Profile.Commission.ItemController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Commissions.Item
|
|
|
|
alias Philomena.Commissions
|
|
|
|
alias Philomena.Users.User
|
|
|
|
alias Philomena.Repo
|
|
|
|
|
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
plug :load_resource,
|
|
|
|
model: User,
|
|
|
|
id_name: "profile_id",
|
|
|
|
id_field: "slug",
|
|
|
|
preload: [
|
|
|
|
:verified_links,
|
2020-12-06 16:08:13 +01:00
|
|
|
commission: [
|
2023-05-29 13:06:41 +02:00
|
|
|
sheet_image: [:sources, tags: :aliases],
|
2020-12-06 16:08:13 +01:00
|
|
|
user: [awards: :badge],
|
2023-05-29 13:06:41 +02:00
|
|
|
items: [example_image: [:sources, tags: :aliases]]
|
2020-12-06 16:08:13 +01:00
|
|
|
]
|
2020-01-11 05:20:19 +01:00
|
|
|
],
|
|
|
|
persisted: true
|
|
|
|
|
2019-12-04 18:14:41 +01:00
|
|
|
plug :ensure_commission
|
|
|
|
plug :ensure_correct_user
|
|
|
|
|
|
|
|
def new(conn, _params) do
|
|
|
|
user = conn.assigns.user
|
|
|
|
commission = user.commission
|
|
|
|
|
|
|
|
changeset = Commissions.change_item(%Item{})
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
render(conn, "new.html",
|
|
|
|
title: "New Commission Item",
|
|
|
|
user: user,
|
|
|
|
commission: commission,
|
|
|
|
changeset: changeset
|
|
|
|
)
|
2019-12-04 18:14:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"item" => item_params}) do
|
|
|
|
user = conn.assigns.user
|
|
|
|
commission = user.commission
|
|
|
|
|
|
|
|
case Commissions.create_item(commission, item_params) do
|
2020-03-26 18:18:42 +01:00
|
|
|
{:ok, _multi} ->
|
2019-12-04 18:14:41 +01:00
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Item successfully created.")
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/profiles/#{conn.assigns.user}/commission")
|
2019-12-04 18:14:41 +01:00
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
render(conn, "new.html", user: user, commission: commission, changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit(conn, %{"id" => id}) do
|
|
|
|
user = conn.assigns.user
|
|
|
|
commission = user.commission
|
|
|
|
item = Repo.get_by!(Item, commission_id: commission.id, id: id)
|
|
|
|
|
|
|
|
changeset = Commissions.change_item(item)
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
render(conn, "edit.html",
|
|
|
|
title: "Editing Commission Item",
|
|
|
|
user: user,
|
|
|
|
commission: commission,
|
|
|
|
item: item,
|
|
|
|
changeset: changeset
|
|
|
|
)
|
2019-12-04 18:14:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"id" => id, "item" => item_params}) do
|
|
|
|
user = conn.assigns.user
|
|
|
|
commission = user.commission
|
|
|
|
item = Repo.get_by!(Item, commission_id: commission.id, id: id)
|
|
|
|
|
|
|
|
case Commissions.update_item(item, item_params) do
|
|
|
|
{:ok, _commission} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Item successfully updated.")
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/profiles/#{conn.assigns.user}/commission")
|
2019-12-04 18:14:41 +01:00
|
|
|
|
|
|
|
{:error, changeset} ->
|
2020-01-11 05:20:19 +01:00
|
|
|
render(conn, "edit.html",
|
|
|
|
user: user,
|
|
|
|
commission: commission,
|
|
|
|
item: item,
|
|
|
|
changeset: changeset
|
|
|
|
)
|
2019-12-04 18:14:41 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, %{"id" => id}) do
|
|
|
|
user = conn.assigns.user
|
|
|
|
commission = user.commission
|
|
|
|
item = Repo.get_by!(Item, commission_id: commission.id, id: id)
|
|
|
|
|
2020-03-26 18:18:42 +01:00
|
|
|
{:ok, _multi} = Commissions.delete_item(item)
|
2019-12-04 18:14:41 +01:00
|
|
|
|
|
|
|
conn
|
2023-11-23 17:07:49 +01:00
|
|
|
|> put_flash(:info, "Item successfully deleted.")
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/profiles/#{conn.assigns.user}/commission")
|
2019-12-04 18:14:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp ensure_commission(conn, _opts) do
|
|
|
|
case is_nil(conn.assigns.user.commission) do
|
2020-01-11 05:20:19 +01:00
|
|
|
true -> PhilomenaWeb.NotFoundPlug.call(conn)
|
2019-12-04 18:14:41 +01:00
|
|
|
false -> conn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp ensure_correct_user(conn, _opts) do
|
|
|
|
user_id = conn.assigns.user.id
|
|
|
|
|
|
|
|
case conn.assigns.current_user do
|
|
|
|
%{id: ^user_id} -> conn
|
2020-01-11 05:20:19 +01:00
|
|
|
_other -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
2019-12-04 18:14:41 +01:00
|
|
|
end
|
|
|
|
end
|
2019-12-16 20:24:38 +01:00
|
|
|
end
|