new commission endpoint

This commit is contained in:
byte[] 2019-12-04 10:00:41 -05:00
parent 0eaed4296e
commit 748086c01f
6 changed files with 105 additions and 5 deletions

View file

@ -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

View file

@ -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

View file

@ -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) ->

View file

@ -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"

View file

@ -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
= text_input :commission, :commission_url, value: url, class: "input input--wide input--separate-top", readonly: true

View file

@ -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