cannery/lib/cannery/containers/container_tag.ex

47 lines
1.3 KiB
Elixir
Raw Normal View History

2021-09-12 19:27:18 -04:00
defmodule Cannery.Containers.ContainerTag do
2022-01-22 21:40:29 -05:00
@moduledoc """
Thru-table struct for associating Cannery.Containers.Container and
Cannery.Containers.Tag.
2022-01-22 21:40:29 -05:00
"""
2021-09-12 19:27:18 -04:00
use Ecto.Schema
import Ecto.Changeset
alias Cannery.Containers.{Container, Tag}
2022-01-31 20:08:01 -05:00
alias Ecto.{Changeset, UUID}
2021-09-12 19:27:18 -04:00
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "container_tags" do
2022-01-22 21:40:29 -05:00
belongs_to :container, Container
belongs_to :tag, Tag
2021-09-12 19:27:18 -04:00
timestamps()
end
@type t :: %__MODULE__{
2022-01-31 20:08:01 -05:00
id: id(),
2022-01-22 21:40:29 -05:00
container: Container.t(),
2022-01-31 20:08:01 -05:00
container_id: Container.id(),
2022-01-22 21:40:29 -05:00
tag: Tag.t(),
2022-01-31 20:08:01 -05:00
tag_id: Tag.id(),
2021-09-12 19:27:18 -04:00
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
@type new_container_tag :: %__MODULE__{}
2022-01-31 20:08:01 -05:00
@type id :: UUID.t()
2022-11-23 20:46:41 -05:00
@type changeset :: Changeset.t(t() | new_container_tag())
2021-09-12 19:27:18 -04:00
@doc false
2022-11-23 20:46:41 -05:00
@spec create_changeset(new_container_tag(), Tag.t(), Container.t()) :: changeset()
2022-07-04 20:25:55 -04:00
def create_changeset(
container_tag,
%Tag{id: tag_id, user_id: user_id},
%Container{id: container_id, user_id: user_id}
) do
2021-09-12 19:27:18 -04:00
container_tag
2022-07-04 20:25:55 -04:00
|> change(tag_id: tag_id)
|> change(container_id: container_id)
2021-09-12 19:27:18 -04:00
|> validate_required([:tag_id, :container_id])
end
end