philomena/lib/philomena_web/controllers/admin/donation_controller.ex

42 lines
1.1 KiB
Elixir
Raw Normal View History

2019-12-17 04:24:40 +01:00
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.")
2024-06-06 22:28:35 +02:00
|> redirect(to: ~p"/admin/donations")
2019-12-17 04:24:40 +01:00
_error ->
conn
|> put_flash(:error, "Error creating donation!")
2024-06-06 22:28:35 +02:00
|> redirect(to: ~p"/admin/donations")
2019-12-17 04:24:40 +01:00
end
end
defp verify_authorized(conn, _opts) do
case Canada.Can.can?(conn.assigns.current_user, :index, Donation) do
2020-01-11 05:20:19 +01:00
true -> conn
2019-12-17 04:24:40 +01:00
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end
end
end