From 1044f9a9f859866a844ecdee9fbdbe446df73c8c Mon Sep 17 00:00:00 2001 From: "Liam P. White" Date: Wed, 28 Aug 2019 12:53:01 -0400 Subject: [PATCH] context: donations --- lib/philomena/donations.ex | 104 ++++++++++++++++++++++++++++ lib/philomena/donations/donation.ex | 24 +++++++ test/philomena/donations_test.exs | 62 +++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 lib/philomena/donations.ex create mode 100644 lib/philomena/donations/donation.ex create mode 100644 test/philomena/donations_test.exs diff --git a/lib/philomena/donations.ex b/lib/philomena/donations.ex new file mode 100644 index 00000000..e9a040b0 --- /dev/null +++ b/lib/philomena/donations.ex @@ -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 diff --git a/lib/philomena/donations/donation.ex b/lib/philomena/donations/donation.ex new file mode 100644 index 00000000..dceb2b37 --- /dev/null +++ b/lib/philomena/donations/donation.ex @@ -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 diff --git a/test/philomena/donations_test.exs b/test/philomena/donations_test.exs new file mode 100644 index 00000000..541f471d --- /dev/null +++ b/test/philomena/donations_test.exs @@ -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