diff --git a/lib/philomena/comments/comment.ex b/lib/philomena/comments/comment.ex index 59a01795..231f6808 100644 --- a/lib/philomena/comments/comment.ex +++ b/lib/philomena/comments/comment.ex @@ -4,7 +4,7 @@ defmodule Philomena.Comments.Comment do schema "comments" do belongs_to :user, Philomena.Users.User - belongs_to :image, Philomena.Images.Images + belongs_to :image, Philomena.Images.Image belongs_to :deleted_by, Philomena.Users.User field :body, :string diff --git a/lib/philomena/commissions.ex b/lib/philomena/commissions.ex new file mode 100644 index 00000000..10326b14 --- /dev/null +++ b/lib/philomena/commissions.ex @@ -0,0 +1,200 @@ +defmodule Philomena.Commissions do + @moduledoc """ + The Commissions context. + """ + + import Ecto.Query, warn: false + alias Philomena.Repo + + alias Philomena.Commissions.Commission + + @doc """ + Returns the list of commissions. + + ## Examples + + iex> list_commissions() + [%Commission{}, ...] + + """ + def list_commissions do + Repo.all(Commission) + end + + @doc """ + Gets a single commission. + + Raises `Ecto.NoResultsError` if the Commission does not exist. + + ## Examples + + iex> get_commission!(123) + %Commission{} + + iex> get_commission!(456) + ** (Ecto.NoResultsError) + + """ + def get_commission!(id), do: Repo.get!(Commission, id) + + @doc """ + Creates a commission. + + ## Examples + + iex> create_commission(%{field: value}) + {:ok, %Commission{}} + + iex> create_commission(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_commission(attrs \\ %{}) do + %Commission{} + |> Commission.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a commission. + + ## Examples + + iex> update_commission(commission, %{field: new_value}) + {:ok, %Commission{}} + + iex> update_commission(commission, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_commission(%Commission{} = commission, attrs) do + commission + |> Commission.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a Commission. + + ## Examples + + iex> delete_commission(commission) + {:ok, %Commission{}} + + iex> delete_commission(commission) + {:error, %Ecto.Changeset{}} + + """ + def delete_commission(%Commission{} = commission) do + Repo.delete(commission) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking commission changes. + + ## Examples + + iex> change_commission(commission) + %Ecto.Changeset{source: %Commission{}} + + """ + def change_commission(%Commission{} = commission) do + Commission.changeset(commission, %{}) + end + + alias Philomena.Commissions.Item + + @doc """ + Returns the list of commission_items. + + ## Examples + + iex> list_commission_items() + [%Item{}, ...] + + """ + def list_commission_items do + Repo.all(Item) + end + + @doc """ + Gets a single item. + + Raises `Ecto.NoResultsError` if the Item does not exist. + + ## Examples + + iex> get_item!(123) + %Item{} + + iex> get_item!(456) + ** (Ecto.NoResultsError) + + """ + def get_item!(id), do: Repo.get!(Item, id) + + @doc """ + Creates a item. + + ## Examples + + iex> create_item(%{field: value}) + {:ok, %Item{}} + + iex> create_item(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_item(attrs \\ %{}) do + %Item{} + |> Item.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a item. + + ## Examples + + iex> update_item(item, %{field: new_value}) + {:ok, %Item{}} + + iex> update_item(item, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_item(%Item{} = item, attrs) do + item + |> Item.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a Item. + + ## Examples + + iex> delete_item(item) + {:ok, %Item{}} + + iex> delete_item(item) + {:error, %Ecto.Changeset{}} + + """ + def delete_item(%Item{} = item) do + Repo.delete(item) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking item changes. + + ## Examples + + iex> change_item(item) + %Ecto.Changeset{source: %Item{}} + + """ + def change_item(%Item{} = item) do + Item.changeset(item, %{}) + end +end diff --git a/lib/philomena/commissions/commission.ex b/lib/philomena/commissions/commission.ex new file mode 100644 index 00000000..ba419e97 --- /dev/null +++ b/lib/philomena/commissions/commission.ex @@ -0,0 +1,26 @@ +defmodule Philomena.Commissions.Commission do + use Ecto.Schema + import Ecto.Changeset + + schema "commissions" do + belongs_to :user, Philomena.Users.User + belongs_to :sheet_image, Philomena.Images.Image + + field :open, :boolean + field :categories, {:array, :string}, default: [] + field :information, :string + field :contact, :string + field :will_create, :string + field :will_not_create, :string + field :commission_items_count, :integer, default: 0 + + timestamps(inserted_at: :created_at) + end + + @doc false + def changeset(commission, attrs) do + commission + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/lib/philomena/commissions/item.ex b/lib/philomena/commissions/item.ex new file mode 100644 index 00000000..f7ce5de2 --- /dev/null +++ b/lib/philomena/commissions/item.ex @@ -0,0 +1,23 @@ +defmodule Philomena.Commissions.Item do + use Ecto.Schema + import Ecto.Changeset + + schema "commission_items" do + belongs_to :commission, Philomena.Commissions.Commission + belongs_to :example_image, Philomena.Images.Image + + field :item_type, :string + field :description, :string + field :base_price, :decimal + field :add_ons, :string + + timestamps(inserted_at: :created_at) + end + + @doc false + def changeset(item, attrs) do + item + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/test/philomena/commissions_test.exs b/test/philomena/commissions_test.exs new file mode 100644 index 00000000..73c816e9 --- /dev/null +++ b/test/philomena/commissions_test.exs @@ -0,0 +1,119 @@ +defmodule Philomena.CommissionsTest do + use Philomena.DataCase + + alias Philomena.Commissions + + describe "commissions" do + alias Philomena.Commissions.Commission + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def commission_fixture(attrs \\ %{}) do + {:ok, commission} = + attrs + |> Enum.into(@valid_attrs) + |> Commissions.create_commission() + + commission + end + + test "list_commissions/0 returns all commissions" do + commission = commission_fixture() + assert Commissions.list_commissions() == [commission] + end + + test "get_commission!/1 returns the commission with given id" do + commission = commission_fixture() + assert Commissions.get_commission!(commission.id) == commission + end + + test "create_commission/1 with valid data creates a commission" do + assert {:ok, %Commission{} = commission} = Commissions.create_commission(@valid_attrs) + end + + test "create_commission/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Commissions.create_commission(@invalid_attrs) + end + + test "update_commission/2 with valid data updates the commission" do + commission = commission_fixture() + assert {:ok, %Commission{} = commission} = Commissions.update_commission(commission, @update_attrs) + end + + test "update_commission/2 with invalid data returns error changeset" do + commission = commission_fixture() + assert {:error, %Ecto.Changeset{}} = Commissions.update_commission(commission, @invalid_attrs) + assert commission == Commissions.get_commission!(commission.id) + end + + test "delete_commission/1 deletes the commission" do + commission = commission_fixture() + assert {:ok, %Commission{}} = Commissions.delete_commission(commission) + assert_raise Ecto.NoResultsError, fn -> Commissions.get_commission!(commission.id) end + end + + test "change_commission/1 returns a commission changeset" do + commission = commission_fixture() + assert %Ecto.Changeset{} = Commissions.change_commission(commission) + end + end + + describe "commission_items" do + alias Philomena.Commissions.Item + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def item_fixture(attrs \\ %{}) do + {:ok, item} = + attrs + |> Enum.into(@valid_attrs) + |> Commissions.create_item() + + item + end + + test "list_commission_items/0 returns all commission_items" do + item = item_fixture() + assert Commissions.list_commission_items() == [item] + end + + test "get_item!/1 returns the item with given id" do + item = item_fixture() + assert Commissions.get_item!(item.id) == item + end + + test "create_item/1 with valid data creates a item" do + assert {:ok, %Item{} = item} = Commissions.create_item(@valid_attrs) + end + + test "create_item/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Commissions.create_item(@invalid_attrs) + end + + test "update_item/2 with valid data updates the item" do + item = item_fixture() + assert {:ok, %Item{} = item} = Commissions.update_item(item, @update_attrs) + end + + test "update_item/2 with invalid data returns error changeset" do + item = item_fixture() + assert {:error, %Ecto.Changeset{}} = Commissions.update_item(item, @invalid_attrs) + assert item == Commissions.get_item!(item.id) + end + + test "delete_item/1 deletes the item" do + item = item_fixture() + assert {:ok, %Item{}} = Commissions.delete_item(item) + assert_raise Ecto.NoResultsError, fn -> Commissions.get_item!(item.id) end + end + + test "change_item/1 returns a item changeset" do + item = item_fixture() + assert %Ecto.Changeset{} = Commissions.change_item(item) + end + end +end