From 748086c01f3e6bae3e22cdc3b22c9d988de6fb16 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Wed, 4 Dec 2019 10:00:41 -0500 Subject: [PATCH] new commission endpoint --- .../controllers/commission_controller.ex | 16 ++++++++ .../profile/commission_controller.ex | 40 ++++++++++++++++++- .../commission/_directory_results.html.slime | 13 +++++- .../profile/commission/_form.html.slime | 34 ++++++++++++++++ .../commission/_listing_sidebar.html.slime | 2 +- .../profile/commission/new.html.slime | 5 +++ 6 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 lib/philomena_web/templates/profile/commission/_form.html.slime create mode 100644 lib/philomena_web/templates/profile/commission/new.html.slime diff --git a/lib/philomena_web/controllers/commission_controller.ex b/lib/philomena_web/controllers/commission_controller.ex index 33a7b785..9f326963 100644 --- a/lib/philomena_web/controllers/commission_controller.ex +++ b/lib/philomena_web/controllers/commission_controller.ex @@ -5,6 +5,8 @@ defmodule PhilomenaWeb.CommissionController do alias Philomena.Repo import Ecto.Query + plug :preload_commission + def index(conn, params) do commissions = commission_search(params["commission"]) @@ -80,4 +82,18 @@ defmodule PhilomenaWeb.CommissionController do defp like_sanitize(input) do "%" <> String.replace(input, ["\\", "%", "_"], &<<"\\", &1>>) <> "%" end + + defp preload_commission(conn, _opts) do + user = conn.assigns.current_user + + case user do + nil -> + conn + + user -> + user = Repo.preload(user, :commission) + config = Pow.Plug.fetch_config(conn) + Pow.Plug.assign_current_user(conn, user, config) + end + end end \ No newline at end of file diff --git a/lib/philomena_web/controllers/profile/commission_controller.ex b/lib/philomena_web/controllers/profile/commission_controller.ex index eac79f42..e097264e 100644 --- a/lib/philomena_web/controllers/profile/commission_controller.ex +++ b/lib/philomena_web/controllers/profile/commission_controller.ex @@ -1,12 +1,17 @@ defmodule PhilomenaWeb.Profile.CommissionController do use PhilomenaWeb, :controller + alias Philomena.Commissions.Commission + alias Philomena.Commissions alias Philomena.Textile.Renderer alias Philomena.Users.User plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create, :edit, :update, :delete] - plug :load_resource, model: User, id_name: "profile_id", id_field: "slug", preload: [commission: [sheet_image: :tags, user: [awards: :badge], items: [example_image: :tags]]], persisted: true - plug :ensure_commission + plug :load_resource, model: User, id_name: "profile_id", id_field: "slug", preload: [:verified_links, commission: [sheet_image: :tags, user: [awards: :badge], items: [example_image: :tags]]], persisted: true + 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] def show(conn, _params) do commission = conn.assigns.user.commission @@ -45,10 +50,41 @@ defmodule PhilomenaWeb.Profile.CommissionController do render(conn, "show.html", rendered: rendered, commission: commission, items: items, layout_class: "layout--wide") end + def new(conn, _params) do + changeset = Commissions.change_commission(%Commission{}) + render(conn, "new.html", changeset: changeset) + end + defp ensure_commission(conn, _opts) do case is_nil(conn.assigns.user.commission) do true -> PhilomenaWeb.NotFoundPlug.call(conn) false -> conn end end + + defp ensure_no_commission(conn, _opts) do + case is_nil(conn.assigns.user.commission) do + false -> PhilomenaWeb.NotAuthorizedPlug.call(conn) + true -> 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 + _other -> PhilomenaWeb.NotAuthorizedPlug.call(conn) + end + end + + defp ensure_links_verified(conn, _opts) do + case Enum.any?(conn.assigns.user.verified_links) do + true -> conn + false -> + conn + |> put_flash(:error, "You must have a verified user link to create a commission listing.") + |> redirect(to: Routes.commission_path(conn, :index)) + end + end end \ No newline at end of file diff --git a/lib/philomena_web/templates/commission/_directory_results.html.slime b/lib/philomena_web/templates/commission/_directory_results.html.slime index 0d1d3981..a52b1488 100644 --- a/lib/philomena_web/templates/commission/_directory_results.html.slime +++ b/lib/philomena_web/templates/commission/_directory_results.html.slime @@ -7,6 +7,15 @@ elixir: span.block__header__title Open Commissions = pagination + = cond do + - not is_nil(@conn.assigns.current_user) and not is_nil(@conn.assigns.current_user.commission) -> + = link "View my listing", to: Routes.profile_commission_path(@conn, :show, @conn.assigns.current_user) + + - not is_nil(@conn.assigns.current_user) -> + = link "Create my listing", to: Routes.profile_commission_path(@conn, :new, @conn.assigns.current_user) + + - true -> + .block__content = cond do - Enum.any?(@commissions) -> @@ -15,11 +24,11 @@ elixir: .block__content.flex.flex--no-wrap .flex__fixed.spacing-right = render PhilomenaWeb.UserAttributionView, "_user_avatar.html", object: c, conn: @conn - + .flex__grow.commission__listing_body span.commission__listing_artist = render PhilomenaWeb.UserAttributionView, "_user.html", object: c, badges: true, conn: @conn - + .commission__listing__body_text - {min, max} = Enum.min_max_by(c.items, & &1.base_price) diff --git a/lib/philomena_web/templates/profile/commission/_form.html.slime b/lib/philomena_web/templates/profile/commission/_form.html.slime new file mode 100644 index 00000000..10e8b1a4 --- /dev/null +++ b/lib/philomena_web/templates/profile/commission/_form.html.slime @@ -0,0 +1,34 @@ += form_for @changeset, @action, fn f -> + = if @changeset.action do + .alert.alert-danger + p Oops, something went wrong! Please check the errors below. + + .field + => label f, :information, "General Information:" + = textarea f, :information, class: "input input--wide input--text", placeholder: "A general overview about your commissions." + + .field + => label f, :contact, "Contact Information:" + = textarea f, :contact, class: "input input--wide input--text", placeholder: "How you would like potential customers to contact you (PM, Discord, Email, etc). Remember that this information will be publicly visible." + .field + => label f, :will_create, "Content you are particularly interested in drawing or creating (optional):" + = textarea f, :will_create, class: "input input--wide input--text", placeholder: "List specific content you are willing to accept commissions for." + .field + => label f, :will_not_create, "Content you will not draw or create (optional):" + = textarea f, :will_not_create, class: "input input--wide input--text", placeholder: "List specific content you are not willing to accept commissions for." + .field + => label f, :categories, "Art Categories:" + br + /= f.collection_check_boxes :categories, Commission::CATEGORIES.sort_by(&:downcase), :itself, :itself do |b| + span.commission__category + => b.label + = b.check_box + .field + => label f, :sheet_image_id, "Image ID of your commissions sheet (optional but recommended):" + br + = number_input f, :sheet_image_id, class: "input", placeholder: "1227409" + .field + = label f, :open, "Currently accepting commissions." do + => checkbox f, :open, class: "checkbox" + br + = submit "Save", class: "button" diff --git a/lib/philomena_web/templates/profile/commission/_listing_sidebar.html.slime b/lib/philomena_web/templates/profile/commission/_listing_sidebar.html.slime index 6dfd9dde..c83add82 100644 --- a/lib/philomena_web/templates/profile/commission/_listing_sidebar.html.slime +++ b/lib/philomena_web/templates/profile/commission/_listing_sidebar.html.slime @@ -88,4 +88,4 @@ a href="#" data-click-copy="#commission_url" i.fa.fa-clipboard> ' Copy - = text_input :commission_url, url, class: "input input--wide input--separate-top", readonly: true \ No newline at end of file + = text_input :commission, :commission_url, value: url, class: "input input--wide input--separate-top", readonly: true \ No newline at end of file diff --git a/lib/philomena_web/templates/profile/commission/new.html.slime b/lib/philomena_web/templates/profile/commission/new.html.slime new file mode 100644 index 00000000..98653d8d --- /dev/null +++ b/lib/philomena_web/templates/profile/commission/new.html.slime @@ -0,0 +1,5 @@ +h1 New Commission Listing +p + = link "Back to index", to: Routes.commission_path(@conn, :index) + += render PhilomenaWeb.Profile.CommissionView, "_form.html", changeset: @changeset, action: Routes.profile_commission_path(@conn, :create, @user), conn: @conn \ No newline at end of file