memEx/lib/memex/pipelines/step.ex

79 lines
2.1 KiB
Elixir
Raw Permalink Normal View History

2022-11-26 21:26:21 -05:00
defmodule Memex.Pipelines.Steps.Step do
2022-11-24 15:35:29 -05:00
@moduledoc """
Represents a step taken while executing a pipeline
"""
use Ecto.Schema
import Ecto.Changeset
alias Ecto.{Changeset, UUID}
2022-11-26 20:37:10 -05:00
alias Memex.{Accounts.User, Pipelines.Pipeline}
2022-11-24 15:35:29 -05:00
2022-12-20 19:15:08 -05:00
@derive {Jason.Encoder,
only: [
:title,
:content,
:position,
:inserted_at,
:updated_at
]}
2022-11-24 15:35:29 -05:00
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "steps" do
field :title, :string
field :content, :string
field :position, :integer
belongs_to :pipeline, Pipeline
2023-03-16 23:48:03 -04:00
field :user_id, :binary_id
2022-11-24 15:35:29 -05:00
timestamps()
end
@type t :: %__MODULE__{
title: String.t(),
content: String.t(),
position: non_neg_integer(),
pipeline: Pipeline.t() | Ecto.Association.NotLoaded.t(),
pipeline_id: Pipeline.id(),
user_id: User.id(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
@type id :: UUID.t()
@type changeset :: Changeset.t(t())
@doc false
@spec create_changeset(attrs :: map(), position :: non_neg_integer(), Pipeline.t(), User.t()) ::
changeset()
def create_changeset(attrs, position, %Pipeline{id: pipeline_id, user_id: user_id}, %User{
id: user_id
}) do
%__MODULE__{}
|> cast(attrs, [:title, :content])
|> change(pipeline_id: pipeline_id, user_id: user_id, position: position)
2023-11-04 22:10:16 -04:00
|> validate_required([:title, :user_id, :position])
2022-11-24 15:35:29 -05:00
end
2022-11-26 21:26:21 -05:00
@spec update_changeset(t(), attrs :: map(), User.t()) ::
2022-11-24 15:35:29 -05:00
changeset()
def update_changeset(
%{user_id: user_id} = step,
attrs,
%User{id: user_id}
) do
step
|> cast(attrs, [:title, :content])
2023-11-04 22:10:16 -04:00
|> validate_required([:title, :user_id, :position])
2022-11-26 21:26:21 -05:00
end
@spec position_changeset(t(), position :: non_neg_integer(), User.t()) :: changeset()
def position_changeset(
%{user_id: user_id} = step,
position,
%User{id: user_id}
) do
step
2022-11-24 15:35:29 -05:00
|> change(position: position)
2023-11-04 22:10:16 -04:00
|> validate_required([:title, :user_id, :position])
2022-11-24 15:35:29 -05:00
end
end