- harden ammo context

- add user_id to ammo types
This commit is contained in:
2022-02-10 00:57:56 -05:00
parent 3b0850852e
commit e9ef44eb53
13 changed files with 325 additions and 242 deletions

View File

@ -13,12 +13,13 @@ defmodule Cannery.Ammo do
## Examples
iex> list_ammo_types()
iex> list_ammo_types(%User{id: 123})
[%AmmoType{}, ...]
"""
@spec list_ammo_types() :: [AmmoType.t()]
def list_ammo_types, do: Repo.all(AmmoType)
@spec list_ammo_types(User.t()) :: [AmmoType.t()]
def list_ammo_types(%User{id: user_id}),
do: Repo.all(from at in AmmoType, where: at.user_id == ^user_id)
@doc """
Gets a single ammo_type.
@ -27,15 +28,16 @@ defmodule Cannery.Ammo do
## Examples
iex> get_ammo_type!(123)
iex> get_ammo_type!(123, %User{id: 123})
%AmmoType{}
iex> get_ammo_type!(456)
iex> get_ammo_type!(456, %User{id: 123})
** (Ecto.NoResultsError)
"""
@spec get_ammo_type!(AmmoType.id()) :: AmmoType.t()
def get_ammo_type!(id), do: Repo.get!(AmmoType, id)
@spec get_ammo_type!(AmmoType.id(), User.t()) :: AmmoType.t()
def get_ammo_type!(id, %User{id: user_id}),
do: Repo.one!(from at in AmmoType, where: at.id == ^id and at.user_id == ^user_id)
@doc """
Gets the average cost of a single ammo type
@ -44,15 +46,18 @@ defmodule Cannery.Ammo do
## Examples
iex> get_ammo_type!(123)
iex> get_ammo_type!(123, %User{id: 123})
%AmmoType{}
iex> get_ammo_type!(456)
iex> get_ammo_type!(456, %User{id: 123})
** (Ecto.NoResultsError)
"""
@spec get_average_cost_for_ammo_type!(AmmoType.t()) :: float()
def get_average_cost_for_ammo_type!(%AmmoType{id: ammo_type_id}) do
@spec get_average_cost_for_ammo_type!(AmmoType.t(), User.t()) :: float()
def get_average_cost_for_ammo_type!(
%AmmoType{id: ammo_type_id, user_id: user_id},
%User{id: user_id}
) do
Repo.one!(
from ag in AmmoGroup,
where: ag.ammo_type_id == ^ammo_type_id,
@ -66,62 +71,67 @@ defmodule Cannery.Ammo do
## Examples
iex> create_ammo_type(%{field: value})
iex> create_ammo_type(%{field: value}, %User{id: 123})
{:ok, %AmmoType{}}
iex> create_ammo_type(%{field: bad_value})
iex> create_ammo_type(%{field: bad_value}, %User{id: 123})
{:error, %Changeset{}}
"""
@spec create_ammo_type(attrs :: map()) ::
@spec create_ammo_type(attrs :: map(), User.t()) ::
{:ok, AmmoType.t()} | {:error, Changeset.t(AmmoType.new_ammo_type())}
def create_ammo_type(attrs \\ %{}),
do: %AmmoType{} |> AmmoType.changeset(attrs) |> Repo.insert()
def create_ammo_type(attrs \\ %{}, %User{id: user_id}) do
%AmmoType{}
|> AmmoType.create_changeset(attrs |> Map.put("user_id", user_id))
|> Repo.insert()
end
@doc """
Updates a ammo_type.
## Examples
iex> update_ammo_type(ammo_type, %{field: new_value})
iex> update_ammo_type(ammo_type, %{field: new_value}, %User{id: 123})
{:ok, %AmmoType{}}
iex> update_ammo_type(ammo_type, %{field: bad_value})
iex> update_ammo_type(ammo_type, %{field: bad_value}, %User{id: 123})
{:error, %Changeset{}}
"""
@spec update_ammo_type(AmmoType.t(), attrs :: map()) ::
@spec update_ammo_type(AmmoType.t(), attrs :: map(), User.t()) ::
{:ok, AmmoType.t()} | {:error, Changeset.t(AmmoType.t())}
def update_ammo_type(%AmmoType{} = ammo_type, attrs),
do: ammo_type |> AmmoType.changeset(attrs) |> Repo.update()
def update_ammo_type(%AmmoType{user_id: user_id} = ammo_type, attrs, %User{id: user_id}),
do: ammo_type |> AmmoType.update_changeset(attrs) |> Repo.update()
@doc """
Deletes a ammo_type.
## Examples
iex> delete_ammo_type(ammo_type)
iex> delete_ammo_type(ammo_type, %User{id: 123})
{:ok, %AmmoType{}}
iex> delete_ammo_type(ammo_type)
iex> delete_ammo_type(ammo_type, %User{id: 123})
{:error, %Changeset{}}
"""
@spec delete_ammo_type(AmmoType.t()) ::
@spec delete_ammo_type(AmmoType.t(), User.t()) ::
{:ok, AmmoType.t()} | {:error, Changeset.t(AmmoType.t())}
def delete_ammo_type(%AmmoType{} = ammo_type), do: ammo_type |> Repo.delete()
def delete_ammo_type(%AmmoType{user_id: user_id} = ammo_type, %User{id: user_id}),
do: ammo_type |> Repo.delete()
@doc """
Deletes a ammo_type.
## Examples
iex> delete_ammo_type(ammo_type)
iex> delete_ammo_type(ammo_type, %User{id: 123})
%AmmoType{}
"""
@spec delete_ammo_type!(AmmoType.t()) :: AmmoType.t()
def delete_ammo_type!(%AmmoType{} = ammo_type), do: ammo_type |> Repo.delete!()
@spec delete_ammo_type!(AmmoType.t(), User.t()) :: AmmoType.t()
def delete_ammo_type!(%AmmoType{user_id: user_id} = ammo_type, %User{id: user_id}),
do: ammo_type |> Repo.delete!()
@doc """
Returns an `%Changeset{}` for tracking ammo_type changes.
@ -137,7 +147,7 @@ defmodule Cannery.Ammo do
@spec change_ammo_type(AmmoType.t() | AmmoType.new_ammo_type(), attrs :: map()) ::
Changeset.t(AmmoType.t() | AmmoType.new_ammo_type())
def change_ammo_type(%AmmoType{} = ammo_type, attrs \\ %{}),
do: AmmoType.changeset(ammo_type, attrs)
do: AmmoType.update_changeset(ammo_type, attrs)
@doc """
Returns the list of ammo_groups for a user and type.
@ -149,7 +159,7 @@ defmodule Cannery.Ammo do
"""
@spec list_ammo_groups_for_type(AmmoType.t(), User.t()) :: [AmmoGroup.t()]
def list_ammo_groups_for_type(%AmmoType{id: ammo_type_id}, %User{id: user_id}) do
def list_ammo_groups_for_type(%AmmoType{id: ammo_type_id, user_id: user_id}, %User{id: user_id}) do
Repo.all(
from am in AmmoGroup,
where: am.ammo_type_id == ^ammo_type_id,
@ -178,77 +188,83 @@ defmodule Cannery.Ammo do
## Examples
iex> get_ammo_group!(123)
iex> get_ammo_group!(123, %User{id: 123})
%AmmoGroup{}
iex> get_ammo_group!(456)
iex> get_ammo_group!(456, %User{id: 123})
** (Ecto.NoResultsError)
"""
@spec get_ammo_group!(AmmoGroup.id()) :: AmmoGroup.t()
def get_ammo_group!(id), do: Repo.get!(AmmoGroup, id)
@spec get_ammo_group!(AmmoGroup.id(), User.t()) :: AmmoGroup.t()
def get_ammo_group!(id, %User{id: user_id}),
do: Repo.one!(from am in AmmoGroup, where: am.id == ^id and am.user_id == ^user_id)
@doc """
Creates a ammo_group.
## Examples
iex> create_ammo_group(%{field: value})
iex> create_ammo_group(%{field: value}, %User{id: 123})
{:ok, %AmmoGroup{}}
iex> create_ammo_group(%{field: bad_value})
iex> create_ammo_group(%{field: bad_value}, %User{id: 123})
{:error, %Changeset{}}
"""
@spec create_ammo_group(attrs :: map()) ::
@spec create_ammo_group(attrs :: map(), User.t()) ::
{:ok, AmmoGroup.t()} | {:error, Changeset.t(AmmoGroup.new_ammo_group())}
def create_ammo_group(attrs \\ %{}),
do: %AmmoGroup{} |> AmmoGroup.changeset(attrs) |> Repo.insert()
def create_ammo_group(attrs \\ %{}, %User{id: user_id}) do
%AmmoGroup{}
|> AmmoGroup.create_changeset(attrs |> Map.put("user_id", user_id))
|> Repo.insert()
end
@doc """
Updates a ammo_group.
## Examples
iex> update_ammo_group(ammo_group, %{field: new_value})
iex> update_ammo_group(ammo_group, %{field: new_value}, %User{id: 123})
{:ok, %AmmoGroup{}}
iex> update_ammo_group(ammo_group, %{field: bad_value})
iex> update_ammo_group(ammo_group, %{field: bad_value}, %User{id: 123})
{:error, %Changeset{}}
"""
@spec update_ammo_group(AmmoGroup.t(), attrs :: map()) ::
@spec update_ammo_group(AmmoGroup.t(), attrs :: map(), User.t()) ::
{:ok, AmmoGroup.t()} | {:error, Changeset.t(AmmoGroup.t())}
def update_ammo_group(%AmmoGroup{} = ammo_group, attrs),
do: ammo_group |> AmmoGroup.changeset(attrs) |> Repo.update()
def update_ammo_group(%AmmoGroup{user_id: user_id} = ammo_group, attrs, %User{id: user_id}),
do: ammo_group |> AmmoGroup.update_changeset(attrs) |> Repo.update()
@doc """
Deletes a ammo_group.
## Examples
iex> delete_ammo_group(ammo_group)
iex> delete_ammo_group(ammo_group, %User{id: 123})
{:ok, %AmmoGroup{}}
iex> delete_ammo_group(ammo_group)
iex> delete_ammo_group(ammo_group, %User{id: 123})
{:error, %Changeset{}}
"""
@spec delete_ammo_group(AmmoGroup.t()) ::
@spec delete_ammo_group(AmmoGroup.t(), User.t()) ::
{:ok, AmmoGroup.t()} | {:error, Changeset.t(AmmoGroup.t())}
def delete_ammo_group(%AmmoGroup{} = ammo_group), do: ammo_group |> Repo.delete()
def delete_ammo_group(%AmmoGroup{user_id: user_id} = ammo_group, %User{id: user_id}),
do: ammo_group |> Repo.delete()
@doc """
Deletes a ammo_group.
## Examples
iex> delete_ammo_group!(ammo_group)
iex> delete_ammo_group!(ammo_group, %User{id: 123})
%AmmoGroup{}
"""
@spec delete_ammo_group!(AmmoGroup.t()) :: AmmoGroup.t()
def delete_ammo_group!(%AmmoGroup{} = ammo_group), do: ammo_group |> Repo.delete!()
@spec delete_ammo_group!(AmmoGroup.t(), User.t()) :: AmmoGroup.t()
def delete_ammo_group!(%AmmoGroup{user_id: user_id} = ammo_group, %User{id: user_id}),
do: ammo_group |> Repo.delete!()
@doc """
Returns an `%Changeset{}` for tracking ammo_group changes.
@ -262,5 +278,5 @@ defmodule Cannery.Ammo do
@spec change_ammo_group(AmmoGroup.t()) :: Changeset.t(AmmoGroup.t())
@spec change_ammo_group(AmmoGroup.t(), attrs :: map()) :: Changeset.t(AmmoGroup.t())
def change_ammo_group(%AmmoGroup{} = ammo_group, attrs \\ %{}),
do: AmmoGroup.changeset(ammo_group, attrs)
do: AmmoGroup.update_changeset(ammo_group, attrs)
end

View File

@ -44,10 +44,11 @@ defmodule Cannery.Ammo.AmmoGroup do
@type id :: UUID.t()
@doc false
@spec changeset(t() | new_ammo_group(), attrs :: map()) :: Changeset.t(t() | new_ammo_group())
def changeset(ammo_group, attrs) do
@spec create_changeset(t() | new_ammo_group(), attrs :: map()) ::
Changeset.t(t() | new_ammo_group())
def create_changeset(ammo_group, attrs) do
ammo_group
|> cast(attrs, [:count, :price_paid, :notes, :tag_id, :ammo_type_id, :container_id, :user_id])
|> cast(attrs, [:count, :price_paid, :notes, :ammo_type_id, :container_id, :user_id])
|> validate_required([
:count,
:ammo_type_id,
@ -55,4 +56,17 @@ defmodule Cannery.Ammo.AmmoGroup do
:user_id
])
end
@doc false
@spec update_changeset(t() | new_ammo_group(), attrs :: map()) ::
Changeset.t(t() | new_ammo_group())
def update_changeset(ammo_group, attrs) do
ammo_group
|> cast(attrs, [:count, :price_paid, :notes, :ammo_type_id, :container_id, :user_id])
|> validate_required([
:count,
:ammo_type_id,
:container_id
])
end
end

View File

@ -7,6 +7,7 @@ defmodule Cannery.Ammo.AmmoType do
use Ecto.Schema
import Ecto.Changeset
alias Cannery.Accounts.User
alias Cannery.Ammo.{AmmoGroup, AmmoType}
alias Ecto.{Changeset, UUID}
@ -34,6 +35,8 @@ defmodule Cannery.Ammo.AmmoType do
field :manufacturer, :string
field :sku, :string
belongs_to :user, User
has_many :ammo_groups, AmmoGroup
timestamps()
@ -58,6 +61,8 @@ defmodule Cannery.Ammo.AmmoType do
corrosive: boolean(),
manufacturer: String.t() | nil,
sku: String.t() | nil,
user_id: User.id(),
user: User.t() | nil,
ammo_groups: [AmmoGroup.t()] | nil,
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
@ -65,11 +70,9 @@ defmodule Cannery.Ammo.AmmoType do
@type new_ammo_type :: %AmmoType{}
@type id :: UUID.t()
@doc false
@spec changeset(t() | new_ammo_type(), attrs :: map()) :: Changeset.t(t() | new_ammo_type())
def changeset(ammo_type, attrs) do
ammo_type
|> cast(attrs, [
@spec changeset_fields() :: [atom()]
defp changeset_fields,
do: [
:name,
:desc,
:bullet_type,
@ -87,7 +90,23 @@ defmodule Cannery.Ammo.AmmoType do
:corrosive,
:manufacturer,
:sku
])
|> validate_required([:name])
]
@doc false
@spec create_changeset(t() | new_ammo_type(), attrs :: map()) ::
Changeset.t(t() | new_ammo_type())
def create_changeset(ammo_type, attrs) do
ammo_type
|> cast(attrs, [:user_id | changeset_fields()])
|> validate_required([:name, :user_id])
end
@doc false
@spec update_changeset(t() | new_ammo_type(), attrs :: map()) ::
Changeset.t(t() | new_ammo_type())
def update_changeset(ammo_type, attrs) do
ammo_type
|> cast(attrs, changeset_fields())
|> validate_required([:name, :user_id])
end
end