Current section

Files

Jump to
ami lib ami unit.ex
Raw

lib/ami/unit.ex

defmodule Ami.Unit do
use Ecto.Schema
import Ecto.{
Query,
Changeset
}
alias Ami.{
Repo,
Course,
Lesson,
UnitLesson
}
schema "units" do
@timestamps_opts [type: :naive_datetime_usec]
field(:title, :string)
field(:description, :string)
field(:grade_value, :integer)
field(:position, :integer)
field(:created_at, :utc_datetime_usec)
field(:updated_at, :utc_datetime_usec)
belongs_to(:course, Course)
# has_many(:lessons, Lesson)
has_many(:unit_lessons, UnitLesson, on_delete: :delete_all)
has_many(:lessons, through: [:unit_lessons, :lesson])
end
def completed?(unit, enrollment) do
Enum.all?(unit.lessons, &(Lesson.completed?(&1, enrollment) == true))
end
def changeset(%__MODULE__{} = unit, params \\ %{}) do
unit
|> cast(params, [
:course_id,
:title,
:description,
:grade_value,
:position,
:created_at,
:updated_at
])
|> validate_required([
:title,
:grade_value
])
end
def update_position(id, position) do
Repo.get!(__MODULE__, id)
|> change(%{position: position})
|> Repo.update()
end
end