2021-09-02 23:31:14 -04:00
|
|
|
defmodule Cannery.Containers.Container do
|
2022-01-22 21:40:29 -05:00
|
|
|
@moduledoc """
|
|
|
|
A container that holds ammunition and belongs to a user.
|
|
|
|
"""
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2022-01-22 21:40:29 -05:00
|
|
|
alias Ecto.{Changeset, UUID}
|
2023-03-18 21:06:00 -04:00
|
|
|
alias Cannery.{Accounts.User, Containers.ContainerTag, Containers.Tag}
|
2021-09-02 23:31:14 -04:00
|
|
|
|
2022-11-09 23:33:50 -05:00
|
|
|
@derive {Jason.Encoder,
|
|
|
|
only: [
|
|
|
|
:id,
|
|
|
|
:name,
|
|
|
|
:desc,
|
|
|
|
:location,
|
|
|
|
:type,
|
|
|
|
:tags
|
|
|
|
]}
|
2021-09-02 23:31:14 -04:00
|
|
|
@primary_key {:id, :binary_id, autogenerate: true}
|
|
|
|
@foreign_key_type :binary_id
|
|
|
|
schema "containers" do
|
2021-09-12 19:26:04 -04:00
|
|
|
field :name, :string
|
2021-09-02 23:31:14 -04:00
|
|
|
field :desc, :string
|
|
|
|
field :location, :string
|
|
|
|
field :type, :string
|
2021-09-12 18:54:53 -04:00
|
|
|
|
2023-03-18 21:06:00 -04:00
|
|
|
field :user_id, :binary_id
|
2021-09-02 23:31:14 -04:00
|
|
|
|
2022-02-13 21:14:48 -05:00
|
|
|
many_to_many :tags, Tag, join_through: ContainerTag
|
2022-02-05 01:59:09 -05:00
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2023-03-18 21:06:00 -04:00
|
|
|
@type t :: %__MODULE__{
|
2022-01-31 20:08:01 -05:00
|
|
|
id: id(),
|
2021-09-12 19:26:04 -04:00
|
|
|
name: String.t(),
|
|
|
|
desc: String.t(),
|
|
|
|
location: String.t(),
|
|
|
|
type: String.t(),
|
2022-01-31 20:08:01 -05:00
|
|
|
user_id: User.id(),
|
2022-02-13 21:14:48 -05:00
|
|
|
tags: [Tag.t()] | nil,
|
2021-09-12 19:26:04 -04:00
|
|
|
inserted_at: NaiveDateTime.t(),
|
|
|
|
updated_at: NaiveDateTime.t()
|
|
|
|
}
|
2023-03-18 21:06:00 -04:00
|
|
|
@type new_container :: %__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())
|
2022-01-22 21:40:29 -05:00
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
@doc false
|
2022-11-23 20:46:41 -05:00
|
|
|
@spec create_changeset(new_container(), User.t(), attrs :: map()) :: changeset()
|
2022-07-04 20:30:05 -04:00
|
|
|
def create_changeset(container, %User{id: user_id}, attrs) do
|
2021-09-02 23:31:14 -04:00
|
|
|
container
|
2022-07-04 20:30:05 -04:00
|
|
|
|> change(user_id: user_id)
|
|
|
|
|> cast(attrs, [:name, :desc, :type, :location])
|
2022-01-22 21:40:29 -05:00
|
|
|
|> validate_required([:name, :type, :user_id])
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
2022-02-08 22:10:48 -05:00
|
|
|
|
|
|
|
@doc false
|
2022-11-23 20:46:41 -05:00
|
|
|
@spec update_changeset(t() | new_container(), attrs :: map()) :: changeset()
|
2022-02-08 22:10:48 -05:00
|
|
|
def update_changeset(container, attrs) do
|
|
|
|
container
|
|
|
|
|> cast(attrs, [:name, :desc, :type, :location])
|
2022-07-01 19:53:44 -04:00
|
|
|
|> validate_required([:name, :type])
|
2022-02-08 22:10:48 -05:00
|
|
|
end
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|