Skip to content

Commit

Permalink
Create supply struct definition
Browse files Browse the repository at this point in the history
  • Loading branch information
erickmp07 committed Oct 20, 2021
1 parent a0ce8cc commit d5463b7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/inmana/restaurant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule Inmana.Restaurant do
use Ecto.Schema
import Ecto.Changeset

alias Inmana.Supply

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

@required_params [:email, :name]
Expand All @@ -12,6 +14,8 @@ defmodule Inmana.Restaurant do
field :email, :string
field :name, :string

has_many :supplies, Supply

timestamps()
end

Expand Down
30 changes: 30 additions & 0 deletions lib/inmana/supply.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Inmana.Supply do
use Ecto.Schema
import Ecto.Changeset

alias Inmana.Restaurant

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

@required_params [:description, :expiration_date, :responsible, :restaurant_id]

@derive {Jason.Encoder, only: @required_params ++ [:id]}

schema "supplies" do
field :description, :string
field :expiration_date, :date
field :responsible, :string

belongs_to :restaurant, Restaurant

timestamps()
end

def changeset(params) do
%__MODULE__{}
|> cast(params, @required_params)
|> validate_required(@required_params)
|> validate_length(:description, min: 3)
|> validate_length(:responsible, min: 3)
end
end

0 comments on commit d5463b7

Please sign in to comment.