Current section

Files

Jump to
ami lib ami lesson_resource.ex
Raw

lib/ami/lesson_resource.ex

defmodule Ami.LessonResource do
use Ecto.Schema
import Ecto.{
Query,
Changeset
}
alias Ami.{
Lesson,
ResourceAttachment,
Repo
}
schema "lessons_resources" do
@timestamps_opts [type: :naive_datetime_usec]
field(:position, :integer)
timestamps()
belongs_to(:lesson, Lesson)
belongs_to(:resource_attachment, ResourceAttachment)
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:lesson_id, :resource_attachment_id, :position])
|> cast_assoc(:resource_attachment)
|> case do
%{valid?: true, changes: changes} = changeset when changes == %{} ->
%{changeset | action: :ignore}
changeset ->
changeset
end
end
def update_position(id, position) do
Repo.get!(__MODULE__, id)
|> change(%{position: position})
|> Repo.update()
end
end