From c45a2a16d9d749a8ff69659e89d4d9191ba7d980 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 16 Dec 2019 22:24:40 -0500 Subject: [PATCH] admin donations --- lib/philomena/donations/donation.ex | 5 ++- lib/philomena/users/user.ex | 2 + .../admin/donation/user_controller.ex | 23 +++++++++++ .../controllers/admin/donation_controller.ex | 41 +++++++++++++++++++ lib/philomena_web/router.ex | 2 + .../admin/donation/_table.html.slime | 27 ++++++++++++ .../templates/admin/donation/index.html.slime | 14 +++++++ .../admin/donation/user/index.html.slime | 26 ++++++++++++ .../views/admin/donation/user_view.ex | 3 ++ .../views/admin/donation_view.ex | 3 ++ 10 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 lib/philomena_web/controllers/admin/donation/user_controller.ex create mode 100644 lib/philomena_web/controllers/admin/donation_controller.ex create mode 100644 lib/philomena_web/templates/admin/donation/_table.html.slime create mode 100644 lib/philomena_web/templates/admin/donation/index.html.slime create mode 100644 lib/philomena_web/templates/admin/donation/user/index.html.slime create mode 100644 lib/philomena_web/views/admin/donation/user_view.ex create mode 100644 lib/philomena_web/views/admin/donation_view.ex diff --git a/lib/philomena/donations/donation.ex b/lib/philomena/donations/donation.ex index 1900ff94..2191d0bd 100644 --- a/lib/philomena/donations/donation.ex +++ b/lib/philomena/donations/donation.ex @@ -11,7 +11,7 @@ defmodule Philomena.Donations.Donation do field :amount, :decimal field :fee, :decimal field :txn_id, :string - field :reciept_id, :string + field :receipt_id, :string field :note, :string timestamps(inserted_at: :created_at) @@ -20,7 +20,8 @@ defmodule Philomena.Donations.Donation do @doc false def changeset(donation, attrs) do donation - |> cast(attrs, []) + |> cast(attrs, [:email, :amount, :note, :user_id]) |> validate_required([]) + |> foreign_key_constraint(:user_id, name: :fk_rails_5470822a00) end end diff --git a/lib/philomena/users/user.ex b/lib/philomena/users/user.ex index 6b6c0ac5..6018f18a 100644 --- a/lib/philomena/users/user.ex +++ b/lib/philomena/users/user.ex @@ -26,6 +26,7 @@ defmodule Philomena.Users.User do alias Philomena.UserFingerprints.UserFingerprint alias Philomena.UserIps.UserIp alias Philomena.Bans.User, as: UserBan + alias Philomena.Donations.Donation @derive {Phoenix.Param, key: :slug} @@ -41,6 +42,7 @@ defmodule Philomena.Users.User do has_many :user_ips, UserIp has_many :user_fingerprints, UserFingerprint has_many :bans, UserBan + has_many :donations, Donation has_one :commission, Commission many_to_many :roles, Role, join_through: "users_roles", on_replace: :delete diff --git a/lib/philomena_web/controllers/admin/donation/user_controller.ex b/lib/philomena_web/controllers/admin/donation/user_controller.ex new file mode 100644 index 00000000..1b81df27 --- /dev/null +++ b/lib/philomena_web/controllers/admin/donation/user_controller.ex @@ -0,0 +1,23 @@ +defmodule PhilomenaWeb.Admin.Donation.UserController do + use PhilomenaWeb, :controller + + alias Philomena.Users.User + alias Philomena.Donations.Donation + alias Philomena.Donations + + plug :verify_authorized + plug :load_resource, model: User, id_field: "slug", persisted: true, preload: [donations: :user] + + def show(conn, _params) do + user = conn.assigns.user + changeset = Donations.change_donation(%Donation{}) + render(conn, "index.html", title: "Donations for User `#{user.name}'", donations: user.donations, changeset: changeset) + end + + defp verify_authorized(conn, _opts) do + case Canada.Can.can?(conn.assigns.current_user, :index, Donation) do + true -> conn + _false -> PhilomenaWeb.NotAuthorizedPlug.call(conn) + end + end +end diff --git a/lib/philomena_web/controllers/admin/donation_controller.ex b/lib/philomena_web/controllers/admin/donation_controller.ex new file mode 100644 index 00000000..e73d0def --- /dev/null +++ b/lib/philomena_web/controllers/admin/donation_controller.ex @@ -0,0 +1,41 @@ +defmodule PhilomenaWeb.Admin.DonationController do + use PhilomenaWeb, :controller + + alias Philomena.Donations.Donation + alias Philomena.Donations + alias Philomena.Repo + import Ecto.Query + + plug :verify_authorized + + def index(conn, _params) do + donations = + Donation + |> order_by(desc: :created_at, asc: :user_id) + |> preload(:user) + |> Repo.paginate(conn.assigns.scrivener) + + render(conn, "index.html", title: "Admin - Donations", donations: donations) + end + + def create(conn, %{"donation" => donation_params}) do + case Donations.create_donation(donation_params) do + {:ok, _donation} -> + conn + |> put_flash(:info, "Donation successfully created.") + |> redirect(to: Routes.admin_donation_path(conn, :index)) + + _error -> + conn + |> put_flash(:error, "Error creating donation!") + |> redirect(to: Routes.admin_donation_path(conn, :index)) + end + end + + defp verify_authorized(conn, _opts) do + case Canada.Can.can?(conn.assigns.current_user, :index, Donation) do + true -> conn + _false -> PhilomenaWeb.NotAuthorizedPlug.call(conn) + end + end +end diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index f37b6702..d979e058 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -227,6 +227,8 @@ defmodule PhilomenaWeb.Router do end resources "/batch/tags", Batch.TagController, only: [:update], singleton: true + resources "/donations", DonationController, only: [:index, :create] + resources "/donations/user", Donation.UserController, only: [:show] end resources "/duplicate_reports", DuplicateReportController, only: [] do diff --git a/lib/philomena_web/templates/admin/donation/_table.html.slime b/lib/philomena_web/templates/admin/donation/_table.html.slime new file mode 100644 index 00000000..7bcd7ac6 --- /dev/null +++ b/lib/philomena_web/templates/admin/donation/_table.html.slime @@ -0,0 +1,27 @@ +table.table + thead + tr + th User + th Email + th Amount + th Note + th At + + tbody + = for donation <- @donations do + tr + td + = if donation.user do + = link donation.user.name, to: Routes.profile_path(@conn, :show, donation.user) + + td + = donation.email + + td + = donation.amount + + td title=donation.note + = String.slice(donation.note, 0, 30) + + td + = pretty_time donation.created_at diff --git a/lib/philomena_web/templates/admin/donation/index.html.slime b/lib/philomena_web/templates/admin/donation/index.html.slime new file mode 100644 index 00000000..f27ed0bb --- /dev/null +++ b/lib/philomena_web/templates/admin/donation/index.html.slime @@ -0,0 +1,14 @@ +- route = fn p -> Routes.admin_donation_path(@conn, :index, p) end +- pagination = render PhilomenaWeb.PaginationView, "_pagination.html", page: @donations, route: route, conn: @conn + +h1 Donations + +.block + .block__header + = pagination + + .block__content + = render PhilomenaWeb.Admin.DonationView, "_table.html", donations: @donations, conn: @conn + + .block__header.block__header--light + = pagination diff --git a/lib/philomena_web/templates/admin/donation/user/index.html.slime b/lib/philomena_web/templates/admin/donation/user/index.html.slime new file mode 100644 index 00000000..dc45ab3e --- /dev/null +++ b/lib/philomena_web/templates/admin/donation/user/index.html.slime @@ -0,0 +1,26 @@ +.walloftext + h1 + = @user.name + | 's Donations + + = render PhilomenaWeb.Admin.DonationView, "_table.html", donations: @donations, conn: @conn + + h1 Add Donation + = form_for @changeset, Routes.admin_donation_path(@conn, :create), fn f -> + .field + => label f, :user_id, "User ID:" + = number_input f, :user_id, class: "input input--short", value: @user.id + + .field + => label f, :email, "Email:" + = text_input f, :email, class: "input input--wide", value: @user.email + + .field + => label f, :amount, "Amount:" + = number_input f, :amount, class: "input input--wide", min: 0, step: 0.01 + + .field + => label f, :note, "Note:" + = textarea f, :note, class: "input input--wide" + + = submit "Create Donation", class: "button" diff --git a/lib/philomena_web/views/admin/donation/user_view.ex b/lib/philomena_web/views/admin/donation/user_view.ex new file mode 100644 index 00000000..4474bc96 --- /dev/null +++ b/lib/philomena_web/views/admin/donation/user_view.ex @@ -0,0 +1,3 @@ +defmodule PhilomenaWeb.Admin.Donation.UserView do + use PhilomenaWeb, :view +end diff --git a/lib/philomena_web/views/admin/donation_view.ex b/lib/philomena_web/views/admin/donation_view.ex new file mode 100644 index 00000000..352d5fa0 --- /dev/null +++ b/lib/philomena_web/views/admin/donation_view.ex @@ -0,0 +1,3 @@ +defmodule PhilomenaWeb.Admin.DonationView do + use PhilomenaWeb, :view +end