Skip to content

Commit

Permalink
[feature] Added the promotion business layer
Browse files Browse the repository at this point in the history
  • Loading branch information
julioleitao committed Mar 15, 2019
1 parent 602d61b commit 044e43f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apps/store_core/lib/store_core/contexts/promotions.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule StoreCore.Promotions do
alias StoreCore.{Promotion, Repo}

def list, do: Promotion |> Repo.all
end
21 changes: 21 additions & 0 deletions apps/store_core/lib/store_core/schemas/promotion.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule StoreCore.Promotion do
use Ecto.Schema

import Ecto.Changeset

@primary_key {:id, :binary_id, autogenerate: true}

schema "promotions" do
field :name, :string

timestamps()
end

@required_params [:name]

def changeset(promotion, params \\ %{}) do
promotion
|> cast(params, @required_params)
|> validate_required(@required_params)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule StoreCore.Repo.Migrations.CreatePromotions do
use Ecto.Migration

alias StoreCore.{Promotion, Repo}

def change do
create table(:promotions, primary_key: false) do
add :id, :uuid, primary_key: true
add :name, :string, null: false

timestamps()
end

create unique_index(:promotions, [:name])

flush()

insert_promotions()
end

defp insert_promotions() do
["Pague 1 Leve 2", "3 Por 10 reais"]
|> Enum.map(fn name ->
%Promotion{name: name}
|> Promotion.changeset()
|> Repo.insert()
end)
end
end
9 changes: 9 additions & 0 deletions apps/store_core/test/store_core/contexts/promotions_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule StoreCore.PromotionsTest do
use StoreCore.DataCase

alias StoreCore.Promotions

test "list the default promotions" do
assert length(Promotions.list()) === 2
end
end

0 comments on commit 044e43f

Please sign in to comment.