-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Added the promotion business layer
- Loading branch information
1 parent
602d61b
commit 044e43f
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
29 changes: 29 additions & 0 deletions
29
apps/store_core/priv/repo/migrations/20190315041038_create_promotions.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |