mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 21:47:59 +01:00
context: donations
This commit is contained in:
parent
5556699799
commit
1044f9a9f8
3 changed files with 190 additions and 0 deletions
104
lib/philomena/donations.ex
Normal file
104
lib/philomena/donations.ex
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
defmodule Philomena.Donations do
|
||||||
|
@moduledoc """
|
||||||
|
The Donations context.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import Ecto.Query, warn: false
|
||||||
|
alias Philomena.Repo
|
||||||
|
|
||||||
|
alias Philomena.Donations.Donation
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns the list of donations.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> list_donations()
|
||||||
|
[%Donation{}, ...]
|
||||||
|
|
||||||
|
"""
|
||||||
|
def list_donations do
|
||||||
|
Repo.all(Donation)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Gets a single donation.
|
||||||
|
|
||||||
|
Raises `Ecto.NoResultsError` if the Donation does not exist.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> get_donation!(123)
|
||||||
|
%Donation{}
|
||||||
|
|
||||||
|
iex> get_donation!(456)
|
||||||
|
** (Ecto.NoResultsError)
|
||||||
|
|
||||||
|
"""
|
||||||
|
def get_donation!(id), do: Repo.get!(Donation, id)
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Creates a donation.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> create_donation(%{field: value})
|
||||||
|
{:ok, %Donation{}}
|
||||||
|
|
||||||
|
iex> create_donation(%{field: bad_value})
|
||||||
|
{:error, %Ecto.Changeset{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def create_donation(attrs \\ %{}) do
|
||||||
|
%Donation{}
|
||||||
|
|> Donation.changeset(attrs)
|
||||||
|
|> Repo.insert()
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Updates a donation.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> update_donation(donation, %{field: new_value})
|
||||||
|
{:ok, %Donation{}}
|
||||||
|
|
||||||
|
iex> update_donation(donation, %{field: bad_value})
|
||||||
|
{:error, %Ecto.Changeset{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def update_donation(%Donation{} = donation, attrs) do
|
||||||
|
donation
|
||||||
|
|> Donation.changeset(attrs)
|
||||||
|
|> Repo.update()
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Deletes a Donation.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> delete_donation(donation)
|
||||||
|
{:ok, %Donation{}}
|
||||||
|
|
||||||
|
iex> delete_donation(donation)
|
||||||
|
{:error, %Ecto.Changeset{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def delete_donation(%Donation{} = donation) do
|
||||||
|
Repo.delete(donation)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns an `%Ecto.Changeset{}` for tracking donation changes.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> change_donation(donation)
|
||||||
|
%Ecto.Changeset{source: %Donation{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def change_donation(%Donation{} = donation) do
|
||||||
|
Donation.changeset(donation, %{})
|
||||||
|
end
|
||||||
|
end
|
24
lib/philomena/donations/donation.ex
Normal file
24
lib/philomena/donations/donation.ex
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
defmodule Philomena.Donations.Donation do
|
||||||
|
use Ecto.Schema
|
||||||
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
schema "donations" do
|
||||||
|
belongs_to :user, Philomena.Users.User
|
||||||
|
|
||||||
|
field :email, :string
|
||||||
|
field :amount, :decimal
|
||||||
|
field :fee, :decimal
|
||||||
|
field :txn_id, :string
|
||||||
|
field :reciept_id, :string
|
||||||
|
field :note, :string
|
||||||
|
|
||||||
|
timestamps(inserted_at: :created_at)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def changeset(donation, attrs) do
|
||||||
|
donation
|
||||||
|
|> cast(attrs, [])
|
||||||
|
|> validate_required([])
|
||||||
|
end
|
||||||
|
end
|
62
test/philomena/donations_test.exs
Normal file
62
test/philomena/donations_test.exs
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
defmodule Philomena.DonationsTest do
|
||||||
|
use Philomena.DataCase
|
||||||
|
|
||||||
|
alias Philomena.Donations
|
||||||
|
|
||||||
|
describe "donations" do
|
||||||
|
alias Philomena.Donations.Donation
|
||||||
|
|
||||||
|
@valid_attrs %{}
|
||||||
|
@update_attrs %{}
|
||||||
|
@invalid_attrs %{}
|
||||||
|
|
||||||
|
def donation_fixture(attrs \\ %{}) do
|
||||||
|
{:ok, donation} =
|
||||||
|
attrs
|
||||||
|
|> Enum.into(@valid_attrs)
|
||||||
|
|> Donations.create_donation()
|
||||||
|
|
||||||
|
donation
|
||||||
|
end
|
||||||
|
|
||||||
|
test "list_donations/0 returns all donations" do
|
||||||
|
donation = donation_fixture()
|
||||||
|
assert Donations.list_donations() == [donation]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "get_donation!/1 returns the donation with given id" do
|
||||||
|
donation = donation_fixture()
|
||||||
|
assert Donations.get_donation!(donation.id) == donation
|
||||||
|
end
|
||||||
|
|
||||||
|
test "create_donation/1 with valid data creates a donation" do
|
||||||
|
assert {:ok, %Donation{} = donation} = Donations.create_donation(@valid_attrs)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "create_donation/1 with invalid data returns error changeset" do
|
||||||
|
assert {:error, %Ecto.Changeset{}} = Donations.create_donation(@invalid_attrs)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "update_donation/2 with valid data updates the donation" do
|
||||||
|
donation = donation_fixture()
|
||||||
|
assert {:ok, %Donation{} = donation} = Donations.update_donation(donation, @update_attrs)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "update_donation/2 with invalid data returns error changeset" do
|
||||||
|
donation = donation_fixture()
|
||||||
|
assert {:error, %Ecto.Changeset{}} = Donations.update_donation(donation, @invalid_attrs)
|
||||||
|
assert donation == Donations.get_donation!(donation.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "delete_donation/1 deletes the donation" do
|
||||||
|
donation = donation_fixture()
|
||||||
|
assert {:ok, %Donation{}} = Donations.delete_donation(donation)
|
||||||
|
assert_raise Ecto.NoResultsError, fn -> Donations.get_donation!(donation.id) end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "change_donation/1 returns a donation changeset" do
|
||||||
|
donation = donation_fixture()
|
||||||
|
assert %Ecto.Changeset{} = Donations.change_donation(donation)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue