diff --git a/lib/inmana/restaurant.ex b/lib/inmana/restaurant.ex index 59af4ae..f5a91a4 100644 --- a/lib/inmana/restaurant.ex +++ b/lib/inmana/restaurant.ex @@ -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] @@ -12,6 +14,8 @@ defmodule Inmana.Restaurant do field :email, :string field :name, :string + has_many :supplies, Supply + timestamps() end diff --git a/lib/inmana/supply.ex b/lib/inmana/supply.ex new file mode 100644 index 0000000..8a6b69b --- /dev/null +++ b/lib/inmana/supply.ex @@ -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