cannery/lib/cannery/containers.ex

223 lines
5.8 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule Cannery.Containers do
@moduledoc """
The Containers context.
"""
2022-02-07 23:58:29 -05:00
import CanneryWeb.Gettext
2021-09-02 23:31:14 -04:00
import Ecto.Query, warn: false
2022-02-07 23:58:29 -05:00
alias Cannery.{Accounts.User, Ammo.AmmoGroup, Repo, Tags.Tag}
2022-01-31 20:10:48 -05:00
alias Cannery.Containers.{Container, ContainerTag}
2022-02-01 00:22:44 -05:00
alias Ecto.Changeset
2021-09-02 23:31:14 -04:00
@doc """
Returns the list of containers.
## Examples
2022-02-11 00:33:51 -05:00
iex> list_containers(%User{id: 123})
2021-09-02 23:31:14 -04:00
[%Container{}, ...]
"""
2022-02-11 00:33:51 -05:00
@spec list_containers(User.t()) :: [Container.t()]
2022-02-18 22:56:46 -05:00
def list_containers(%User{id: user_id}) do
Repo.all(
from c in Container,
left_join: t in assoc(c, :tags),
left_join: ag in assoc(c, :ammo_groups),
where: c.user_id == ^user_id,
order_by: c.name,
preload: [tags: t, ammo_groups: ag]
)
end
2021-09-02 23:31:14 -04:00
@doc """
Gets a single container.
Raises `Ecto.NoResultsError` if the Container does not exist.
## Examples
2022-02-11 00:33:51 -05:00
iex> get_container!(123, %User{id: 123})
2021-09-02 23:31:14 -04:00
%Container{}
2022-02-11 00:33:51 -05:00
iex> get_container!(456, %User{id: 123})
2021-09-02 23:31:14 -04:00
** (Ecto.NoResultsError)
"""
2022-02-11 00:33:51 -05:00
@spec get_container!(Container.id(), User.t()) :: Container.t()
2022-02-18 22:56:46 -05:00
def get_container!(id, %User{id: user_id}) do
Repo.one!(
from c in Container,
left_join: t in assoc(c, :tags),
left_join: ag in assoc(c, :ammo_groups),
where: c.user_id == ^user_id,
where: c.id == ^id,
order_by: c.name,
preload: [tags: t, ammo_groups: ag]
)
end
2021-09-02 23:31:14 -04:00
@doc """
Creates a container.
## Examples
2022-02-11 00:33:51 -05:00
iex> create_container(%{field: value}, %User{id: 123})
2021-09-02 23:31:14 -04:00
{:ok, %Container{}}
2022-02-11 00:33:51 -05:00
iex> create_container(%{field: bad_value}, %User{id: 123})
2022-02-01 00:22:44 -05:00
{:error, %Changeset{}}
2021-09-02 23:31:14 -04:00
"""
2022-02-08 22:10:48 -05:00
@spec create_container(attrs :: map(), User.t()) ::
2022-01-31 23:45:00 -05:00
{:ok, Container.t()} | {:error, Changeset.t(Container.new_container())}
2022-02-08 22:10:48 -05:00
def create_container(attrs, %User{id: user_id}) do
attrs = attrs |> Map.put("user_id", user_id)
%Container{} |> Container.create_changeset(attrs) |> Repo.insert()
2021-09-02 23:31:14 -04:00
end
@doc """
Updates a container.
## Examples
2022-02-11 00:33:51 -05:00
iex> update_container(container, %User{id: 123}, %{field: new_value})
2021-09-02 23:31:14 -04:00
{:ok, %Container{}}
2022-02-11 00:33:51 -05:00
iex> update_container(container, %User{id: 123}, %{field: bad_value})
2022-02-01 00:22:44 -05:00
{:error, %Changeset{}}
2021-09-02 23:31:14 -04:00
"""
2022-02-08 22:10:48 -05:00
@spec update_container(Container.t(), User.t(), attrs :: map()) ::
2022-01-31 23:45:00 -05:00
{:ok, Container.t()} | {:error, Changeset.t(Container.t())}
2022-02-08 22:10:48 -05:00
def update_container(%Container{user_id: user_id} = container, %User{id: user_id}, attrs) do
container |> Container.update_changeset(attrs) |> Repo.update()
2021-09-02 23:31:14 -04:00
end
@doc """
Deletes a container.
## Examples
2022-02-11 00:33:51 -05:00
iex> delete_container(container, %User{id: 123})
2021-09-02 23:31:14 -04:00
{:ok, %Container{}}
2022-02-11 00:33:51 -05:00
iex> delete_container(container, %User{id: 123})
2022-02-01 00:22:44 -05:00
{:error, %Changeset{}}
2021-09-02 23:31:14 -04:00
"""
2022-02-08 22:10:48 -05:00
@spec delete_container(Container.t(), User.t()) ::
2022-01-31 23:45:00 -05:00
{:ok, Container.t()} | {:error, Changeset.t(Container.t())}
2022-02-08 22:10:48 -05:00
def delete_container(%Container{user_id: user_id} = container, %User{id: user_id}) do
2022-02-07 23:58:29 -05:00
Repo.one(
from ag in AmmoGroup,
where: ag.container_id == ^container.id,
select: count(ag.id)
)
|> case do
0 ->
container |> Repo.delete()
2022-02-11 00:33:51 -05:00
_amount ->
error = dgettext("errors", "Container must be empty before deleting")
2022-02-07 23:58:29 -05:00
container
|> change_container()
2022-02-11 00:33:51 -05:00
|> Changeset.add_error(:ammo_groups, error)
2022-02-07 23:58:29 -05:00
|> Changeset.apply_action(:delete)
end
end
2022-01-31 21:42:24 -05:00
@doc """
Deletes a container.
## Examples
2022-02-11 00:33:51 -05:00
iex> delete_container(container, %User{id: 123})
2022-01-31 21:42:24 -05:00
%Container{}
"""
2022-02-08 22:10:48 -05:00
@spec delete_container!(Container.t(), User.t()) :: Container.t()
def delete_container!(container, user) do
{:ok, container} = container |> delete_container(user)
2022-02-07 23:58:29 -05:00
container
end
2021-09-02 23:31:14 -04:00
@doc """
2022-02-01 00:22:44 -05:00
Returns an `%Changeset{}` for tracking container changes.
2021-09-02 23:31:14 -04:00
## Examples
iex> change_container(container)
2022-02-01 00:22:44 -05:00
%Changeset{data: %Container{}}
2021-09-02 23:31:14 -04:00
2022-02-01 00:22:44 -05:00
iex> change_container(%Changeset{})
%Changeset{data: %Container{}}
2022-01-22 21:40:29 -05:00
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-02-08 22:10:48 -05:00
def change_container(container, attrs \\ %{}),
do: container |> Container.update_changeset(attrs)
2022-01-31 20:10:48 -05:00
@doc """
Adds a tag to a container
## Examples
2022-02-11 00:33:51 -05:00
iex> add_tag!(container, tag, %User{id: 123})
2022-01-31 20:10:48 -05:00
%Container{}
"""
2022-02-11 00:33:51 -05:00
@spec add_tag!(Container.t(), Tag.t(), User.t()) :: ContainerTag.t()
def add_tag!(
%Container{id: container_id, user_id: user_id},
%Tag{id: tag_id, user_id: user_id},
%User{id: user_id}
) do
2022-01-31 20:10:48 -05:00
%ContainerTag{}
|> ContainerTag.changeset(%{"container_id" => container_id, "tag_id" => tag_id})
|> Repo.insert!()
end
@doc """
Removes a tag from a container
## Examples
2022-02-11 00:33:51 -05:00
iex> remove_tag!(container, tag, %User{id: 123})
2022-01-31 20:10:48 -05:00
%Container{}
"""
2022-02-11 00:33:51 -05:00
@spec remove_tag!(Container.t(), Tag.t(), User.t()) :: non_neg_integer()
def remove_tag!(
%Container{id: container_id, user_id: user_id},
%Tag{id: tag_id, user_id: user_id},
%User{id: user_id}
) do
{count, _} =
Repo.delete_all(
from ct in ContainerTag,
where: ct.container_id == ^container_id,
2022-02-13 21:14:48 -05:00
where: ct.tag_id == ^tag_id
2022-02-11 00:33:51 -05:00
)
if count == 0, do: raise("could not delete container tag"), else: count
2022-01-31 20:10:48 -05:00
end
2022-02-18 22:56:46 -05:00
@doc """
Returns number of rounds in container. If data is already preloaded, then
there will be no db hit.
"""
@spec get_container_rounds!(Container.t()) :: non_neg_integer()
def get_container_rounds!(%Container{} = container) do
container
|> Repo.preload(:ammo_groups)
2022-03-28 23:05:12 -04:00
|> Map.fetch!(:ammo_groups)
2022-02-18 22:56:46 -05:00
|> Enum.map(fn %{count: count} -> count end)
|> Enum.sum()
end
2021-09-02 23:31:14 -04:00
end