cannery/lib/cannery/containers.ex

169 lines
4.4 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule Cannery.Containers do
@moduledoc """
The Containers context.
"""
import Ecto.Query, warn: false
2022-01-31 23:49:22 -05:00
alias Cannery.{Accounts.User, Repo, Tags.Tag}
2022-01-31 20:10:48 -05:00
alias Cannery.Containers.{Container, ContainerTag}
alias Ecto.{Changeset}
2021-09-02 23:31:14 -04:00
@doc """
Returns the list of containers.
## Examples
iex> list_containers()
[%Container{}, ...]
"""
2022-01-31 20:10:48 -05:00
@spec list_containers(user_or_user_id :: User.t() | User.id()) :: [Container.t()]
def list_containers(%{id: user_id}), do: list_containers(user_id)
def list_containers(user_id), do: Repo.all(from c in Container, where: c.user_id == ^user_id)
2021-09-02 23:31:14 -04:00
@doc """
Gets a single container.
Raises `Ecto.NoResultsError` if the Container does not exist.
## Examples
iex> get_container!(123)
%Container{}
iex> get_container!(456)
** (Ecto.NoResultsError)
"""
2022-01-31 20:10:48 -05:00
@spec get_container!(Container.id()) :: Container.t()
2021-09-02 23:31:14 -04:00
def get_container!(id), do: Repo.get!(Container, id)
@doc """
Creates a container.
## Examples
iex> create_container(%{field: value})
{:ok, %Container{}}
iex> create_container(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
2022-01-31 23:45:00 -05:00
@spec create_container(attrs :: map()) ::
{:ok, Container.t()} | {:error, Changeset.t(Container.new_container())}
2022-01-22 21:40:29 -05:00
def create_container(attrs) do
%Container{} |> Container.changeset(attrs) |> Repo.insert()
2021-09-02 23:31:14 -04:00
end
@doc """
Updates a container.
## Examples
iex> update_container(container, %{field: new_value})
{:ok, %Container{}}
iex> update_container(container, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
2022-01-31 23:45:00 -05:00
@spec update_container(Container.t(), attrs :: map()) ::
{:ok, Container.t()} | {:error, Changeset.t(Container.t())}
2022-01-22 21:40:29 -05:00
def update_container(container, attrs) do
container |> Container.changeset(attrs) |> Repo.update()
2021-09-02 23:31:14 -04:00
end
@doc """
Deletes a container.
## Examples
iex> delete_container(container)
{:ok, %Container{}}
iex> delete_container(container)
{:error, %Ecto.Changeset{}}
"""
2022-01-31 23:45:00 -05:00
@spec delete_container(Container.t()) ::
{:ok, Container.t()} | {:error, Changeset.t(Container.t())}
2022-01-31 21:42:24 -05:00
def delete_container(container), do: container |> Repo.delete()
@doc """
Deletes a container.
## Examples
iex> delete_container(container)
%Container{}
"""
2022-01-31 23:45:00 -05:00
@spec delete_container!(Container.t()) :: Container.t()
2022-01-31 21:42:24 -05:00
def delete_container!(container), do: container |> Repo.delete!()
2021-09-02 23:31:14 -04:00
@doc """
Returns an `%Ecto.Changeset{}` for tracking container changes.
## Examples
iex> change_container(container)
%Ecto.Changeset{data: %Container{}}
2022-01-22 21:40:29 -05:00
iex> change_container(%Ecto.Changeset{})
%Ecto.Changeset{data: %Container{}}
2021-09-02 23:31:14 -04:00
"""
2022-01-31 23:45:00 -05:00
@spec change_container(Container.t() | Container.new_container()) ::
Changeset.t(Container.t() | Container.new_container())
2022-01-31 20:10:48 -05:00
@spec change_container(Container.t() | Container.new_container(), attrs :: map()) ::
2022-01-31 23:45:00 -05:00
Changeset.t(Container.t() | Container.new_container())
2022-01-22 21:40:29 -05:00
def change_container(container, attrs \\ %{}), do: container |> Container.changeset(attrs)
2022-01-31 20:10:48 -05:00
@doc """
Adds a tag to a container
## Examples
iex> add_tag!(container, tag)
%Container{}
iex> add_tag!(container_id, tag_id)
%Container{}
"""
@spec add_tag!(Container.t(), Tag.t()) :: Container.t()
def add_tag!(%{id: container_id}, %{id: tag_id}), do: add_tag!(container_id, tag_id)
@spec add_tag!(Container.id(), Tag.id()) :: Container.t()
def add_tag!(container_id, tag_id)
when not (container_id |> is_nil()) and not (tag_id |> is_nil()) do
%ContainerTag{}
|> ContainerTag.changeset(%{"container_id" => container_id, "tag_id" => tag_id})
|> Repo.insert!()
end
@doc """
Removes a tag from a container
## Examples
iex> remove_tag!(container, tag)
%Container{}
iex> remove_tag!(container_id, tag_id)
%Container{}
"""
@spec remove_tag!(Container.t(), Tag.t()) :: Container.t()
def remove_tag!(%{id: container_id}, %{id: tag_id}), do: remove_tag!(container_id, tag_id)
@spec remove_tag!(Container.id(), Tag.id()) :: Container.t()
def remove_tag!(container_id, tag_id)
when not (container_id |> is_nil()) and not (tag_id |> is_nil()) do
Repo.delete_all(
from ct in ContainerTag,
where: ct.container_id == ^container_id,
where: ct.tag_id == ^tag_id
)
end
2021-09-02 23:31:14 -04:00
end