2019-12-04 15:28:17 +01:00
|
|
|
defmodule PhilomenaWeb.Profile.CommissionController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-12-04 16:00:41 +01:00
|
|
|
alias Philomena.Commissions.Commission
|
|
|
|
alias Philomena.Commissions
|
2021-09-29 22:24:38 +02:00
|
|
|
alias PhilomenaWeb.MarkdownRenderer
|
2019-12-04 15:28:17 +01:00
|
|
|
alias Philomena.Users.User
|
|
|
|
|
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create, :edit, :update, :delete]
|
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 16:00:41 +01:00
|
|
|
plug :ensure_commission when action in [:show, :edit, :update, :delete]
|
|
|
|
plug :ensure_no_commission when action in [:new, :create]
|
|
|
|
plug :ensure_correct_user when action in [:new, :create, :edit, :update, :delete]
|
|
|
|
plug :ensure_links_verified when action in [:new, :create, :edit, :update, :delete]
|
2019-12-04 15:28:17 +01:00
|
|
|
|
|
|
|
def show(conn, _params) do
|
|
|
|
commission = conn.assigns.user.commission
|
|
|
|
|
2020-01-02 03:35:37 +01:00
|
|
|
items =
|
2019-12-04 15:28:17 +01:00
|
|
|
commission.items
|
2020-10-26 22:01:29 +01:00
|
|
|
|> Enum.sort(&(Decimal.compare(&1.base_price, &2.base_price) != :gt))
|
2020-01-02 03:35:37 +01:00
|
|
|
|
|
|
|
item_descriptions =
|
|
|
|
items
|
2021-09-29 22:05:16 +02:00
|
|
|
|> Enum.map(&%{body: &1.description})
|
2021-09-29 22:24:38 +02:00
|
|
|
|> MarkdownRenderer.render_collection(conn)
|
2019-12-04 15:28:17 +01:00
|
|
|
|
|
|
|
item_add_ons =
|
2020-01-02 03:35:37 +01:00
|
|
|
items
|
2021-09-29 22:05:16 +02:00
|
|
|
|> Enum.map(&%{body: &1.add_ons})
|
2021-09-29 22:24:38 +02:00
|
|
|
|> MarkdownRenderer.render_collection(conn)
|
2019-12-04 15:28:17 +01:00
|
|
|
|
|
|
|
[information, contact, will_create, will_not_create] =
|
2021-09-29 22:24:38 +02:00
|
|
|
MarkdownRenderer.render_collection(
|
2019-12-04 15:28:17 +01:00
|
|
|
[
|
2021-09-29 22:05:16 +02:00
|
|
|
%{body: commission.information || ""},
|
|
|
|
%{body: commission.contact || ""},
|
|
|
|
%{body: commission.will_create || ""},
|
|
|
|
%{body: commission.will_not_create || ""}
|
2019-12-04 15:28:17 +01:00
|
|
|
],
|
|
|
|
conn
|
|
|
|
)
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
rendered = %{
|
|
|
|
information: information,
|
|
|
|
contact: contact,
|
|
|
|
will_create: will_create,
|
|
|
|
will_not_create: will_not_create
|
|
|
|
}
|
2019-12-04 15:28:17 +01:00
|
|
|
|
2020-01-02 03:35:37 +01:00
|
|
|
items = Enum.zip([item_descriptions, item_add_ons, items])
|
2019-12-04 15:28:17 +01:00
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
render(conn, "show.html",
|
|
|
|
title: "Showing Commission",
|
|
|
|
rendered: rendered,
|
|
|
|
commission: commission,
|
|
|
|
items: items,
|
|
|
|
layout_class: "layout--wide"
|
|
|
|
)
|
2019-12-04 15:28:17 +01:00
|
|
|
end
|
|
|
|
|
2019-12-04 16:00:41 +01:00
|
|
|
def new(conn, _params) do
|
|
|
|
changeset = Commissions.change_commission(%Commission{})
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "new.html", title: "New Commission", changeset: changeset)
|
2019-12-04 16:00:41 +01:00
|
|
|
end
|
|
|
|
|
2019-12-04 17:35:06 +01:00
|
|
|
def create(conn, %{"commission" => commission_params}) do
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
case Commissions.create_commission(user, commission_params) do
|
|
|
|
{:ok, _commission} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Commission successfully created.")
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/profiles/#{user}/commission")
|
2019-12-04 17:35:06 +01:00
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
render(conn, "new.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit(conn, _params) do
|
|
|
|
changeset = Commissions.change_commission(conn.assigns.user.commission)
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "edit.html", title: "Editing Commission", changeset: changeset)
|
2019-12-04 17:35:06 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"commission" => commission_params}) do
|
|
|
|
commission = conn.assigns.user.commission
|
|
|
|
|
|
|
|
case Commissions.update_commission(commission, commission_params) do
|
|
|
|
{:ok, _commission} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Commission successfully updated.")
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/profiles/#{conn.assigns.user}/commission")
|
2019-12-04 17:35:06 +01:00
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
render(conn, "edit.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, _params) do
|
|
|
|
commission = conn.assigns.user.commission
|
|
|
|
|
|
|
|
{:ok, _commission} = Commissions.delete_commission(commission)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Commission deleted successfully.")
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/commissions")
|
2019-12-04 17:35:06 +01:00
|
|
|
end
|
|
|
|
|
2019-12-04 15:28:17 +01:00
|
|
|
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 15:28:17 +01:00
|
|
|
false -> conn
|
|
|
|
end
|
|
|
|
end
|
2019-12-04 16:00:41 +01:00
|
|
|
|
|
|
|
defp ensure_no_commission(conn, _opts) do
|
|
|
|
case is_nil(conn.assigns.user.commission) do
|
|
|
|
false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
2020-01-11 05:20:19 +01:00
|
|
|
true -> conn
|
2019-12-04 16:00:41 +01:00
|
|
|
end
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-04 16:00:41 +01:00
|
|
|
defp ensure_correct_user(conn, _opts) do
|
|
|
|
user_id = conn.assigns.user.id
|
|
|
|
|
|
|
|
case conn.assigns.current_user do
|
|
|
|
%{id: ^user_id} -> conn
|
2020-09-15 05:39:58 +02:00
|
|
|
%{role: role} when role in ["admin", "moderator"] -> conn
|
2020-01-11 05:20:19 +01:00
|
|
|
_other -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
2019-12-04 16:00:41 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp ensure_links_verified(conn, _opts) do
|
|
|
|
case Enum.any?(conn.assigns.user.verified_links) do
|
2020-01-11 05:20:19 +01:00
|
|
|
true ->
|
|
|
|
conn
|
|
|
|
|
2019-12-04 16:00:41 +01:00
|
|
|
false ->
|
|
|
|
conn
|
2020-12-02 20:18:25 +01:00
|
|
|
|> put_flash(
|
2023-11-23 17:07:49 +01:00
|
|
|
:warning,
|
2020-12-02 20:18:25 +01:00
|
|
|
"You must have a verified artist link to create a commission listing."
|
|
|
|
)
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/commissions")
|
2020-05-04 09:18:36 +02:00
|
|
|
|> halt()
|
2019-12-04 16:00:41 +01:00
|
|
|
end
|
|
|
|
end
|
2019-12-16 20:24:38 +01:00
|
|
|
end
|