[day-04] add training schema
With this we can represent a traning program for a user.
This commit is contained in:
parent
96a3e2e9f0
commit
ea5616addf
28
lib/wabanex/training.ex
Normal file
28
lib/wabanex/training.ex
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
defmodule Wabanex.Training do
|
||||||
|
use Ecto.Schema
|
||||||
|
|
||||||
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
alias Wabanex.{Exercise, User}
|
||||||
|
|
||||||
|
@primary_key {:id, :binary_id, autogenerate: true}
|
||||||
|
@foreign_key_type :binary_id
|
||||||
|
|
||||||
|
@fields [:start_date, :end_date, :user_id]
|
||||||
|
@required_fields [:start_date, :user_id]
|
||||||
|
|
||||||
|
schema "trainings" do
|
||||||
|
field :start_date, :date
|
||||||
|
field :end_date, :date
|
||||||
|
|
||||||
|
belongs_to :user, User
|
||||||
|
|
||||||
|
timestamps()
|
||||||
|
end
|
||||||
|
|
||||||
|
def changeset(params) do
|
||||||
|
%__MODULE__{}
|
||||||
|
|> cast(params, @fields)
|
||||||
|
|> validate_required(@required_fields)
|
||||||
|
end
|
||||||
|
end
|
9
lib/wabanex/trainings/create.ex
Normal file
9
lib/wabanex/trainings/create.ex
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
defmodule Wabanex.Trainings.Create do
|
||||||
|
alias Wabanex.{Training, Repo}
|
||||||
|
|
||||||
|
def call(params) do
|
||||||
|
params
|
||||||
|
|> Training.changeset()
|
||||||
|
|> Repo.insert()
|
||||||
|
end
|
||||||
|
end
|
@ -3,6 +3,8 @@ defmodule Wabanex.User do
|
|||||||
|
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
alias Wabanex.Training
|
||||||
|
|
||||||
@primary_key {:id, :binary_id, autogenerate: true}
|
@primary_key {:id, :binary_id, autogenerate: true}
|
||||||
|
|
||||||
@fields ~w(name email password)a
|
@fields ~w(name email password)a
|
||||||
@ -12,6 +14,8 @@ defmodule Wabanex.User do
|
|||||||
field :email, :string
|
field :email, :string
|
||||||
field :password, :string
|
field :password, :string
|
||||||
|
|
||||||
|
has_many :trainings, Training
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
defmodule Wabanex.Repo.Migrations.CreateTrainingsTable do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
create table(:trainings) do
|
||||||
|
add :start_date, :date, null: false
|
||||||
|
add :end_date, :date
|
||||||
|
add :user_id, references(:users), null: false
|
||||||
|
|
||||||
|
timestamps()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user