[day-04] add exercise schema
So a training is a collection of exercises that have a description and a protocol to be followed.
This commit is contained in:
parent
ea5616addf
commit
58c4601203
30
lib/wabanex/exercise.ex
Normal file
30
lib/wabanex/exercise.ex
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
defmodule Wabanex.Exercise do
|
||||||
|
use Ecto.Schema
|
||||||
|
|
||||||
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
alias Wabanex.Training
|
||||||
|
|
||||||
|
@primary_key {:id, :binary_id, autogenerate: true}
|
||||||
|
@foreign_key_type :binary_id
|
||||||
|
|
||||||
|
@fields [:name, :video_url, :protocol_description, :repetitions, :training_id]
|
||||||
|
@required_fields [:name, :protocol_description, :repetitions]
|
||||||
|
|
||||||
|
schema "exercises" do
|
||||||
|
field :name, :string
|
||||||
|
field :video_url, :string
|
||||||
|
field :protocol_description, :string
|
||||||
|
field :repetitions, :string
|
||||||
|
|
||||||
|
belongs_to :training, Training
|
||||||
|
|
||||||
|
timestamps()
|
||||||
|
end
|
||||||
|
|
||||||
|
def changeset(%__MODULE__{} = struct, params) do
|
||||||
|
struct
|
||||||
|
|> cast(params, @fields)
|
||||||
|
|> validate_required(@required_fields)
|
||||||
|
end
|
||||||
|
end
|
@ -17,6 +17,8 @@ defmodule Wabanex.Training do
|
|||||||
|
|
||||||
belongs_to :user, User
|
belongs_to :user, User
|
||||||
|
|
||||||
|
has_many :exercises, Exercise
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -24,5 +26,6 @@ defmodule Wabanex.Training do
|
|||||||
%__MODULE__{}
|
%__MODULE__{}
|
||||||
|> cast(params, @fields)
|
|> cast(params, @fields)
|
||||||
|> validate_required(@required_fields)
|
|> validate_required(@required_fields)
|
||||||
|
|> cast_assoc(:exercises)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
defmodule Wabanex.Repo.Migrations.CreateExercisesTable do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
create table(:exercises) do
|
||||||
|
add :name, :string, null: false
|
||||||
|
add :video_url, :string
|
||||||
|
add :protocol_description, :string, null: false
|
||||||
|
add :repetitions, :string, null: false
|
||||||
|
add :training_id, references(:trainings), null: false
|
||||||
|
|
||||||
|
timestamps()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user