rename ammo groups to packs
This commit is contained in:
		| @@ -4,7 +4,7 @@ defmodule Cannery.ActivityLog do | ||||
|   """ | ||||
|  | ||||
|   import Ecto.Query, warn: false | ||||
|   alias Cannery.Ammo.{AmmoGroup, AmmoType} | ||||
|   alias Cannery.Ammo.{Pack, AmmoType} | ||||
|   alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Repo} | ||||
|   alias Ecto.{Multi, Queryable} | ||||
|  | ||||
| @@ -29,9 +29,9 @@ defmodule Cannery.ActivityLog do | ||||
|   def list_shot_groups(search \\ nil, type, %{id: user_id}) do | ||||
|     from(sg in ShotGroup, | ||||
|       as: :sg, | ||||
|       left_join: ag in AmmoGroup, | ||||
|       left_join: ag in Pack, | ||||
|       as: :ag, | ||||
|       on: sg.ammo_group_id == ag.id, | ||||
|       on: sg.pack_id == ag.id, | ||||
|       left_join: at in AmmoType, | ||||
|       as: :at, | ||||
|       on: ag.ammo_type_id == at.id, | ||||
| @@ -92,14 +92,14 @@ defmodule Cannery.ActivityLog do | ||||
|  | ||||
|   defp list_shot_groups_filter_type(query, _all), do: query | ||||
|  | ||||
|   @spec list_shot_groups_for_ammo_group(AmmoGroup.t(), User.t()) :: [ShotGroup.t()] | ||||
|   def list_shot_groups_for_ammo_group( | ||||
|         %AmmoGroup{id: ammo_group_id, user_id: user_id}, | ||||
|   @spec list_shot_groups_for_pack(Pack.t(), User.t()) :: [ShotGroup.t()] | ||||
|   def list_shot_groups_for_pack( | ||||
|         %Pack{id: pack_id, user_id: user_id}, | ||||
|         %User{id: user_id} | ||||
|       ) do | ||||
|     Repo.all( | ||||
|       from sg in ShotGroup, | ||||
|         where: sg.ammo_group_id == ^ammo_group_id, | ||||
|         where: sg.pack_id == ^pack_id, | ||||
|         where: sg.user_id == ^user_id | ||||
|     ) | ||||
|   end | ||||
| @@ -140,31 +140,31 @@ defmodule Cannery.ActivityLog do | ||||
|       {:error, %Ecto.Changeset{}} | ||||
|  | ||||
|   """ | ||||
|   @spec create_shot_group(attrs :: map(), User.t(), AmmoGroup.t()) :: | ||||
|   @spec create_shot_group(attrs :: map(), User.t(), Pack.t()) :: | ||||
|           {:ok, ShotGroup.t()} | {:error, ShotGroup.changeset() | nil} | ||||
|   def create_shot_group(attrs, user, ammo_group) do | ||||
|   def create_shot_group(attrs, user, pack) do | ||||
|     Multi.new() | ||||
|     |> Multi.insert( | ||||
|       :create_shot_group, | ||||
|       %ShotGroup{} |> ShotGroup.create_changeset(user, ammo_group, attrs) | ||||
|       %ShotGroup{} |> ShotGroup.create_changeset(user, pack, attrs) | ||||
|     ) | ||||
|     |> Multi.run( | ||||
|       :ammo_group, | ||||
|       fn _repo, %{create_shot_group: %{ammo_group_id: ammo_group_id, user_id: user_id}} -> | ||||
|         ammo_group = | ||||
|       :pack, | ||||
|       fn _repo, %{create_shot_group: %{pack_id: pack_id, user_id: user_id}} -> | ||||
|         pack = | ||||
|           Repo.one( | ||||
|             from ag in AmmoGroup, | ||||
|               where: ag.id == ^ammo_group_id, | ||||
|             from ag in Pack, | ||||
|               where: ag.id == ^pack_id, | ||||
|               where: ag.user_id == ^user_id | ||||
|           ) | ||||
|  | ||||
|         {:ok, ammo_group} | ||||
|         {:ok, pack} | ||||
|       end | ||||
|     ) | ||||
|     |> Multi.update( | ||||
|       :update_ammo_group, | ||||
|       fn %{create_shot_group: %{count: shot_group_count}, ammo_group: %{count: ammo_group_count}} -> | ||||
|         ammo_group |> AmmoGroup.range_changeset(%{"count" => ammo_group_count - shot_group_count}) | ||||
|       :update_pack, | ||||
|       fn %{create_shot_group: %{count: shot_group_count}, pack: %{count: pack_count}} -> | ||||
|         pack |> Pack.range_changeset(%{"count" => pack_count - shot_group_count}) | ||||
|       end | ||||
|     ) | ||||
|     |> Repo.transaction() | ||||
| @@ -200,21 +200,20 @@ defmodule Cannery.ActivityLog do | ||||
|       shot_group |> ShotGroup.update_changeset(user, attrs) | ||||
|     ) | ||||
|     |> Multi.run( | ||||
|       :ammo_group, | ||||
|       fn repo, %{update_shot_group: %{ammo_group_id: ammo_group_id, user_id: user_id}} -> | ||||
|         {:ok, | ||||
|          repo.one(from ag in AmmoGroup, where: ag.id == ^ammo_group_id and ag.user_id == ^user_id)} | ||||
|       :pack, | ||||
|       fn repo, %{update_shot_group: %{pack_id: pack_id, user_id: user_id}} -> | ||||
|         {:ok, repo.one(from ag in Pack, where: ag.id == ^pack_id and ag.user_id == ^user_id)} | ||||
|       end | ||||
|     ) | ||||
|     |> Multi.update( | ||||
|       :update_ammo_group, | ||||
|       :update_pack, | ||||
|       fn %{ | ||||
|            update_shot_group: %{count: new_count}, | ||||
|            ammo_group: %{count: ammo_group_count} = ammo_group | ||||
|            pack: %{count: pack_count} = pack | ||||
|          } -> | ||||
|         shot_diff_to_add = new_count - count | ||||
|         new_ammo_group_count = ammo_group_count - shot_diff_to_add | ||||
|         ammo_group |> AmmoGroup.range_changeset(%{"count" => new_ammo_group_count}) | ||||
|         new_pack_count = pack_count - shot_diff_to_add | ||||
|         pack |> Pack.range_changeset(%{"count" => new_pack_count}) | ||||
|       end | ||||
|     ) | ||||
|     |> Repo.transaction() | ||||
| @@ -246,20 +245,19 @@ defmodule Cannery.ActivityLog do | ||||
|     Multi.new() | ||||
|     |> Multi.delete(:delete_shot_group, shot_group) | ||||
|     |> Multi.run( | ||||
|       :ammo_group, | ||||
|       fn repo, %{delete_shot_group: %{ammo_group_id: ammo_group_id, user_id: user_id}} -> | ||||
|         {:ok, | ||||
|          repo.one(from ag in AmmoGroup, where: ag.id == ^ammo_group_id and ag.user_id == ^user_id)} | ||||
|       :pack, | ||||
|       fn repo, %{delete_shot_group: %{pack_id: pack_id, user_id: user_id}} -> | ||||
|         {:ok, repo.one(from ag in Pack, where: ag.id == ^pack_id and ag.user_id == ^user_id)} | ||||
|       end | ||||
|     ) | ||||
|     |> Multi.update( | ||||
|       :update_ammo_group, | ||||
|       :update_pack, | ||||
|       fn %{ | ||||
|            delete_shot_group: %{count: count}, | ||||
|            ammo_group: %{count: ammo_group_count} = ammo_group | ||||
|            pack: %{count: pack_count} = pack | ||||
|          } -> | ||||
|         new_ammo_group_count = ammo_group_count + count | ||||
|         ammo_group |> AmmoGroup.range_changeset(%{"count" => new_ammo_group_count}) | ||||
|         new_pack_count = pack_count + count | ||||
|         pack |> Pack.range_changeset(%{"count" => new_pack_count}) | ||||
|       end | ||||
|     ) | ||||
|     |> Repo.transaction() | ||||
| @@ -273,29 +271,29 @@ defmodule Cannery.ActivityLog do | ||||
|   @doc """ | ||||
|   Returns the number of shot rounds for an ammo group | ||||
|   """ | ||||
|   @spec get_used_count(AmmoGroup.t(), User.t()) :: non_neg_integer() | ||||
|   def get_used_count(%AmmoGroup{id: ammo_group_id} = ammo_group, user) do | ||||
|     [ammo_group] | ||||
|   @spec get_used_count(Pack.t(), User.t()) :: non_neg_integer() | ||||
|   def get_used_count(%Pack{id: pack_id} = pack, user) do | ||||
|     [pack] | ||||
|     |> get_used_counts(user) | ||||
|     |> Map.get(ammo_group_id, 0) | ||||
|     |> Map.get(pack_id, 0) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Returns the number of shot rounds for multiple ammo groups | ||||
|   """ | ||||
|   @spec get_used_counts([AmmoGroup.t()], User.t()) :: | ||||
|           %{optional(AmmoGroup.id()) => non_neg_integer()} | ||||
|   def get_used_counts(ammo_groups, %User{id: user_id}) do | ||||
|     ammo_group_ids = | ||||
|       ammo_groups | ||||
|       |> Enum.map(fn %{id: ammo_group_id} -> ammo_group_id end) | ||||
|   @spec get_used_counts([Pack.t()], User.t()) :: | ||||
|           %{optional(Pack.id()) => non_neg_integer()} | ||||
|   def get_used_counts(packs, %User{id: user_id}) do | ||||
|     pack_ids = | ||||
|       packs | ||||
|       |> Enum.map(fn %{id: pack_id} -> pack_id end) | ||||
|  | ||||
|     Repo.all( | ||||
|       from sg in ShotGroup, | ||||
|         where: sg.ammo_group_id in ^ammo_group_ids, | ||||
|         where: sg.pack_id in ^pack_ids, | ||||
|         where: sg.user_id == ^user_id, | ||||
|         group_by: sg.ammo_group_id, | ||||
|         select: {sg.ammo_group_id, sum(sg.count)} | ||||
|         group_by: sg.pack_id, | ||||
|         select: {sg.pack_id, sum(sg.count)} | ||||
|     ) | ||||
|     |> Map.new() | ||||
|   end | ||||
| @@ -303,28 +301,28 @@ defmodule Cannery.ActivityLog do | ||||
|   @doc """ | ||||
|   Returns the last entered shot group date for an ammo group | ||||
|   """ | ||||
|   @spec get_last_used_date(AmmoGroup.t(), User.t()) :: Date.t() | nil | ||||
|   def get_last_used_date(%AmmoGroup{id: ammo_group_id} = ammo_group, user) do | ||||
|     [ammo_group] | ||||
|   @spec get_last_used_date(Pack.t(), User.t()) :: Date.t() | nil | ||||
|   def get_last_used_date(%Pack{id: pack_id} = pack, user) do | ||||
|     [pack] | ||||
|     |> get_last_used_dates(user) | ||||
|     |> Map.get(ammo_group_id) | ||||
|     |> Map.get(pack_id) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Returns the last entered shot group date for an ammo group | ||||
|   """ | ||||
|   @spec get_last_used_dates([AmmoGroup.t()], User.t()) :: %{optional(AmmoGroup.id()) => Date.t()} | ||||
|   def get_last_used_dates(ammo_groups, %User{id: user_id}) do | ||||
|     ammo_group_ids = | ||||
|       ammo_groups | ||||
|       |> Enum.map(fn %AmmoGroup{id: ammo_group_id, user_id: ^user_id} -> ammo_group_id end) | ||||
|   @spec get_last_used_dates([Pack.t()], User.t()) :: %{optional(Pack.id()) => Date.t()} | ||||
|   def get_last_used_dates(packs, %User{id: user_id}) do | ||||
|     pack_ids = | ||||
|       packs | ||||
|       |> Enum.map(fn %Pack{id: pack_id, user_id: ^user_id} -> pack_id end) | ||||
|  | ||||
|     Repo.all( | ||||
|       from sg in ShotGroup, | ||||
|         where: sg.ammo_group_id in ^ammo_group_ids, | ||||
|         where: sg.pack_id in ^pack_ids, | ||||
|         where: sg.user_id == ^user_id, | ||||
|         group_by: sg.ammo_group_id, | ||||
|         select: {sg.ammo_group_id, max(sg.date)} | ||||
|         group_by: sg.pack_id, | ||||
|         select: {sg.pack_id, max(sg.date)} | ||||
|     ) | ||||
|     |> Map.new() | ||||
|   end | ||||
| @@ -367,9 +365,9 @@ defmodule Cannery.ActivityLog do | ||||
|       |> Enum.map(fn %AmmoType{id: ammo_type_id, user_id: ^user_id} -> ammo_type_id end) | ||||
|  | ||||
|     Repo.all( | ||||
|       from ag in AmmoGroup, | ||||
|       from ag in Pack, | ||||
|         left_join: sg in ShotGroup, | ||||
|         on: ag.id == sg.ammo_group_id, | ||||
|         on: ag.id == sg.pack_id, | ||||
|         where: ag.ammo_type_id in ^ammo_type_ids, | ||||
|         where: not (sg.count |> is_nil()), | ||||
|         group_by: ag.ammo_type_id, | ||||
|   | ||||
| @@ -6,7 +6,7 @@ defmodule Cannery.ActivityLog.ShotGroup do | ||||
|   use Ecto.Schema | ||||
|   import CanneryWeb.Gettext | ||||
|   import Ecto.Changeset | ||||
|   alias Cannery.{Accounts.User, Ammo, Ammo.AmmoGroup} | ||||
|   alias Cannery.{Accounts.User, Ammo, Ammo.Pack} | ||||
|   alias Ecto.{Changeset, UUID} | ||||
|  | ||||
|   @derive {Jason.Encoder, | ||||
| @@ -15,7 +15,7 @@ defmodule Cannery.ActivityLog.ShotGroup do | ||||
|              :count, | ||||
|              :date, | ||||
|              :notes, | ||||
|              :ammo_group_id | ||||
|              :pack_id | ||||
|            ]} | ||||
|   @primary_key {:id, :binary_id, autogenerate: true} | ||||
|   @foreign_key_type :binary_id | ||||
| @@ -25,7 +25,7 @@ defmodule Cannery.ActivityLog.ShotGroup do | ||||
|     field :notes, :string | ||||
|  | ||||
|     field :user_id, :binary_id | ||||
|     field :ammo_group_id, :binary_id | ||||
|     field :pack_id, :binary_id | ||||
|  | ||||
|     timestamps() | ||||
|   end | ||||
| @@ -35,7 +35,7 @@ defmodule Cannery.ActivityLog.ShotGroup do | ||||
|           count: integer, | ||||
|           notes: String.t() | nil, | ||||
|           date: Date.t() | nil, | ||||
|           ammo_group_id: AmmoGroup.id(), | ||||
|           pack_id: Pack.id(), | ||||
|           user_id: User.id(), | ||||
|           inserted_at: NaiveDateTime.t(), | ||||
|           updated_at: NaiveDateTime.t() | ||||
| @@ -48,46 +48,44 @@ defmodule Cannery.ActivityLog.ShotGroup do | ||||
|   @spec create_changeset( | ||||
|           new_shot_group(), | ||||
|           User.t() | any(), | ||||
|           AmmoGroup.t() | any(), | ||||
|           Pack.t() | any(), | ||||
|           attrs :: map() | ||||
|         ) :: changeset() | ||||
|   def create_changeset( | ||||
|         shot_group, | ||||
|         %User{id: user_id}, | ||||
|         %AmmoGroup{id: ammo_group_id, user_id: user_id} = ammo_group, | ||||
|         %Pack{id: pack_id, user_id: user_id} = pack, | ||||
|         attrs | ||||
|       ) do | ||||
|     shot_group | ||||
|     |> change(user_id: user_id) | ||||
|     |> change(ammo_group_id: ammo_group_id) | ||||
|     |> change(pack_id: pack_id) | ||||
|     |> cast(attrs, [:count, :notes, :date]) | ||||
|     |> validate_length(:notes, max: 255) | ||||
|     |> validate_create_shot_group_count(ammo_group) | ||||
|     |> validate_required([:date, :ammo_group_id, :user_id]) | ||||
|     |> validate_create_shot_group_count(pack) | ||||
|     |> validate_required([:date, :pack_id, :user_id]) | ||||
|   end | ||||
|  | ||||
|   def create_changeset(shot_group, _invalid_user, _invalid_ammo_group, attrs) do | ||||
|   def create_changeset(shot_group, _invalid_user, _invalid_pack, attrs) do | ||||
|     shot_group | ||||
|     |> cast(attrs, [:count, :notes, :date]) | ||||
|     |> validate_length(:notes, max: 255) | ||||
|     |> validate_required([:ammo_group_id, :user_id]) | ||||
|     |> validate_required([:pack_id, :user_id]) | ||||
|     |> add_error(:invalid, dgettext("errors", "Please select a valid user and ammo pack")) | ||||
|   end | ||||
|  | ||||
|   defp validate_create_shot_group_count(changeset, %AmmoGroup{count: ammo_group_count}) do | ||||
|   defp validate_create_shot_group_count(changeset, %Pack{count: pack_count}) do | ||||
|     case changeset |> Changeset.get_field(:count) do | ||||
|       nil -> | ||||
|         changeset |> Changeset.add_error(:ammo_left, dgettext("errors", "can't be blank")) | ||||
|  | ||||
|       count when count > ammo_group_count -> | ||||
|       count when count > pack_count -> | ||||
|         changeset | ||||
|         |> Changeset.add_error(:ammo_left, dgettext("errors", "Ammo left must be at least 0")) | ||||
|  | ||||
|       count when count <= 0 -> | ||||
|         error = | ||||
|           dgettext("errors", "Ammo left can be at most %{count} rounds", | ||||
|             count: ammo_group_count - 1 | ||||
|           ) | ||||
|           dgettext("errors", "Ammo left can be at most %{count} rounds", count: pack_count - 1) | ||||
|  | ||||
|         changeset |> Changeset.add_error(:ammo_left, error) | ||||
|  | ||||
| @@ -109,17 +107,16 @@ defmodule Cannery.ActivityLog.ShotGroup do | ||||
|  | ||||
|   defp validate_update_shot_group_count( | ||||
|          changeset, | ||||
|          %__MODULE__{ammo_group_id: ammo_group_id, count: count}, | ||||
|          %__MODULE__{pack_id: pack_id, count: count}, | ||||
|          user | ||||
|        ) do | ||||
|     %{count: ammo_group_count} = Ammo.get_ammo_group!(ammo_group_id, user) | ||||
|     %{count: pack_count} = Ammo.get_pack!(pack_id, user) | ||||
|  | ||||
|     new_shot_group_count = changeset |> Changeset.get_field(:count) | ||||
|     shot_diff_to_add = new_shot_group_count - count | ||||
|  | ||||
|     if shot_diff_to_add > ammo_group_count do | ||||
|       error = | ||||
|         dgettext("errors", "Count can be at most %{count} shots", count: ammo_group_count + count) | ||||
|     if shot_diff_to_add > pack_count do | ||||
|       error = dgettext("errors", "Count can be at most %{count} shots", count: pack_count + count) | ||||
|  | ||||
|       changeset |> Changeset.add_error(:count, error) | ||||
|     else | ||||
|   | ||||
| @@ -8,12 +8,12 @@ defmodule Cannery.Ammo do | ||||
|   alias Cannery.{Accounts.User, Containers, Repo} | ||||
|   alias Cannery.Containers.{Container, ContainerTag, Tag} | ||||
|   alias Cannery.{ActivityLog, ActivityLog.ShotGroup} | ||||
|   alias Cannery.Ammo.{AmmoGroup, AmmoType} | ||||
|   alias Cannery.Ammo.{Pack, AmmoType} | ||||
|   alias Ecto.{Changeset, Queryable} | ||||
|  | ||||
|   @ammo_group_create_limit 10_000 | ||||
|   @ammo_group_preloads [:ammo_type] | ||||
|   @ammo_type_preloads [:ammo_groups] | ||||
|   @pack_create_limit 10_000 | ||||
|   @pack_preloads [:ammo_type] | ||||
|   @ammo_type_preloads [:packs] | ||||
|  | ||||
|   @doc """ | ||||
|   Returns the list of ammo_types. | ||||
| @@ -169,14 +169,14 @@ defmodule Cannery.Ammo do | ||||
|     sg_total_query = | ||||
|       from sg in ShotGroup, | ||||
|         where: not (sg.count |> is_nil()), | ||||
|         group_by: sg.ammo_group_id, | ||||
|         select: %{ammo_group_id: sg.ammo_group_id, total: sum(sg.count)} | ||||
|         group_by: sg.pack_id, | ||||
|         select: %{pack_id: sg.pack_id, total: sum(sg.count)} | ||||
|  | ||||
|     Repo.all( | ||||
|       from ag in AmmoGroup, | ||||
|         as: :ammo_group, | ||||
|       from ag in Pack, | ||||
|         as: :pack, | ||||
|         left_join: sg_query in subquery(sg_total_query), | ||||
|         on: ag.id == sg_query.ammo_group_id, | ||||
|         on: ag.id == sg_query.pack_id, | ||||
|         where: ag.ammo_type_id in ^ammo_type_ids, | ||||
|         group_by: ag.ammo_type_id, | ||||
|         where: not (ag.price_paid |> is_nil()), | ||||
| @@ -225,7 +225,7 @@ defmodule Cannery.Ammo do | ||||
|       |> Enum.map(fn %AmmoType{id: ammo_type_id, user_id: ^user_id} -> ammo_type_id end) | ||||
|  | ||||
|     Repo.all( | ||||
|       from ag in AmmoGroup, | ||||
|       from ag in Pack, | ||||
|         where: ag.ammo_type_id in ^ammo_type_ids, | ||||
|         where: ag.user_id == ^user_id, | ||||
|         group_by: ag.ammo_type_id, | ||||
| @@ -376,150 +376,150 @@ defmodule Cannery.Ammo do | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Returns the list of ammo_groups for a user and type. | ||||
|   Returns the list of packs for a user and type. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> list_ammo_groups_for_type( | ||||
|       iex> list_packs_for_type( | ||||
|       ...>   %AmmoType{id: 123, user_id: 456}, | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       [%AmmoGroup{}, ...] | ||||
|       [%Pack{}, ...] | ||||
|  | ||||
|       iex> list_ammo_groups_for_type( | ||||
|       iex> list_packs_for_type( | ||||
|       ...>   %AmmoType{id: 123, user_id: 456}, | ||||
|       ...>   %User{id: 456}, | ||||
|       ...>   true | ||||
|       ...> ) | ||||
|       [%AmmoGroup{}, %AmmoGroup{}, ...] | ||||
|       [%Pack{}, %Pack{}, ...] | ||||
|  | ||||
|   """ | ||||
|   @spec list_ammo_groups_for_type(AmmoType.t(), User.t()) :: [AmmoGroup.t()] | ||||
|   @spec list_ammo_groups_for_type(AmmoType.t(), User.t(), show_used :: boolean()) :: | ||||
|           [AmmoGroup.t()] | ||||
|   def list_ammo_groups_for_type(ammo_type, user, show_used \\ false) | ||||
|   @spec list_packs_for_type(AmmoType.t(), User.t()) :: [Pack.t()] | ||||
|   @spec list_packs_for_type(AmmoType.t(), User.t(), show_used :: boolean()) :: | ||||
|           [Pack.t()] | ||||
|   def list_packs_for_type(ammo_type, user, show_used \\ false) | ||||
|  | ||||
|   def list_ammo_groups_for_type( | ||||
|   def list_packs_for_type( | ||||
|         %AmmoType{id: ammo_type_id, user_id: user_id}, | ||||
|         %User{id: user_id}, | ||||
|         show_used | ||||
|       ) do | ||||
|     from(ag in AmmoGroup, | ||||
|     from(ag in Pack, | ||||
|       as: :ag, | ||||
|       where: ag.ammo_type_id == ^ammo_type_id, | ||||
|       where: ag.user_id == ^user_id, | ||||
|       preload: ^@ammo_group_preloads | ||||
|       preload: ^@pack_preloads | ||||
|     ) | ||||
|     |> list_ammo_groups_for_type_show_used(show_used) | ||||
|     |> list_packs_for_type_show_used(show_used) | ||||
|     |> Repo.all() | ||||
|   end | ||||
|  | ||||
|   @spec list_ammo_groups_for_type_show_used(Queryable.t(), show_used :: boolean()) :: | ||||
|   @spec list_packs_for_type_show_used(Queryable.t(), show_used :: boolean()) :: | ||||
|           Queryable.t() | ||||
|   def list_ammo_groups_for_type_show_used(query, false), | ||||
|   def list_packs_for_type_show_used(query, false), | ||||
|     do: query |> where([ag: ag], ag.count > 0) | ||||
|  | ||||
|   def list_ammo_groups_for_type_show_used(query, _true), do: query | ||||
|   def list_packs_for_type_show_used(query, _true), do: query | ||||
|  | ||||
|   @doc """ | ||||
|   Returns the list of ammo_groups for a user and container. | ||||
|   Returns the list of packs for a user and container. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> list_ammo_groups_for_container( | ||||
|       iex> list_packs_for_container( | ||||
|       ...>   %Container{id: 123, user_id: 456}, | ||||
|       ...>   :rifle, | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       [%AmmoGroup{}, ...] | ||||
|       [%Pack{}, ...] | ||||
|  | ||||
|       iex> list_ammo_groups_for_container( | ||||
|       iex> list_packs_for_container( | ||||
|       ...>   %Container{id: 123, user_id: 456}, | ||||
|       ...>   :all, | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       [%AmmoGroup{}, %AmmoGroup{}, ...] | ||||
|       [%Pack{}, %Pack{}, ...] | ||||
|  | ||||
|   """ | ||||
|   @spec list_ammo_groups_for_container( | ||||
|   @spec list_packs_for_container( | ||||
|           Container.t(), | ||||
|           AmmoType.t() | :all, | ||||
|           User.t() | ||||
|         ) :: [AmmoGroup.t()] | ||||
|   def list_ammo_groups_for_container( | ||||
|         ) :: [Pack.t()] | ||||
|   def list_packs_for_container( | ||||
|         %Container{id: container_id, user_id: user_id}, | ||||
|         type, | ||||
|         %User{id: user_id} | ||||
|       ) do | ||||
|     from(ag in AmmoGroup, | ||||
|     from(ag in Pack, | ||||
|       as: :ag, | ||||
|       join: at in assoc(ag, :ammo_type), | ||||
|       as: :at, | ||||
|       where: ag.container_id == ^container_id, | ||||
|       where: ag.user_id == ^user_id, | ||||
|       where: ag.count > 0, | ||||
|       preload: ^@ammo_group_preloads | ||||
|       preload: ^@pack_preloads | ||||
|     ) | ||||
|     |> list_ammo_groups_for_container_filter_type(type) | ||||
|     |> list_packs_for_container_filter_type(type) | ||||
|     |> Repo.all() | ||||
|   end | ||||
|  | ||||
|   @spec list_ammo_groups_for_container_filter_type(Queryable.t(), AmmoType.class() | :all) :: | ||||
|   @spec list_packs_for_container_filter_type(Queryable.t(), AmmoType.class() | :all) :: | ||||
|           Queryable.t() | ||||
|   defp list_ammo_groups_for_container_filter_type(query, :rifle), | ||||
|   defp list_packs_for_container_filter_type(query, :rifle), | ||||
|     do: query |> where([at: at], at.class == :rifle) | ||||
|  | ||||
|   defp list_ammo_groups_for_container_filter_type(query, :pistol), | ||||
|   defp list_packs_for_container_filter_type(query, :pistol), | ||||
|     do: query |> where([at: at], at.class == :pistol) | ||||
|  | ||||
|   defp list_ammo_groups_for_container_filter_type(query, :shotgun), | ||||
|   defp list_packs_for_container_filter_type(query, :shotgun), | ||||
|     do: query |> where([at: at], at.class == :shotgun) | ||||
|  | ||||
|   defp list_ammo_groups_for_container_filter_type(query, _all), do: query | ||||
|   defp list_packs_for_container_filter_type(query, _all), do: query | ||||
|  | ||||
|   @doc """ | ||||
|   Returns a count of ammo_groups. | ||||
|   Returns a count of packs. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_ammo_groups_count!(%User{id: 123}) | ||||
|       iex> get_packs_count!(%User{id: 123}) | ||||
|       3 | ||||
|  | ||||
|       iex> get_ammo_groups_count!(%User{id: 123}, true) | ||||
|       iex> get_packs_count!(%User{id: 123}, true) | ||||
|       4 | ||||
|  | ||||
|   """ | ||||
|   @spec get_ammo_groups_count!(User.t()) :: integer() | ||||
|   @spec get_ammo_groups_count!(User.t(), show_used :: boolean()) :: integer() | ||||
|   def get_ammo_groups_count!(%User{id: user_id}, show_used \\ false) do | ||||
|     from(ag in AmmoGroup, | ||||
|   @spec get_packs_count!(User.t()) :: integer() | ||||
|   @spec get_packs_count!(User.t(), show_used :: boolean()) :: integer() | ||||
|   def get_packs_count!(%User{id: user_id}, show_used \\ false) do | ||||
|     from(ag in Pack, | ||||
|       as: :ag, | ||||
|       where: ag.user_id == ^user_id, | ||||
|       select: count(ag.id), | ||||
|       distinct: true | ||||
|     ) | ||||
|     |> get_ammo_groups_count_show_used(show_used) | ||||
|     |> get_packs_count_show_used(show_used) | ||||
|     |> Repo.one() || 0 | ||||
|   end | ||||
|  | ||||
|   @spec get_ammo_groups_count_show_used(Queryable.t(), show_used :: boolean()) :: Queryable.t() | ||||
|   defp get_ammo_groups_count_show_used(query, false), | ||||
|   @spec get_packs_count_show_used(Queryable.t(), show_used :: boolean()) :: Queryable.t() | ||||
|   defp get_packs_count_show_used(query, false), | ||||
|     do: query |> where([ag: ag], ag.count > 0) | ||||
|  | ||||
|   defp get_ammo_groups_count_show_used(query, _true), do: query | ||||
|   defp get_packs_count_show_used(query, _true), do: query | ||||
|  | ||||
|   @doc """ | ||||
|   Returns the count of ammo_groups for an ammo type. | ||||
|   Returns the count of packs for an ammo type. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_ammo_groups_count_for_type( | ||||
|       iex> get_packs_count_for_type( | ||||
|       ...>   %AmmoType{id: 123, user_id: 456}, | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       3 | ||||
|  | ||||
|       iex> get_ammo_groups_count_for_type( | ||||
|       iex> get_packs_count_for_type( | ||||
|       ...>   %AmmoType{id: 123, user_id: 456}, | ||||
|       ...>   %User{id: 456}, | ||||
|       ...>   true | ||||
| @@ -527,31 +527,31 @@ defmodule Cannery.Ammo do | ||||
|       5 | ||||
|  | ||||
|   """ | ||||
|   @spec get_ammo_groups_count_for_type(AmmoType.t(), User.t()) :: non_neg_integer() | ||||
|   @spec get_ammo_groups_count_for_type(AmmoType.t(), User.t(), show_used :: boolean()) :: | ||||
|   @spec get_packs_count_for_type(AmmoType.t(), User.t()) :: non_neg_integer() | ||||
|   @spec get_packs_count_for_type(AmmoType.t(), User.t(), show_used :: boolean()) :: | ||||
|           non_neg_integer() | ||||
|   def get_ammo_groups_count_for_type( | ||||
|   def get_packs_count_for_type( | ||||
|         %AmmoType{id: ammo_type_id} = ammo_type, | ||||
|         user, | ||||
|         show_used \\ false | ||||
|       ) do | ||||
|     [ammo_type] | ||||
|     |> get_ammo_groups_count_for_types(user, show_used) | ||||
|     |> get_packs_count_for_types(user, show_used) | ||||
|     |> Map.get(ammo_type_id, 0) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Returns the count of ammo_groups for multiple ammo types. | ||||
|   Returns the count of packs for multiple ammo types. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_ammo_groups_count_for_types( | ||||
|       iex> get_packs_count_for_types( | ||||
|       ...>   [%AmmoType{id: 123, user_id: 456}], | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       3 | ||||
|  | ||||
|       iex> get_ammo_groups_count_for_types( | ||||
|       iex> get_packs_count_for_types( | ||||
|       ...>   [%AmmoType{id: 123, user_id: 456}], | ||||
|       ...>   %User{id: 456}, | ||||
|       ...>   true | ||||
| @@ -559,75 +559,75 @@ defmodule Cannery.Ammo do | ||||
|       5 | ||||
|  | ||||
|   """ | ||||
|   @spec get_ammo_groups_count_for_types([AmmoType.t()], User.t()) :: | ||||
|   @spec get_packs_count_for_types([AmmoType.t()], User.t()) :: | ||||
|           %{optional(AmmoType.id()) => non_neg_integer()} | ||||
|   @spec get_ammo_groups_count_for_types([AmmoType.t()], User.t(), show_used :: boolean()) :: | ||||
|   @spec get_packs_count_for_types([AmmoType.t()], User.t(), show_used :: boolean()) :: | ||||
|           %{optional(AmmoType.id()) => non_neg_integer()} | ||||
|   def get_ammo_groups_count_for_types(ammo_types, %User{id: user_id}, show_used \\ false) do | ||||
|   def get_packs_count_for_types(ammo_types, %User{id: user_id}, show_used \\ false) do | ||||
|     ammo_type_ids = | ||||
|       ammo_types | ||||
|       |> Enum.map(fn %AmmoType{id: ammo_type_id, user_id: ^user_id} -> ammo_type_id end) | ||||
|  | ||||
|     from(ag in AmmoGroup, | ||||
|     from(ag in Pack, | ||||
|       as: :ag, | ||||
|       where: ag.user_id == ^user_id, | ||||
|       where: ag.ammo_type_id in ^ammo_type_ids, | ||||
|       group_by: ag.ammo_type_id, | ||||
|       select: {ag.ammo_type_id, count(ag.id)} | ||||
|     ) | ||||
|     |> get_ammo_groups_count_for_types_maybe_show_used(show_used) | ||||
|     |> get_packs_count_for_types_maybe_show_used(show_used) | ||||
|     |> Repo.all() | ||||
|     |> Map.new() | ||||
|   end | ||||
|  | ||||
|   @spec get_ammo_groups_count_for_types_maybe_show_used(Queryable.t(), show_used :: boolean()) :: | ||||
|   @spec get_packs_count_for_types_maybe_show_used(Queryable.t(), show_used :: boolean()) :: | ||||
|           Queryable.t() | ||||
|   defp get_ammo_groups_count_for_types_maybe_show_used(query, true), do: query | ||||
|   defp get_packs_count_for_types_maybe_show_used(query, true), do: query | ||||
|  | ||||
|   defp get_ammo_groups_count_for_types_maybe_show_used(query, _false) do | ||||
|   defp get_packs_count_for_types_maybe_show_used(query, _false) do | ||||
|     query |> where([ag: ag], not (ag.count == 0)) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Returns the count of used ammo_groups for an ammo type. | ||||
|   Returns the count of used packs for an ammo type. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_used_ammo_groups_count_for_type( | ||||
|       iex> get_used_packs_count_for_type( | ||||
|       ...>   %AmmoType{id: 123, user_id: 456}, | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       3 | ||||
|  | ||||
|   """ | ||||
|   @spec get_used_ammo_groups_count_for_type(AmmoType.t(), User.t()) :: non_neg_integer() | ||||
|   def get_used_ammo_groups_count_for_type(%AmmoType{id: ammo_type_id} = ammo_type, user) do | ||||
|   @spec get_used_packs_count_for_type(AmmoType.t(), User.t()) :: non_neg_integer() | ||||
|   def get_used_packs_count_for_type(%AmmoType{id: ammo_type_id} = ammo_type, user) do | ||||
|     [ammo_type] | ||||
|     |> get_used_ammo_groups_count_for_types(user) | ||||
|     |> get_used_packs_count_for_types(user) | ||||
|     |> Map.get(ammo_type_id, 0) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Returns the count of used ammo_groups for multiple ammo types. | ||||
|   Returns the count of used packs for multiple ammo types. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_used_ammo_groups_count_for_types( | ||||
|       iex> get_used_packs_count_for_types( | ||||
|       ...>   [%AmmoType{id: 123, user_id: 456}], | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       %{123 => 3} | ||||
|  | ||||
|   """ | ||||
|   @spec get_used_ammo_groups_count_for_types([AmmoType.t()], User.t()) :: | ||||
|   @spec get_used_packs_count_for_types([AmmoType.t()], User.t()) :: | ||||
|           %{optional(AmmoType.id()) => non_neg_integer()} | ||||
|   def get_used_ammo_groups_count_for_types(ammo_types, %User{id: user_id}) do | ||||
|   def get_used_packs_count_for_types(ammo_types, %User{id: user_id}) do | ||||
|     ammo_type_ids = | ||||
|       ammo_types | ||||
|       |> Enum.map(fn %AmmoType{id: ammo_type_id, user_id: ^user_id} -> ammo_type_id end) | ||||
|  | ||||
|     Repo.all( | ||||
|       from ag in AmmoGroup, | ||||
|       from ag in Pack, | ||||
|         where: ag.user_id == ^user_id, | ||||
|         where: ag.ammo_type_id in ^ammo_type_ids, | ||||
|         where: ag.count == 0, | ||||
| @@ -642,20 +642,20 @@ defmodule Cannery.Ammo do | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_ammo_groups_count_for_container( | ||||
|       iex> get_packs_count_for_container( | ||||
|       ...>   %Container{id: 123, user_id: 456}, | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       3 | ||||
|  | ||||
|   """ | ||||
|   @spec get_ammo_groups_count_for_container!(Container.t(), User.t()) :: non_neg_integer() | ||||
|   def get_ammo_groups_count_for_container!( | ||||
|   @spec get_packs_count_for_container!(Container.t(), User.t()) :: non_neg_integer() | ||||
|   def get_packs_count_for_container!( | ||||
|         %Container{id: container_id} = container, | ||||
|         %User{} = user | ||||
|       ) do | ||||
|     [container] | ||||
|     |> get_ammo_groups_count_for_containers(user) | ||||
|     |> get_packs_count_for_containers(user) | ||||
|     |> Map.get(container_id, 0) | ||||
|   end | ||||
|  | ||||
| @@ -664,23 +664,23 @@ defmodule Cannery.Ammo do | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_ammo_groups_count_for_containers( | ||||
|       iex> get_packs_count_for_containers( | ||||
|       ...>   [%Container{id: 123, user_id: 456}], | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       %{123 => 3} | ||||
|  | ||||
|   """ | ||||
|   @spec get_ammo_groups_count_for_containers([Container.t()], User.t()) :: %{ | ||||
|   @spec get_packs_count_for_containers([Container.t()], User.t()) :: %{ | ||||
|           Container.id() => non_neg_integer() | ||||
|         } | ||||
|   def get_ammo_groups_count_for_containers(containers, %User{id: user_id}) do | ||||
|   def get_packs_count_for_containers(containers, %User{id: user_id}) do | ||||
|     container_ids = | ||||
|       containers | ||||
|       |> Enum.map(fn %Container{id: container_id, user_id: ^user_id} -> container_id end) | ||||
|  | ||||
|     Repo.all( | ||||
|       from ag in AmmoGroup, | ||||
|       from ag in Pack, | ||||
|         where: ag.container_id in ^container_ids, | ||||
|         where: ag.count > 0, | ||||
|         group_by: ag.container_id, | ||||
| @@ -728,7 +728,7 @@ defmodule Cannery.Ammo do | ||||
|       |> Enum.map(fn %Container{id: container_id, user_id: ^user_id} -> container_id end) | ||||
|  | ||||
|     Repo.all( | ||||
|       from ag in AmmoGroup, | ||||
|       from ag in Pack, | ||||
|         where: ag.container_id in ^container_ids, | ||||
|         group_by: ag.container_id, | ||||
|         select: {ag.container_id, sum(ag.count)} | ||||
| @@ -737,27 +737,27 @@ defmodule Cannery.Ammo do | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Returns the list of ammo_groups. | ||||
|   Returns the list of packs. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> list_ammo_groups(%User{id: 123}) | ||||
|       [%AmmoGroup{}, ...] | ||||
|       iex> list_packs(%User{id: 123}) | ||||
|       [%Pack{}, ...] | ||||
|  | ||||
|       iex> list_ammo_groups("cool", %User{id: 123}, true) | ||||
|       [%AmmoGroup{notes: "My cool ammo group"}, ...] | ||||
|       iex> list_packs("cool", %User{id: 123}, true) | ||||
|       [%Pack{notes: "My cool ammo group"}, ...] | ||||
|  | ||||
|   """ | ||||
|   @spec list_ammo_groups(search :: String.t() | nil, AmmoType.class() | :all, User.t()) :: | ||||
|           [AmmoGroup.t()] | ||||
|   @spec list_ammo_groups( | ||||
|   @spec list_packs(search :: String.t() | nil, AmmoType.class() | :all, User.t()) :: | ||||
|           [Pack.t()] | ||||
|   @spec list_packs( | ||||
|           search :: nil | String.t(), | ||||
|           AmmoType.class() | :all, | ||||
|           User.t(), | ||||
|           show_used :: boolean() | ||||
|         ) :: [AmmoGroup.t()] | ||||
|   def list_ammo_groups(search, type, %{id: user_id}, show_used \\ false) do | ||||
|     from(ag in AmmoGroup, | ||||
|         ) :: [Pack.t()] | ||||
|   def list_packs(search, type, %{id: user_id}, show_used \\ false) do | ||||
|     from(ag in Pack, | ||||
|       as: :ag, | ||||
|       join: at in assoc(ag, :ammo_type), | ||||
|       as: :at, | ||||
| @@ -773,38 +773,38 @@ defmodule Cannery.Ammo do | ||||
|       as: :t, | ||||
|       where: ag.user_id == ^user_id, | ||||
|       distinct: ag.id, | ||||
|       preload: ^@ammo_group_preloads | ||||
|       preload: ^@pack_preloads | ||||
|     ) | ||||
|     |> list_ammo_groups_filter_on_type(type) | ||||
|     |> list_ammo_groups_show_used(show_used) | ||||
|     |> list_ammo_groups_search(search) | ||||
|     |> list_packs_filter_on_type(type) | ||||
|     |> list_packs_show_used(show_used) | ||||
|     |> list_packs_search(search) | ||||
|     |> Repo.all() | ||||
|   end | ||||
|  | ||||
|   @spec list_ammo_groups_filter_on_type(Queryable.t(), AmmoType.class() | :all) :: Queryable.t() | ||||
|   defp list_ammo_groups_filter_on_type(query, :rifle), | ||||
|   @spec list_packs_filter_on_type(Queryable.t(), AmmoType.class() | :all) :: Queryable.t() | ||||
|   defp list_packs_filter_on_type(query, :rifle), | ||||
|     do: query |> where([at: at], at.class == :rifle) | ||||
|  | ||||
|   defp list_ammo_groups_filter_on_type(query, :pistol), | ||||
|   defp list_packs_filter_on_type(query, :pistol), | ||||
|     do: query |> where([at: at], at.class == :pistol) | ||||
|  | ||||
|   defp list_ammo_groups_filter_on_type(query, :shotgun), | ||||
|   defp list_packs_filter_on_type(query, :shotgun), | ||||
|     do: query |> where([at: at], at.class == :shotgun) | ||||
|  | ||||
|   defp list_ammo_groups_filter_on_type(query, _all), do: query | ||||
|   defp list_packs_filter_on_type(query, _all), do: query | ||||
|  | ||||
|   @spec list_ammo_groups_show_used(Queryable.t(), show_used :: boolean()) :: Queryable.t() | ||||
|   defp list_ammo_groups_show_used(query, true), do: query | ||||
|   @spec list_packs_show_used(Queryable.t(), show_used :: boolean()) :: Queryable.t() | ||||
|   defp list_packs_show_used(query, true), do: query | ||||
|  | ||||
|   defp list_ammo_groups_show_used(query, _false) do | ||||
|   defp list_packs_show_used(query, _false) do | ||||
|     query |> where([ag: ag], not (ag.count == 0)) | ||||
|   end | ||||
|  | ||||
|   @spec list_ammo_groups_show_used(Queryable.t(), search :: String.t() | nil) :: Queryable.t() | ||||
|   defp list_ammo_groups_search(query, nil), do: query | ||||
|   defp list_ammo_groups_search(query, ""), do: query | ||||
|   @spec list_packs_show_used(Queryable.t(), search :: String.t() | nil) :: Queryable.t() | ||||
|   defp list_packs_search(query, nil), do: query | ||||
|   defp list_packs_search(query, ""), do: query | ||||
|  | ||||
|   defp list_ammo_groups_search(query, search) do | ||||
|   defp list_packs_search(query, search) do | ||||
|     trimmed_search = String.trim(search) | ||||
|  | ||||
|     query | ||||
| @@ -843,60 +843,60 @@ defmodule Cannery.Ammo do | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Returns the list of staged ammo_groups for a user. | ||||
|   Returns the list of staged packs for a user. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> list_staged_ammo_groups(%User{id: 123}) | ||||
|       [%AmmoGroup{}, ...] | ||||
|       iex> list_staged_packs(%User{id: 123}) | ||||
|       [%Pack{}, ...] | ||||
|  | ||||
|   """ | ||||
|   @spec list_staged_ammo_groups(User.t()) :: [AmmoGroup.t()] | ||||
|   def list_staged_ammo_groups(%User{id: user_id}) do | ||||
|   @spec list_staged_packs(User.t()) :: [Pack.t()] | ||||
|   def list_staged_packs(%User{id: user_id}) do | ||||
|     Repo.all( | ||||
|       from ag in AmmoGroup, | ||||
|       from ag in Pack, | ||||
|         where: ag.user_id == ^user_id, | ||||
|         where: ag.staged == true, | ||||
|         preload: ^@ammo_group_preloads | ||||
|         preload: ^@pack_preloads | ||||
|     ) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Gets a single ammo_group. | ||||
|   Gets a single pack. | ||||
|  | ||||
|   Raises `KeyError` if the Ammo group does not exist. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_ammo_group!(123, %User{id: 123}) | ||||
|       %AmmoGroup{} | ||||
|       iex> get_pack!(123, %User{id: 123}) | ||||
|       %Pack{} | ||||
|  | ||||
|       iex> get_ammo_group!(456, %User{id: 123}) | ||||
|       iex> get_pack!(456, %User{id: 123}) | ||||
|       ** (KeyError) | ||||
|  | ||||
|   """ | ||||
|   @spec get_ammo_group!(AmmoGroup.id(), User.t()) :: AmmoGroup.t() | ||||
|   def get_ammo_group!(id, user) do | ||||
|     [id] |> get_ammo_groups(user) |> Map.fetch!(id) | ||||
|   @spec get_pack!(Pack.id(), User.t()) :: Pack.t() | ||||
|   def get_pack!(id, user) do | ||||
|     [id] |> get_packs(user) |> Map.fetch!(id) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Gets a group of ammo_groups by their ID. | ||||
|   Gets a group of packs by their ID. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_ammo_groups([123, 456], %User{id: 123}) | ||||
|       %{123 => %AmmoGroup{}, 456 => %AmmoGroup{}} | ||||
|       iex> get_packs([123, 456], %User{id: 123}) | ||||
|       %{123 => %Pack{}, 456 => %Pack{}} | ||||
|  | ||||
|   """ | ||||
|   @spec get_ammo_groups([AmmoGroup.id()], User.t()) :: | ||||
|           %{optional(AmmoGroup.id()) => AmmoGroup.t()} | ||||
|   def get_ammo_groups(ids, %User{id: user_id}) do | ||||
|   @spec get_packs([Pack.id()], User.t()) :: | ||||
|           %{optional(Pack.id()) => Pack.t()} | ||||
|   def get_packs(ids, %User{id: user_id}) do | ||||
|     Repo.all( | ||||
|       from ag in AmmoGroup, | ||||
|       from ag in Pack, | ||||
|         where: ag.id in ^ids, | ||||
|         where: ag.user_id == ^user_id, | ||||
|         preload: ^@ammo_group_preloads, | ||||
|         preload: ^@pack_preloads, | ||||
|         select: {ag.id, ag} | ||||
|     ) | ||||
|     |> Map.new() | ||||
| @@ -908,17 +908,17 @@ defmodule Cannery.Ammo do | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_percentage_remaining( | ||||
|       ...>   %AmmoGroup{id: 123, count: 5, user_id: 456}, | ||||
|       ...>   %Pack{id: 123, count: 5, user_id: 456}, | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       100 | ||||
|  | ||||
|   """ | ||||
|   @spec get_percentage_remaining(AmmoGroup.t(), User.t()) :: non_neg_integer() | ||||
|   def get_percentage_remaining(%AmmoGroup{id: ammo_group_id} = ammo_group, user) do | ||||
|     [ammo_group] | ||||
|   @spec get_percentage_remaining(Pack.t(), User.t()) :: non_neg_integer() | ||||
|   def get_percentage_remaining(%Pack{id: pack_id} = pack, user) do | ||||
|     [pack] | ||||
|     |> get_percentages_remaining(user) | ||||
|     |> Map.fetch!(ammo_group_id) | ||||
|     |> Map.fetch!(pack_id) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
| @@ -927,26 +927,26 @@ defmodule Cannery.Ammo do | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_percentages_remaining( | ||||
|       ...>   [%AmmoGroup{id: 123, count: 5, user_id: 456}], | ||||
|       ...>   [%Pack{id: 123, count: 5, user_id: 456}], | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       %{123 => 100} | ||||
|  | ||||
|   """ | ||||
|   @spec get_percentages_remaining([AmmoGroup.t()], User.t()) :: | ||||
|           %{optional(AmmoGroup.id()) => non_neg_integer()} | ||||
|   def get_percentages_remaining(ammo_groups, %User{id: user_id} = user) do | ||||
|     original_counts = get_original_counts(ammo_groups, user) | ||||
|   @spec get_percentages_remaining([Pack.t()], User.t()) :: | ||||
|           %{optional(Pack.id()) => non_neg_integer()} | ||||
|   def get_percentages_remaining(packs, %User{id: user_id} = user) do | ||||
|     original_counts = get_original_counts(packs, user) | ||||
|  | ||||
|     ammo_groups | ||||
|     |> Map.new(fn %AmmoGroup{id: ammo_group_id, count: count, user_id: ^user_id} -> | ||||
|     packs | ||||
|     |> Map.new(fn %Pack{id: pack_id, count: count, user_id: ^user_id} -> | ||||
|       percentage = | ||||
|         case count do | ||||
|           0 -> 0 | ||||
|           count -> round(count / Map.fetch!(original_counts, ammo_group_id) * 100) | ||||
|           count -> round(count / Map.fetch!(original_counts, pack_id) * 100) | ||||
|         end | ||||
|  | ||||
|       {ammo_group_id, percentage} | ||||
|       {pack_id, percentage} | ||||
|     end) | ||||
|   end | ||||
|  | ||||
| @@ -956,17 +956,17 @@ defmodule Cannery.Ammo do | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_original_count( | ||||
|       ...>   %AmmoGroup{id: 123, count: 5, user_id: 456}, | ||||
|       ...>   %Pack{id: 123, count: 5, user_id: 456}, | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       5 | ||||
|  | ||||
|   """ | ||||
|   @spec get_original_count(AmmoGroup.t(), User.t()) :: non_neg_integer() | ||||
|   def get_original_count(%AmmoGroup{id: ammo_group_id} = ammo_group, current_user) do | ||||
|     [ammo_group] | ||||
|   @spec get_original_count(Pack.t(), User.t()) :: non_neg_integer() | ||||
|   def get_original_count(%Pack{id: pack_id} = pack, current_user) do | ||||
|     [pack] | ||||
|     |> get_original_counts(current_user) | ||||
|     |> Map.fetch!(ammo_group_id) | ||||
|     |> Map.fetch!(pack_id) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
| @@ -975,20 +975,20 @@ defmodule Cannery.Ammo do | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_original_counts( | ||||
|       ...>   [%AmmoGroup{id: 123, count: 5, user_id: 456}], | ||||
|       ...>   [%Pack{id: 123, count: 5, user_id: 456}], | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       %{123 => 5} | ||||
|  | ||||
|   """ | ||||
|   @spec get_original_counts([AmmoGroup.t()], User.t()) :: | ||||
|           %{optional(AmmoGroup.id()) => non_neg_integer()} | ||||
|   def get_original_counts(ammo_groups, %User{id: user_id} = current_user) do | ||||
|     used_counts = ActivityLog.get_used_counts(ammo_groups, current_user) | ||||
|   @spec get_original_counts([Pack.t()], User.t()) :: | ||||
|           %{optional(Pack.id()) => non_neg_integer()} | ||||
|   def get_original_counts(packs, %User{id: user_id} = current_user) do | ||||
|     used_counts = ActivityLog.get_used_counts(packs, current_user) | ||||
|  | ||||
|     ammo_groups | ||||
|     |> Map.new(fn %AmmoGroup{id: ammo_group_id, count: count, user_id: ^user_id} -> | ||||
|       {ammo_group_id, count + Map.get(used_counts, ammo_group_id, 0)} | ||||
|     packs | ||||
|     |> Map.new(fn %Pack{id: pack_id, count: count, user_id: ^user_id} -> | ||||
|       {pack_id, count + Map.get(used_counts, pack_id, 0)} | ||||
|     end) | ||||
|   end | ||||
|  | ||||
| @@ -998,17 +998,17 @@ defmodule Cannery.Ammo do | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_cpr( | ||||
|       ...>   %AmmoGroup{id: 123, price_paid: 5, count: 5, user_id: 456}, | ||||
|       ...>   %Pack{id: 123, price_paid: 5, count: 5, user_id: 456}, | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       1 | ||||
|  | ||||
|   """ | ||||
|   @spec get_cpr(AmmoGroup.t(), User.t()) :: float() | nil | ||||
|   def get_cpr(%AmmoGroup{id: ammo_group_id} = ammo_group, user) do | ||||
|     [ammo_group] | ||||
|   @spec get_cpr(Pack.t(), User.t()) :: float() | nil | ||||
|   def get_cpr(%Pack{id: pack_id} = pack, user) do | ||||
|     [pack] | ||||
|     |> get_cprs(user) | ||||
|     |> Map.get(ammo_group_id) | ||||
|     |> Map.get(pack_id) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
| @@ -1017,22 +1017,22 @@ defmodule Cannery.Ammo do | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_cprs( | ||||
|       ...>   [%AmmoGroup{id: 123, price_paid: 5, count: 5, user_id: 456}], | ||||
|       ...>   [%Pack{id: 123, price_paid: 5, count: 5, user_id: 456}], | ||||
|       ...>   %User{id: 456} | ||||
|       ...> ) | ||||
|       %{123 => 1} | ||||
|  | ||||
|   """ | ||||
|   @spec get_cprs([AmmoGroup.t()], User.t()) :: %{optional(AmmoGroup.id()) => float()} | ||||
|   def get_cprs(ammo_groups, %User{id: user_id} = current_user) do | ||||
|     original_counts = get_original_counts(ammo_groups, current_user) | ||||
|   @spec get_cprs([Pack.t()], User.t()) :: %{optional(Pack.id()) => float()} | ||||
|   def get_cprs(packs, %User{id: user_id} = current_user) do | ||||
|     original_counts = get_original_counts(packs, current_user) | ||||
|  | ||||
|     ammo_groups | ||||
|     |> Enum.reject(fn %AmmoGroup{price_paid: price_paid, user_id: ^user_id} -> | ||||
|     packs | ||||
|     |> Enum.reject(fn %Pack{price_paid: price_paid, user_id: ^user_id} -> | ||||
|       price_paid |> is_nil() | ||||
|     end) | ||||
|     |> Map.new(fn %{id: ammo_group_id, price_paid: price_paid} -> | ||||
|       {ammo_group_id, calculate_cpr(price_paid, Map.fetch!(original_counts, ammo_group_id))} | ||||
|     |> Map.new(fn %{id: pack_id, price_paid: price_paid} -> | ||||
|       {pack_id, calculate_cpr(price_paid, Map.fetch!(original_counts, pack_id))} | ||||
|     end) | ||||
|   end | ||||
|  | ||||
| @@ -1042,41 +1042,41 @@ defmodule Cannery.Ammo do | ||||
|   defp calculate_cpr(price_paid, total_count), do: price_paid / total_count | ||||
|  | ||||
|   @doc """ | ||||
|   Creates multiple ammo_groups at once. | ||||
|   Creates multiple packs at once. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> create_ammo_groups(%{field: value}, 3, %User{id: 123}) | ||||
|       {:ok, {3, [%AmmoGroup{}]}} | ||||
|       iex> create_packs(%{field: value}, 3, %User{id: 123}) | ||||
|       {:ok, {3, [%Pack{}]}} | ||||
|  | ||||
|       iex> create_ammo_groups(%{field: bad_value}, 3, %User{id: 123}) | ||||
|       iex> create_packs(%{field: bad_value}, 3, %User{id: 123}) | ||||
|       {:error, %Changeset{}} | ||||
|  | ||||
|   """ | ||||
|   @spec create_ammo_groups(attrs :: map(), multiplier :: non_neg_integer(), User.t()) :: | ||||
|           {:ok, {count :: non_neg_integer(), [AmmoGroup.t()] | nil}} | ||||
|           | {:error, AmmoGroup.changeset()} | ||||
|   def create_ammo_groups(attrs, multiplier, %User{} = user) do | ||||
|   @spec create_packs(attrs :: map(), multiplier :: non_neg_integer(), User.t()) :: | ||||
|           {:ok, {count :: non_neg_integer(), [Pack.t()] | nil}} | ||||
|           | {:error, Pack.changeset()} | ||||
|   def create_packs(attrs, multiplier, %User{} = user) do | ||||
|     attrs | ||||
|     |> Map.new(fn {k, v} -> {to_string(k), v} end) | ||||
|     |> do_create_ammo_groups(multiplier, user) | ||||
|     |> do_create_packs(multiplier, user) | ||||
|   end | ||||
|  | ||||
|   defp do_create_ammo_groups( | ||||
|   defp do_create_packs( | ||||
|          %{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs, | ||||
|          multiplier, | ||||
|          user | ||||
|        ) | ||||
|        when multiplier >= 1 and | ||||
|               multiplier <= @ammo_group_create_limit and | ||||
|               multiplier <= @pack_create_limit and | ||||
|               ammo_type_id |> is_binary() and | ||||
|               container_id |> is_binary() do | ||||
|     now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) | ||||
|  | ||||
|     changesets = | ||||
|       Enum.map(1..multiplier, fn _count -> | ||||
|         %AmmoGroup{} | ||||
|         |> AmmoGroup.create_changeset( | ||||
|         %Pack{} | ||||
|         |> Pack.create_changeset( | ||||
|           get_ammo_type!(ammo_type_id, user), | ||||
|           Containers.get_container!(container_id, user), | ||||
|           user, | ||||
| @@ -1085,9 +1085,9 @@ defmodule Cannery.Ammo do | ||||
|       end) | ||||
|  | ||||
|     if changesets |> Enum.all?(fn %{valid?: valid} -> valid end) do | ||||
|       {count, inserted_ammo_groups} = | ||||
|       {count, inserted_packs} = | ||||
|         Repo.insert_all( | ||||
|           AmmoGroup, | ||||
|           Pack, | ||||
|           changesets | ||||
|           |> Enum.map(fn changeset -> | ||||
|             changeset | ||||
| @@ -1097,7 +1097,7 @@ defmodule Cannery.Ammo do | ||||
|           returning: true | ||||
|         ) | ||||
|  | ||||
|       {:ok, {count, inserted_ammo_groups |> preload_ammo_group()}} | ||||
|       {:ok, {count, inserted_packs |> preload_pack()}} | ||||
|     else | ||||
|       changesets | ||||
|       |> Enum.reject(fn %{valid?: valid} -> valid end) | ||||
| @@ -1106,15 +1106,15 @@ defmodule Cannery.Ammo do | ||||
|     end | ||||
|   end | ||||
|  | ||||
|   defp do_create_ammo_groups( | ||||
|   defp do_create_packs( | ||||
|          %{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs, | ||||
|          _multiplier, | ||||
|          user | ||||
|        ) | ||||
|        when is_binary(ammo_type_id) and is_binary(container_id) do | ||||
|     changeset = | ||||
|       %AmmoGroup{} | ||||
|       |> AmmoGroup.create_changeset( | ||||
|       %Pack{} | ||||
|       |> Pack.create_changeset( | ||||
|         get_ammo_type!(ammo_type_id, user), | ||||
|         Containers.get_container!(container_id, user), | ||||
|         user, | ||||
| @@ -1125,79 +1125,79 @@ defmodule Cannery.Ammo do | ||||
|     {:error, changeset} | ||||
|   end | ||||
|  | ||||
|   defp do_create_ammo_groups(invalid_attrs, _multiplier, user) do | ||||
|     {:error, %AmmoGroup{} |> AmmoGroup.create_changeset(nil, nil, user, invalid_attrs)} | ||||
|   defp do_create_packs(invalid_attrs, _multiplier, user) do | ||||
|     {:error, %Pack{} |> Pack.create_changeset(nil, nil, user, invalid_attrs)} | ||||
|   end | ||||
|  | ||||
|   @spec preload_ammo_group(AmmoGroup.t()) :: AmmoGroup.t() | ||||
|   @spec preload_ammo_group([AmmoGroup.t()]) :: [AmmoGroup.t()] | ||||
|   defp preload_ammo_group(ammo_group_or_ammo_groups) do | ||||
|     ammo_group_or_ammo_groups |> Repo.preload(@ammo_group_preloads) | ||||
|   @spec preload_pack(Pack.t()) :: Pack.t() | ||||
|   @spec preload_pack([Pack.t()]) :: [Pack.t()] | ||||
|   defp preload_pack(pack_or_packs) do | ||||
|     pack_or_packs |> Repo.preload(@pack_preloads) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Updates a ammo_group. | ||||
|   Updates a pack. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> update_ammo_group(ammo_group, %{field: new_value}, %User{id: 123}) | ||||
|       {:ok, %AmmoGroup{}} | ||||
|       iex> update_pack(pack, %{field: new_value}, %User{id: 123}) | ||||
|       {:ok, %Pack{}} | ||||
|  | ||||
|       iex> update_ammo_group(ammo_group, %{field: bad_value}, %User{id: 123}) | ||||
|       iex> update_pack(pack, %{field: bad_value}, %User{id: 123}) | ||||
|       {:error, %Changeset{}} | ||||
|  | ||||
|   """ | ||||
|   @spec update_ammo_group(AmmoGroup.t(), attrs :: map(), User.t()) :: | ||||
|           {:ok, AmmoGroup.t()} | {:error, AmmoGroup.changeset()} | ||||
|   def update_ammo_group( | ||||
|         %AmmoGroup{user_id: user_id} = ammo_group, | ||||
|   @spec update_pack(Pack.t(), attrs :: map(), User.t()) :: | ||||
|           {:ok, Pack.t()} | {:error, Pack.changeset()} | ||||
|   def update_pack( | ||||
|         %Pack{user_id: user_id} = pack, | ||||
|         attrs, | ||||
|         %User{id: user_id} = user | ||||
|       ) do | ||||
|     ammo_group | ||||
|     |> AmmoGroup.update_changeset(attrs, user) | ||||
|     pack | ||||
|     |> Pack.update_changeset(attrs, user) | ||||
|     |> Repo.update() | ||||
|     |> case do | ||||
|       {:ok, ammo_group} -> {:ok, ammo_group |> preload_ammo_group()} | ||||
|       {:ok, pack} -> {:ok, pack |> preload_pack()} | ||||
|       {:error, changeset} -> {:error, changeset} | ||||
|     end | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Deletes a ammo_group. | ||||
|   Deletes a pack. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> delete_ammo_group(ammo_group, %User{id: 123}) | ||||
|       {:ok, %AmmoGroup{}} | ||||
|       iex> delete_pack(pack, %User{id: 123}) | ||||
|       {:ok, %Pack{}} | ||||
|  | ||||
|       iex> delete_ammo_group(ammo_group, %User{id: 123}) | ||||
|       iex> delete_pack(pack, %User{id: 123}) | ||||
|       {:error, %Changeset{}} | ||||
|  | ||||
|   """ | ||||
|   @spec delete_ammo_group(AmmoGroup.t(), User.t()) :: | ||||
|           {:ok, AmmoGroup.t()} | {:error, AmmoGroup.changeset()} | ||||
|   def delete_ammo_group(%AmmoGroup{user_id: user_id} = ammo_group, %User{id: user_id}) do | ||||
|     ammo_group | ||||
|   @spec delete_pack(Pack.t(), User.t()) :: | ||||
|           {:ok, Pack.t()} | {:error, Pack.changeset()} | ||||
|   def delete_pack(%Pack{user_id: user_id} = pack, %User{id: user_id}) do | ||||
|     pack | ||||
|     |> Repo.delete() | ||||
|     |> case do | ||||
|       {:ok, ammo_group} -> {:ok, ammo_group |> preload_ammo_group()} | ||||
|       {:ok, pack} -> {:ok, pack |> preload_pack()} | ||||
|       {:error, changeset} -> {:error, changeset} | ||||
|     end | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Deletes a ammo_group. | ||||
|   Deletes a pack. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> delete_ammo_group!(ammo_group, %User{id: 123}) | ||||
|       %AmmoGroup{} | ||||
|       iex> delete_pack!(pack, %User{id: 123}) | ||||
|       %Pack{} | ||||
|  | ||||
|   """ | ||||
|   @spec delete_ammo_group!(AmmoGroup.t(), User.t()) :: AmmoGroup.t() | ||||
|   def delete_ammo_group!(ammo_group, user) do | ||||
|     {:ok, ammo_group} = delete_ammo_group(ammo_group, user) | ||||
|     ammo_group | ||||
|   @spec delete_pack!(Pack.t(), User.t()) :: Pack.t() | ||||
|   def delete_pack!(pack, user) do | ||||
|     {:ok, pack} = delete_pack(pack, user) | ||||
|     pack | ||||
|   end | ||||
| end | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| defmodule Cannery.Ammo.AmmoGroup do | ||||
| defmodule Cannery.Ammo.Pack do | ||||
|   @moduledoc """ | ||||
|   A group of a certain ammunition type. | ||||
|  | ||||
| @@ -25,7 +25,7 @@ defmodule Cannery.Ammo.AmmoGroup do | ||||
|            ]} | ||||
|   @primary_key {:id, :binary_id, autogenerate: true} | ||||
|   @foreign_key_type :binary_id | ||||
|   schema "ammo_groups" do | ||||
|   schema "packs" do | ||||
|     field :count, :integer | ||||
|     field :notes, :string | ||||
|     field :price_paid, :float | ||||
| @@ -53,27 +53,27 @@ defmodule Cannery.Ammo.AmmoGroup do | ||||
|           inserted_at: NaiveDateTime.t(), | ||||
|           updated_at: NaiveDateTime.t() | ||||
|         } | ||||
|   @type new_ammo_group :: %__MODULE__{} | ||||
|   @type new_pack :: %__MODULE__{} | ||||
|   @type id :: UUID.t() | ||||
|   @type changeset :: Changeset.t(t() | new_ammo_group()) | ||||
|   @type changeset :: Changeset.t(t() | new_pack()) | ||||
|  | ||||
|   @doc false | ||||
|   @spec create_changeset( | ||||
|           new_ammo_group(), | ||||
|           new_pack(), | ||||
|           AmmoType.t() | nil, | ||||
|           Container.t() | nil, | ||||
|           User.t(), | ||||
|           attrs :: map() | ||||
|         ) :: changeset() | ||||
|   def create_changeset( | ||||
|         ammo_group, | ||||
|         pack, | ||||
|         %AmmoType{id: ammo_type_id}, | ||||
|         %Container{id: container_id, user_id: user_id}, | ||||
|         %User{id: user_id}, | ||||
|         attrs | ||||
|       ) | ||||
|       when is_binary(ammo_type_id) and is_binary(container_id) and is_binary(user_id) do | ||||
|     ammo_group | ||||
|     pack | ||||
|     |> change(ammo_type_id: ammo_type_id) | ||||
|     |> change(user_id: user_id) | ||||
|     |> change(container_id: container_id) | ||||
| @@ -85,17 +85,17 @@ defmodule Cannery.Ammo.AmmoGroup do | ||||
|   @doc """ | ||||
|   Invalid changeset, used to prompt user to select ammo type and container | ||||
|   """ | ||||
|   def create_changeset(ammo_group, _invalid_ammo_type, _invalid_container, _invalid_user, attrs) do | ||||
|     ammo_group | ||||
|   def create_changeset(pack, _invalid_ammo_type, _invalid_container, _invalid_user, attrs) do | ||||
|     pack | ||||
|     |> cast(attrs, [:ammo_type_id, :container_id]) | ||||
|     |> validate_required([:ammo_type_id, :container_id]) | ||||
|     |> add_error(:invalid, dgettext("errors", "Please select an ammo type and container")) | ||||
|   end | ||||
|  | ||||
|   @doc false | ||||
|   @spec update_changeset(t() | new_ammo_group(), attrs :: map(), User.t()) :: changeset() | ||||
|   def update_changeset(ammo_group, attrs, user) do | ||||
|     ammo_group | ||||
|   @spec update_changeset(t() | new_pack(), attrs :: map(), User.t()) :: changeset() | ||||
|   def update_changeset(pack, attrs, user) do | ||||
|     pack | ||||
|     |> cast(attrs, [:count, :price_paid, :notes, :staged, :purchased_on, :container_id]) | ||||
|     |> validate_number(:count, greater_than_or_equal_to: 0) | ||||
|     |> validate_container_id(user) | ||||
| @@ -116,9 +116,9 @@ defmodule Cannery.Ammo.AmmoGroup do | ||||
|   This range changeset is used when "using up" ammo groups, and allows for | ||||
|   updating the count to 0 | ||||
|   """ | ||||
|   @spec range_changeset(t() | new_ammo_group(), attrs :: map()) :: changeset() | ||||
|   def range_changeset(ammo_group, attrs) do | ||||
|     ammo_group | ||||
|   @spec range_changeset(t() | new_pack(), attrs :: map()) :: changeset() | ||||
|   def range_changeset(pack, attrs) do | ||||
|     pack | ||||
|     |> cast(attrs, [:count, :staged]) | ||||
|     |> validate_required([:count, :staged]) | ||||
|   end | ||||
|   | ||||
| @@ -8,7 +8,7 @@ defmodule Cannery.Ammo.AmmoType do | ||||
|   use Ecto.Schema | ||||
|   import Ecto.Changeset | ||||
|   alias Cannery.Accounts.User | ||||
|   alias Cannery.Ammo.AmmoGroup | ||||
|   alias Cannery.Ammo.Pack | ||||
|   alias Ecto.{Changeset, UUID} | ||||
|  | ||||
|   @derive {Jason.Encoder, | ||||
| @@ -83,7 +83,7 @@ defmodule Cannery.Ammo.AmmoType do | ||||
|     field :dram_equivalent, :string | ||||
|  | ||||
|     field :user_id, :binary_id | ||||
|     has_many :ammo_groups, AmmoGroup | ||||
|     has_many :packs, Pack | ||||
|  | ||||
|     timestamps() | ||||
|   end | ||||
| @@ -123,7 +123,7 @@ defmodule Cannery.Ammo.AmmoType do | ||||
|           manufacturer: String.t() | nil, | ||||
|           upc: String.t() | nil, | ||||
|           user_id: User.id(), | ||||
|           ammo_groups: [AmmoGroup.t()] | nil, | ||||
|           packs: [Pack.t()] | nil, | ||||
|           inserted_at: NaiveDateTime.t(), | ||||
|           updated_at: NaiveDateTime.t() | ||||
|         } | ||||
|   | ||||
| @@ -5,7 +5,7 @@ defmodule Cannery.Containers do | ||||
|  | ||||
|   import CanneryWeb.Gettext | ||||
|   import Ecto.Query, warn: false | ||||
|   alias Cannery.{Accounts.User, Ammo.AmmoGroup, Repo} | ||||
|   alias Cannery.{Accounts.User, Ammo.Pack, Repo} | ||||
|   alias Cannery.Containers.{Container, ContainerTag, Tag} | ||||
|   alias Ecto.Changeset | ||||
|  | ||||
| @@ -203,7 +203,7 @@ defmodule Cannery.Containers do | ||||
|           {:ok, Container.t()} | {:error, Container.changeset()} | ||||
|   def delete_container(%Container{user_id: user_id} = container, %User{id: user_id}) do | ||||
|     Repo.one( | ||||
|       from ag in AmmoGroup, | ||||
|       from ag in Pack, | ||||
|         where: ag.container_id == ^container.id, | ||||
|         select: count(ag.id) | ||||
|     ) | ||||
| @@ -221,7 +221,7 @@ defmodule Cannery.Containers do | ||||
|  | ||||
|         container | ||||
|         |> Container.update_changeset(%{}) | ||||
|         |> Changeset.add_error(:ammo_groups, error) | ||||
|         |> Changeset.add_error(:packs, error) | ||||
|         |> Changeset.apply_action(:delete) | ||||
|     end | ||||
|   end | ||||
|   | ||||
| @@ -4,7 +4,7 @@ defmodule CanneryWeb.Components.AddShotGroupComponent do | ||||
|   """ | ||||
|  | ||||
|   use CanneryWeb, :live_component | ||||
|   alias Cannery.{Accounts.User, ActivityLog, ActivityLog.ShotGroup, Ammo.AmmoGroup} | ||||
|   alias Cannery.{Accounts.User, ActivityLog, ActivityLog.ShotGroup, Ammo.Pack} | ||||
|   alias Ecto.Changeset | ||||
|   alias Phoenix.LiveView.{JS, Socket} | ||||
|  | ||||
| @@ -12,15 +12,15 @@ defmodule CanneryWeb.Components.AddShotGroupComponent do | ||||
|   @spec update( | ||||
|           %{ | ||||
|             required(:current_user) => User.t(), | ||||
|             required(:ammo_group) => AmmoGroup.t(), | ||||
|             required(:pack) => Pack.t(), | ||||
|             optional(any()) => any() | ||||
|           }, | ||||
|           Socket.t() | ||||
|         ) :: {:ok, Socket.t()} | ||||
|   def update(%{ammo_group: ammo_group, current_user: current_user} = assigns, socket) do | ||||
|   def update(%{pack: pack, current_user: current_user} = assigns, socket) do | ||||
|     changeset = | ||||
|       %ShotGroup{date: Date.utc_today()} | ||||
|       |> ShotGroup.create_changeset(current_user, ammo_group, %{}) | ||||
|       |> ShotGroup.create_changeset(current_user, pack, %{}) | ||||
|  | ||||
|     {:ok, socket |> assign(assigns) |> assign(:changeset, changeset)} | ||||
|   end | ||||
| @@ -29,11 +29,11 @@ defmodule CanneryWeb.Components.AddShotGroupComponent do | ||||
|   def handle_event( | ||||
|         "validate", | ||||
|         %{"shot_group" => shot_group_params}, | ||||
|         %{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket | ||||
|         %{assigns: %{pack: pack, current_user: current_user}} = socket | ||||
|       ) do | ||||
|     params = shot_group_params |> process_params(ammo_group) | ||||
|     params = shot_group_params |> process_params(pack) | ||||
|  | ||||
|     changeset = %ShotGroup{} |> ShotGroup.create_changeset(current_user, ammo_group, params) | ||||
|     changeset = %ShotGroup{} |> ShotGroup.create_changeset(current_user, pack, params) | ||||
|  | ||||
|     changeset = | ||||
|       case changeset |> Changeset.apply_action(:validate) do | ||||
| @@ -48,13 +48,13 @@ defmodule CanneryWeb.Components.AddShotGroupComponent do | ||||
|         "save", | ||||
|         %{"shot_group" => shot_group_params}, | ||||
|         %{ | ||||
|           assigns: %{ammo_group: ammo_group, current_user: current_user, return_to: return_to} | ||||
|           assigns: %{pack: pack, current_user: current_user, return_to: return_to} | ||||
|         } = socket | ||||
|       ) do | ||||
|     socket = | ||||
|       shot_group_params | ||||
|       |> process_params(ammo_group) | ||||
|       |> ActivityLog.create_shot_group(current_user, ammo_group) | ||||
|       |> process_params(pack) | ||||
|       |> ActivityLog.create_shot_group(current_user, pack) | ||||
|       |> case do | ||||
|         {:ok, _shot_group} -> | ||||
|           prompt = dgettext("prompts", "Shots recorded successfully") | ||||
| @@ -68,7 +68,7 @@ defmodule CanneryWeb.Components.AddShotGroupComponent do | ||||
|   end | ||||
|  | ||||
|   # calculate count from shots left | ||||
|   defp process_params(params, %AmmoGroup{count: count}) do | ||||
|   defp process_params(params, %Pack{count: count}) do | ||||
|     shot_group_count = | ||||
|       if params |> Map.get("ammo_left", "") == "" do | ||||
|         nil | ||||
|   | ||||
| @@ -22,7 +22,7 @@ | ||||
|     <%= label(f, :ammo_left, gettext("Rounds left"), class: "title text-lg text-primary-600") %> | ||||
|     <%= number_input(f, :ammo_left, | ||||
|       min: 0, | ||||
|       max: @ammo_group.count - 1, | ||||
|       max: @pack.count - 1, | ||||
|       placeholder: gettext("Rounds left"), | ||||
|       class: "input input-primary" | ||||
|     ) %> | ||||
|   | ||||
| @@ -1,9 +1,9 @@ | ||||
| defmodule CanneryWeb.Components.AmmoGroupTableComponent do | ||||
| defmodule CanneryWeb.Components.PackTableComponent do | ||||
|   @moduledoc """ | ||||
|   A component that displays a list of ammo groups | ||||
|   """ | ||||
|   use CanneryWeb, :live_component | ||||
|   alias Cannery.{Accounts.User, Ammo.AmmoGroup, ComparableDate} | ||||
|   alias Cannery.{Accounts.User, Ammo.Pack, ComparableDate} | ||||
|   alias Cannery.{ActivityLog, Ammo, Containers} | ||||
|   alias CanneryWeb.Components.TableComponent | ||||
|   alias Ecto.UUID | ||||
| @@ -14,7 +14,7 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do | ||||
|           %{ | ||||
|             required(:id) => UUID.t(), | ||||
|             required(:current_user) => User.t(), | ||||
|             required(:ammo_groups) => [AmmoGroup.t()], | ||||
|             required(:packs) => [Pack.t()], | ||||
|             required(:show_used) => boolean(), | ||||
|             optional(:ammo_type) => Rendered.t(), | ||||
|             optional(:range) => Rendered.t(), | ||||
| @@ -25,8 +25,7 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do | ||||
|           Socket.t() | ||||
|         ) :: {:ok, Socket.t()} | ||||
|   def update( | ||||
|         %{id: _id, ammo_groups: _ammo_group, current_user: _current_user, show_used: _show_used} = | ||||
|           assigns, | ||||
|         %{id: _id, packs: _pack, current_user: _current_user, show_used: _show_used} = assigns, | ||||
|         socket | ||||
|       ) do | ||||
|     socket = | ||||
| @@ -36,15 +35,15 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do | ||||
|       |> assign_new(:range, fn -> [] end) | ||||
|       |> assign_new(:container, fn -> [] end) | ||||
|       |> assign_new(:actions, fn -> [] end) | ||||
|       |> display_ammo_groups() | ||||
|       |> display_packs() | ||||
|  | ||||
|     {:ok, socket} | ||||
|   end | ||||
|  | ||||
|   defp display_ammo_groups( | ||||
|   defp display_packs( | ||||
|          %{ | ||||
|            assigns: %{ | ||||
|              ammo_groups: ammo_groups, | ||||
|              packs: packs, | ||||
|              current_user: current_user, | ||||
|              ammo_type: ammo_type, | ||||
|              range: range, | ||||
| @@ -98,7 +97,7 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do | ||||
|       ) | ||||
|  | ||||
|     containers = | ||||
|       ammo_groups | ||||
|       packs | ||||
|       |> Enum.map(fn %{container_id: container_id} -> container_id end) | ||||
|       |> Containers.get_containers(current_user) | ||||
|  | ||||
| @@ -108,18 +107,18 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do | ||||
|       columns: columns, | ||||
|       container: container, | ||||
|       containers: containers, | ||||
|       original_counts: Ammo.get_original_counts(ammo_groups, current_user), | ||||
|       cprs: Ammo.get_cprs(ammo_groups, current_user), | ||||
|       last_used_dates: ActivityLog.get_last_used_dates(ammo_groups, current_user), | ||||
|       percentages_remaining: Ammo.get_percentages_remaining(ammo_groups, current_user), | ||||
|       original_counts: Ammo.get_original_counts(packs, current_user), | ||||
|       cprs: Ammo.get_cprs(packs, current_user), | ||||
|       last_used_dates: ActivityLog.get_last_used_dates(packs, current_user), | ||||
|       percentages_remaining: Ammo.get_percentages_remaining(packs, current_user), | ||||
|       actions: actions, | ||||
|       range: range | ||||
|     } | ||||
|  | ||||
|     rows = | ||||
|       ammo_groups | ||||
|       |> Enum.map(fn ammo_group -> | ||||
|         ammo_group |> get_row_data_for_ammo_group(extra_data) | ||||
|       packs | ||||
|       |> Enum.map(fn pack -> | ||||
|         pack |> get_row_data_for_pack(extra_data) | ||||
|       end) | ||||
|  | ||||
|     socket |> assign(columns: columns, rows: rows) | ||||
| @@ -134,15 +133,15 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do | ||||
|     """ | ||||
|   end | ||||
|  | ||||
|   @spec get_row_data_for_ammo_group(AmmoGroup.t(), additional_data :: map()) :: map() | ||||
|   defp get_row_data_for_ammo_group(ammo_group, %{columns: columns} = additional_data) do | ||||
|   @spec get_row_data_for_pack(Pack.t(), additional_data :: map()) :: map() | ||||
|   defp get_row_data_for_pack(pack, %{columns: columns} = additional_data) do | ||||
|     columns | ||||
|     |> Map.new(fn %{key: key} -> | ||||
|       {key, get_value_for_key(key, ammo_group, additional_data)} | ||||
|       {key, get_value_for_key(key, pack, additional_data)} | ||||
|     end) | ||||
|   end | ||||
|  | ||||
|   @spec get_value_for_key(atom(), AmmoGroup.t(), additional_data :: map()) :: | ||||
|   @spec get_value_for_key(atom(), Pack.t(), additional_data :: map()) :: | ||||
|           any() | {any(), Rendered.t()} | ||||
|   defp get_value_for_key( | ||||
|          :ammo_type, | ||||
| @@ -170,9 +169,9 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do | ||||
|      """} | ||||
|   end | ||||
|  | ||||
|   defp get_value_for_key(:used_up_on, %{id: ammo_group_id}, %{last_used_dates: last_used_dates}) do | ||||
|     last_used_date = last_used_dates |> Map.get(ammo_group_id) | ||||
|     assigns = %{id: ammo_group_id, last_used_date: last_used_date} | ||||
|   defp get_value_for_key(:used_up_on, %{id: pack_id}, %{last_used_dates: last_used_dates}) do | ||||
|     last_used_date = last_used_dates |> Map.get(pack_id) | ||||
|     assigns = %{id: pack_id, last_used_date: last_used_date} | ||||
|  | ||||
|     {last_used_date, | ||||
|      ~H""" | ||||
| @@ -184,29 +183,29 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do | ||||
|      """} | ||||
|   end | ||||
|  | ||||
|   defp get_value_for_key(:range, %{staged: staged} = ammo_group, %{range: range}) do | ||||
|     assigns = %{range: range, ammo_group: ammo_group} | ||||
|   defp get_value_for_key(:range, %{staged: staged} = pack, %{range: range}) do | ||||
|     assigns = %{range: range, pack: pack} | ||||
|  | ||||
|     {staged, | ||||
|      ~H""" | ||||
|      <%= render_slot(@range, @ammo_group) %> | ||||
|      <%= render_slot(@range, @pack) %> | ||||
|      """} | ||||
|   end | ||||
|  | ||||
|   defp get_value_for_key( | ||||
|          :remaining, | ||||
|          %{id: ammo_group_id}, | ||||
|          %{id: pack_id}, | ||||
|          %{percentages_remaining: percentages_remaining} | ||||
|        ) do | ||||
|     percentage = Map.fetch!(percentages_remaining, ammo_group_id) | ||||
|     percentage = Map.fetch!(percentages_remaining, pack_id) | ||||
|     {percentage, gettext("%{percentage}%", percentage: percentage)} | ||||
|   end | ||||
|  | ||||
|   defp get_value_for_key(:actions, ammo_group, %{actions: actions}) do | ||||
|     assigns = %{actions: actions, ammo_group: ammo_group} | ||||
|   defp get_value_for_key(:actions, pack, %{actions: actions}) do | ||||
|     assigns = %{actions: actions, pack: pack} | ||||
|  | ||||
|     ~H""" | ||||
|     <%= render_slot(@actions, @ammo_group) %> | ||||
|     <%= render_slot(@actions, @pack) %> | ||||
|     """ | ||||
|   end | ||||
|  | ||||
| @@ -214,7 +213,7 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do | ||||
|  | ||||
|   defp get_value_for_key( | ||||
|          :container, | ||||
|          %{container_id: container_id} = ammo_group, | ||||
|          %{container_id: container_id} = pack, | ||||
|          %{container: container_block, containers: containers} | ||||
|        ) do | ||||
|     container = %{name: container_name} = Map.fetch!(containers, container_id) | ||||
| @@ -222,35 +221,35 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do | ||||
|     assigns = %{ | ||||
|       container: container, | ||||
|       container_block: container_block, | ||||
|       ammo_group: ammo_group | ||||
|       pack: pack | ||||
|     } | ||||
|  | ||||
|     {container_name, | ||||
|      ~H""" | ||||
|      <%= render_slot(@container_block, {@ammo_group, @container}) %> | ||||
|      <%= render_slot(@container_block, {@pack, @container}) %> | ||||
|      """} | ||||
|   end | ||||
|  | ||||
|   defp get_value_for_key( | ||||
|          :original_count, | ||||
|          %{id: ammo_group_id}, | ||||
|          %{id: pack_id}, | ||||
|          %{original_counts: original_counts} | ||||
|        ) do | ||||
|     Map.fetch!(original_counts, ammo_group_id) | ||||
|     Map.fetch!(original_counts, pack_id) | ||||
|   end | ||||
|  | ||||
|   defp get_value_for_key(:cpr, %{price_paid: nil}, _additional_data), | ||||
|     do: {0, gettext("No cost information")} | ||||
|  | ||||
|   defp get_value_for_key(:cpr, %{id: ammo_group_id}, %{cprs: cprs}) do | ||||
|     amount = Map.fetch!(cprs, ammo_group_id) | ||||
|   defp get_value_for_key(:cpr, %{id: pack_id}, %{cprs: cprs}) do | ||||
|     amount = Map.fetch!(cprs, pack_id) | ||||
|     {amount, gettext("$%{amount}", amount: display_currency(amount))} | ||||
|   end | ||||
|  | ||||
|   defp get_value_for_key(:count, %{count: count}, _additional_data), | ||||
|     do: if(count == 0, do: {0, gettext("Empty")}, else: count) | ||||
|  | ||||
|   defp get_value_for_key(key, ammo_group, _additional_data), do: ammo_group |> Map.get(key) | ||||
|   defp get_value_for_key(key, pack, _additional_data), do: pack |> Map.get(key) | ||||
|  | ||||
|   @spec display_currency(float()) :: String.t() | ||||
|   defp display_currency(float), do: :erlang.float_to_binary(float, decimals: 2) | ||||
|   | ||||
| @@ -153,7 +153,7 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do | ||||
|       |> TableComponent.maybe_compose_columns(%{label: gettext("Name"), key: :name, type: :name}) | ||||
|  | ||||
|     round_counts = ammo_types |> Ammo.get_round_count_for_ammo_types(current_user) | ||||
|     packs_count = ammo_types |> Ammo.get_ammo_groups_count_for_types(current_user) | ||||
|     packs_count = ammo_types |> Ammo.get_packs_count_for_types(current_user) | ||||
|     average_costs = ammo_types |> Ammo.get_average_cost_for_ammo_types(current_user) | ||||
|  | ||||
|     [used_counts, historical_round_counts, historical_pack_counts, used_pack_counts] = | ||||
| @@ -161,8 +161,8 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do | ||||
|         [ | ||||
|           ammo_types |> ActivityLog.get_used_count_for_ammo_types(current_user), | ||||
|           ammo_types |> Ammo.get_historical_count_for_ammo_types(current_user), | ||||
|           ammo_types |> Ammo.get_ammo_groups_count_for_types(current_user, true), | ||||
|           ammo_types |> Ammo.get_used_ammo_groups_count_for_types(current_user) | ||||
|           ammo_types |> Ammo.get_packs_count_for_types(current_user, true), | ||||
|           ammo_types |> Ammo.get_used_packs_count_for_types(current_user) | ||||
|         ] | ||||
|       else | ||||
|         [nil, nil, nil, nil] | ||||
|   | ||||
| @@ -71,7 +71,7 @@ defmodule CanneryWeb.Components.ContainerTableComponent do | ||||
|       current_user: current_user, | ||||
|       tag_actions: tag_actions, | ||||
|       actions: actions, | ||||
|       pack_count: Ammo.get_ammo_groups_count_for_containers(containers, current_user), | ||||
|       pack_count: Ammo.get_packs_count_for_containers(containers, current_user), | ||||
|       round_count: Ammo.get_round_count_for_containers(containers, current_user) | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -5,7 +5,7 @@ defmodule CanneryWeb.CoreComponents do | ||||
|   use Phoenix.Component | ||||
|   import CanneryWeb.{Gettext, ViewHelpers} | ||||
|   alias Cannery.{Accounts, Accounts.Invite, Accounts.User} | ||||
|   alias Cannery.{Ammo, Ammo.AmmoGroup} | ||||
|   alias Cannery.{Ammo, Ammo.Pack} | ||||
|   alias Cannery.{Containers.Container, Containers.Tag} | ||||
|   alias CanneryWeb.{Endpoint, HomeLive} | ||||
|   alias CanneryWeb.Router.Helpers, as: Routes | ||||
| @@ -86,7 +86,7 @@ defmodule CanneryWeb.CoreComponents do | ||||
|  | ||||
|   def simple_tag_card(assigns) | ||||
|  | ||||
|   attr :ammo_group, AmmoGroup, required: true | ||||
|   attr :pack, Pack, required: true | ||||
|   attr :current_user, User, required: true | ||||
|   attr :original_count, :integer, default: nil | ||||
|   attr :cpr, :integer, default: nil | ||||
|   | ||||
| @@ -1,48 +1,45 @@ | ||||
| <div | ||||
|   id={"ammo_group-#{@ammo_group.id}"} | ||||
|   id={"pack-#{@pack.id}"} | ||||
|   class="mx-4 my-2 px-8 py-4 | ||||
|     flex flex-col justify-center items-center | ||||
|     border border-gray-400 rounded-lg shadow-lg hover:shadow-md | ||||
|     transition-all duration-300 ease-in-out" | ||||
| > | ||||
|   <.link navigate={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} class="mb-2 link"> | ||||
|   <.link navigate={Routes.pack_show_path(Endpoint, :show, @pack)} class="mb-2 link"> | ||||
|     <h1 class="title text-xl title-primary-500"> | ||||
|       <%= @ammo_group.ammo_type.name %> | ||||
|       <%= @pack.ammo_type.name %> | ||||
|     </h1> | ||||
|   </.link> | ||||
|  | ||||
|   <div class="flex flex-col justify-center items-center"> | ||||
|     <span class="rounded-lg title text-lg"> | ||||
|       <%= gettext("Count:") %> | ||||
|       <%= if @ammo_group.count == 0, do: gettext("Empty"), else: @ammo_group.count %> | ||||
|       <%= if @pack.count == 0, do: gettext("Empty"), else: @pack.count %> | ||||
|     </span> | ||||
|  | ||||
|     <span | ||||
|       :if={@original_count && @original_count != @ammo_group.count} | ||||
|       class="rounded-lg title text-lg" | ||||
|     > | ||||
|     <span :if={@original_count && @original_count != @pack.count} class="rounded-lg title text-lg"> | ||||
|       <%= gettext("Original Count:") %> | ||||
|       <%= @original_count %> | ||||
|     </span> | ||||
|  | ||||
|     <span :if={@ammo_group.notes} class="rounded-lg title text-lg"> | ||||
|     <span :if={@pack.notes} class="rounded-lg title text-lg"> | ||||
|       <%= gettext("Notes:") %> | ||||
|       <%= @ammo_group.notes %> | ||||
|       <%= @pack.notes %> | ||||
|     </span> | ||||
|  | ||||
|     <span :if={@ammo_group.purchased_on} class="rounded-lg title text-lg"> | ||||
|     <span :if={@pack.purchased_on} class="rounded-lg title text-lg"> | ||||
|       <%= gettext("Purchased on:") %> | ||||
|       <.date id={"#{@ammo_group.id}-purchased-on"} date={@ammo_group.purchased_on} /> | ||||
|       <.date id={"#{@pack.id}-purchased-on"} date={@pack.purchased_on} /> | ||||
|     </span> | ||||
|  | ||||
|     <span :if={@last_used_date} class="rounded-lg title text-lg"> | ||||
|       <%= gettext("Last used on:") %> | ||||
|       <.date id={"#{@ammo_group.id}-last-used-on"} date={@last_used_date} /> | ||||
|       <.date id={"#{@pack.id}-last-used-on"} date={@last_used_date} /> | ||||
|     </span> | ||||
|  | ||||
|     <span :if={@ammo_group.price_paid} class="rounded-lg title text-lg"> | ||||
|     <span :if={@pack.price_paid} class="rounded-lg title text-lg"> | ||||
|       <%= gettext("Price paid:") %> | ||||
|       <%= gettext("$%{amount}", amount: display_currency(@ammo_group.price_paid)) %> | ||||
|       <%= gettext("$%{amount}", amount: display_currency(@pack.price_paid)) %> | ||||
|     </span> | ||||
|  | ||||
|     <span :if={@cpr} class="rounded-lg title text-lg"> | ||||
|   | ||||
| @@ -27,10 +27,10 @@ | ||||
|       <%= @container.location %> | ||||
|     </span> | ||||
|  | ||||
|     <%= if @container |> Ammo.get_ammo_groups_count_for_container!(@current_user) != 0 do %> | ||||
|     <%= if @container |> Ammo.get_packs_count_for_container!(@current_user) != 0 do %> | ||||
|       <span class="rounded-lg title text-lg"> | ||||
|         <%= gettext("Packs:") %> | ||||
|         <%= @container |> Ammo.get_ammo_groups_count_for_container!(@current_user) %> | ||||
|         <%= @container |> Ammo.get_packs_count_for_container!(@current_user) %> | ||||
|       </span> | ||||
|  | ||||
|       <span class="rounded-lg title text-lg"> | ||||
|   | ||||
| @@ -52,7 +52,7 @@ | ||||
|         </li> | ||||
|         <li class="mx-2 my-1"> | ||||
|           <.link | ||||
|             navigate={Routes.ammo_group_index_path(Endpoint, :index)} | ||||
|             navigate={Routes.pack_index_path(Endpoint, :index)} | ||||
|             class="text-white hover:underline" | ||||
|           > | ||||
|             <%= gettext("Ammo") %> | ||||
|   | ||||
| @@ -1,10 +1,10 @@ | ||||
| defmodule CanneryWeb.Components.MoveAmmoGroupComponent do | ||||
| defmodule CanneryWeb.Components.MovePackComponent do | ||||
|   @moduledoc """ | ||||
|   Livecomponent that can move an ammo group to another container | ||||
|   """ | ||||
|  | ||||
|   use CanneryWeb, :live_component | ||||
|   alias Cannery.{Accounts.User, Ammo, Ammo.AmmoGroup, Containers, Containers.Container} | ||||
|   alias Cannery.{Accounts.User, Ammo, Ammo.Pack, Containers, Containers.Container} | ||||
|   alias CanneryWeb.Endpoint | ||||
|   alias Ecto.Changeset | ||||
|   alias Phoenix.LiveView.Socket | ||||
| @@ -13,17 +13,16 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do | ||||
|   @spec update( | ||||
|           %{ | ||||
|             required(:current_user) => User.t(), | ||||
|             required(:ammo_group) => AmmoGroup.t(), | ||||
|             required(:pack) => Pack.t(), | ||||
|             optional(any()) => any() | ||||
|           }, | ||||
|           Socket.t() | ||||
|         ) :: {:ok, Socket.t()} | ||||
|   def update( | ||||
|         %{ammo_group: %{container_id: container_id} = ammo_group, current_user: current_user} = | ||||
|           assigns, | ||||
|         %{pack: %{container_id: container_id} = pack, current_user: current_user} = assigns, | ||||
|         socket | ||||
|       ) do | ||||
|     changeset = ammo_group |> AmmoGroup.update_changeset(%{}, current_user) | ||||
|     changeset = pack |> Pack.update_changeset(%{}, current_user) | ||||
|  | ||||
|     containers = | ||||
|       Containers.list_containers(current_user) | ||||
| @@ -41,16 +40,15 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do | ||||
|   def handle_event( | ||||
|         "move", | ||||
|         %{"container_id" => container_id}, | ||||
|         %{assigns: %{ammo_group: ammo_group, current_user: current_user, return_to: return_to}} = | ||||
|           socket | ||||
|         %{assigns: %{pack: pack, current_user: current_user, return_to: return_to}} = socket | ||||
|       ) do | ||||
|     %{name: container_name} = Containers.get_container!(container_id, current_user) | ||||
|  | ||||
|     socket = | ||||
|       ammo_group | ||||
|       |> Ammo.update_ammo_group(%{"container_id" => container_id}, current_user) | ||||
|       pack | ||||
|       |> Ammo.update_pack(%{"container_id" => container_id}, current_user) | ||||
|       |> case do | ||||
|         {:ok, _ammo_group} -> | ||||
|         {:ok, _pack} -> | ||||
|           prompt = dgettext("prompts", "Ammo moved to %{name} successfully", name: container_name) | ||||
|           socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) | ||||
|  | ||||
| @@ -92,7 +90,7 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do | ||||
|       <% else %> | ||||
|         <.live_component | ||||
|           module={CanneryWeb.Components.TableComponent} | ||||
|           id="move_ammo_group_table" | ||||
|           id="move_pack_table" | ||||
|           columns={@columns} | ||||
|           rows={@rows} | ||||
|         /> | ||||
|   | ||||
| @@ -45,12 +45,12 @@ defmodule CanneryWeb.Components.ShotGroupTableComponent do | ||||
|       %{label: gettext("Actions"), key: :actions, sortable: false} | ||||
|     ] | ||||
|  | ||||
|     ammo_groups = | ||||
|     packs = | ||||
|       shot_groups | ||||
|       |> Enum.map(fn %{ammo_group_id: ammo_group_id} -> ammo_group_id end) | ||||
|       |> Ammo.get_ammo_groups(current_user) | ||||
|       |> Enum.map(fn %{pack_id: pack_id} -> pack_id end) | ||||
|       |> Ammo.get_packs(current_user) | ||||
|  | ||||
|     extra_data = %{current_user: current_user, actions: actions, ammo_groups: ammo_groups} | ||||
|     extra_data = %{current_user: current_user, actions: actions, packs: packs} | ||||
|  | ||||
|     rows = | ||||
|       shot_groups | ||||
| @@ -90,13 +90,13 @@ defmodule CanneryWeb.Components.ShotGroupTableComponent do | ||||
|     end) | ||||
|   end | ||||
|  | ||||
|   defp get_row_value(:name, %{ammo_group_id: ammo_group_id}, %{ammo_groups: ammo_groups}) do | ||||
|     assigns = %{ammo_group: ammo_group = Map.fetch!(ammo_groups, ammo_group_id)} | ||||
|   defp get_row_value(:name, %{pack_id: pack_id}, %{packs: packs}) do | ||||
|     assigns = %{pack: pack = Map.fetch!(packs, pack_id)} | ||||
|  | ||||
|     {ammo_group.ammo_type.name, | ||||
|     {pack.ammo_type.name, | ||||
|      ~H""" | ||||
|      <.link navigate={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} class="link"> | ||||
|        <%= @ammo_group.ammo_type.name %> | ||||
|      <.link navigate={Routes.pack_show_path(Endpoint, :show, @pack)} class="link"> | ||||
|        <%= @pack.ammo_type.name %> | ||||
|      </.link> | ||||
|      """} | ||||
|   end | ||||
|   | ||||
| @@ -6,10 +6,9 @@ defmodule CanneryWeb.ExportController do | ||||
|     ammo_types = Ammo.list_ammo_types(current_user, :all) | ||||
|     used_counts = ammo_types |> ActivityLog.get_used_count_for_ammo_types(current_user) | ||||
|     round_counts = ammo_types |> Ammo.get_round_count_for_ammo_types(current_user) | ||||
|     ammo_group_counts = ammo_types |> Ammo.get_ammo_groups_count_for_types(current_user) | ||||
|     pack_counts = ammo_types |> Ammo.get_packs_count_for_types(current_user) | ||||
|  | ||||
|     total_ammo_group_counts = | ||||
|       ammo_types |> Ammo.get_ammo_groups_count_for_types(current_user, true) | ||||
|     total_pack_counts = ammo_types |> Ammo.get_packs_count_for_types(current_user, true) | ||||
|  | ||||
|     average_costs = ammo_types |> Ammo.get_average_cost_for_ammo_types(current_user) | ||||
|  | ||||
| @@ -23,28 +22,28 @@ defmodule CanneryWeb.ExportController do | ||||
|           "average_cost" => Map.get(average_costs, ammo_type_id), | ||||
|           "round_count" => Map.get(round_counts, ammo_type_id, 0), | ||||
|           "used_count" => Map.get(used_counts, ammo_type_id, 0), | ||||
|           "ammo_group_count" => Map.get(ammo_group_counts, ammo_type_id, 0), | ||||
|           "total_ammo_group_count" => Map.get(total_ammo_group_counts, ammo_type_id, 0) | ||||
|           "pack_count" => Map.get(pack_counts, ammo_type_id, 0), | ||||
|           "total_pack_count" => Map.get(total_pack_counts, ammo_type_id, 0) | ||||
|         }) | ||||
|       end) | ||||
|  | ||||
|     ammo_groups = Ammo.list_ammo_groups(nil, :all, current_user, true) | ||||
|     used_counts = ammo_groups |> ActivityLog.get_used_counts(current_user) | ||||
|     original_counts = ammo_groups |> Ammo.get_original_counts(current_user) | ||||
|     cprs = ammo_groups |> Ammo.get_cprs(current_user) | ||||
|     percentages_remaining = ammo_groups |> Ammo.get_percentages_remaining(current_user) | ||||
|     packs = Ammo.list_packs(nil, :all, current_user, true) | ||||
|     used_counts = packs |> ActivityLog.get_used_counts(current_user) | ||||
|     original_counts = packs |> Ammo.get_original_counts(current_user) | ||||
|     cprs = packs |> Ammo.get_cprs(current_user) | ||||
|     percentages_remaining = packs |> Ammo.get_percentages_remaining(current_user) | ||||
|  | ||||
|     ammo_groups = | ||||
|       ammo_groups | ||||
|       |> Enum.map(fn %{id: ammo_group_id} = ammo_group -> | ||||
|         ammo_group | ||||
|     packs = | ||||
|       packs | ||||
|       |> Enum.map(fn %{id: pack_id} = pack -> | ||||
|         pack | ||||
|         |> Jason.encode!() | ||||
|         |> Jason.decode!() | ||||
|         |> Map.merge(%{ | ||||
|           "used_count" => Map.get(used_counts, ammo_group_id), | ||||
|           "percentage_remaining" => Map.fetch!(percentages_remaining, ammo_group_id), | ||||
|           "original_count" => Map.get(original_counts, ammo_group_id), | ||||
|           "cpr" => Map.get(cprs, ammo_group_id) | ||||
|           "used_count" => Map.get(used_counts, pack_id), | ||||
|           "percentage_remaining" => Map.fetch!(percentages_remaining, pack_id), | ||||
|           "original_count" => Map.get(original_counts, pack_id), | ||||
|           "cpr" => Map.get(cprs, pack_id) | ||||
|         }) | ||||
|       end) | ||||
|  | ||||
| @@ -53,14 +52,14 @@ defmodule CanneryWeb.ExportController do | ||||
|     containers = | ||||
|       Containers.list_containers(current_user) | ||||
|       |> Enum.map(fn container -> | ||||
|         ammo_group_count = container |> Ammo.get_ammo_groups_count_for_container!(current_user) | ||||
|         pack_count = container |> Ammo.get_packs_count_for_container!(current_user) | ||||
|         round_count = container |> Ammo.get_round_count_for_container!(current_user) | ||||
|  | ||||
|         container | ||||
|         |> Jason.encode!() | ||||
|         |> Jason.decode!() | ||||
|         |> Map.merge(%{ | ||||
|           "ammo_group_count" => ammo_group_count, | ||||
|           "pack_count" => pack_count, | ||||
|           "round_count" => round_count | ||||
|         }) | ||||
|       end) | ||||
| @@ -68,7 +67,7 @@ defmodule CanneryWeb.ExportController do | ||||
|     json(conn, %{ | ||||
|       user: current_user, | ||||
|       ammo_types: ammo_types, | ||||
|       ammo_groups: ammo_groups, | ||||
|       packs: packs, | ||||
|       shot_groups: shot_groups, | ||||
|       containers: containers | ||||
|     }) | ||||
|   | ||||
| @@ -1,22 +1,22 @@ | ||||
| defmodule CanneryWeb.AmmoGroupLive.FormComponent do | ||||
| defmodule CanneryWeb.PackLive.FormComponent do | ||||
|   @moduledoc """ | ||||
|   Livecomponent that can update or create an Cannery.Ammo.AmmoGroup | ||||
|   Livecomponent that can update or create an Cannery.Ammo.Pack | ||||
|   """ | ||||
|  | ||||
|   use CanneryWeb, :live_component | ||||
|   alias Cannery.Ammo.{AmmoGroup, AmmoType} | ||||
|   alias Cannery.Ammo.{Pack, AmmoType} | ||||
|   alias Cannery.{Accounts.User, Ammo, Containers, Containers.Container} | ||||
|   alias Ecto.Changeset | ||||
|   alias Phoenix.LiveView.Socket | ||||
|  | ||||
|   @ammo_group_create_limit 10_000 | ||||
|   @pack_create_limit 10_000 | ||||
|  | ||||
|   @impl true | ||||
|   @spec update( | ||||
|           %{:ammo_group => AmmoGroup.t(), :current_user => User.t(), optional(any) => any}, | ||||
|           %{:pack => Pack.t(), :current_user => User.t(), optional(any) => any}, | ||||
|           Socket.t() | ||||
|         ) :: {:ok, Socket.t()} | ||||
|   def update(%{ammo_group: _ammo_group} = assigns, socket) do | ||||
|   def update(%{pack: _pack} = assigns, socket) do | ||||
|     socket |> assign(assigns) |> update() | ||||
|   end | ||||
|  | ||||
| @@ -25,7 +25,7 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do | ||||
|     %{assigns: %{ammo_types: ammo_types, containers: containers}} = | ||||
|       socket = | ||||
|       socket | ||||
|       |> assign(:ammo_group_create_limit, @ammo_group_create_limit) | ||||
|       |> assign(:pack_create_limit, @pack_create_limit) | ||||
|       |> assign(:ammo_types, Ammo.list_ammo_types(current_user, :all)) | ||||
|       |> assign_new(:containers, fn -> Containers.list_containers(current_user) end) | ||||
|  | ||||
| @@ -43,16 +43,16 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do | ||||
|   end | ||||
|  | ||||
|   @impl true | ||||
|   def handle_event("validate", %{"ammo_group" => ammo_group_params}, socket) do | ||||
|     {:noreply, socket |> assign_changeset(ammo_group_params, :validate)} | ||||
|   def handle_event("validate", %{"pack" => pack_params}, socket) do | ||||
|     {:noreply, socket |> assign_changeset(pack_params, :validate)} | ||||
|   end | ||||
|  | ||||
|   def handle_event( | ||||
|         "save", | ||||
|         %{"ammo_group" => ammo_group_params}, | ||||
|         %{"pack" => pack_params}, | ||||
|         %{assigns: %{action: action}} = socket | ||||
|       ) do | ||||
|     save_ammo_group(socket, action, ammo_group_params) | ||||
|     save_pack(socket, action, pack_params) | ||||
|   end | ||||
|  | ||||
|   # HTML Helpers | ||||
| @@ -70,8 +70,8 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do | ||||
|   # Save Helpers | ||||
|  | ||||
|   defp assign_changeset( | ||||
|          %{assigns: %{action: action, ammo_group: ammo_group, current_user: user}} = socket, | ||||
|          ammo_group_params, | ||||
|          %{assigns: %{action: action, pack: pack, current_user: user}} = socket, | ||||
|          pack_params, | ||||
|          changeset_action \\ nil | ||||
|        ) do | ||||
|     default_action = | ||||
| @@ -83,12 +83,12 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do | ||||
|     changeset = | ||||
|       case default_action do | ||||
|         :insert -> | ||||
|           ammo_type = maybe_get_ammo_type(ammo_group_params, user) | ||||
|           container = maybe_get_container(ammo_group_params, user) | ||||
|           ammo_group |> AmmoGroup.create_changeset(ammo_type, container, user, ammo_group_params) | ||||
|           ammo_type = maybe_get_ammo_type(pack_params, user) | ||||
|           container = maybe_get_container(pack_params, user) | ||||
|           pack |> Pack.create_changeset(ammo_type, container, user, pack_params) | ||||
|  | ||||
|         :update -> | ||||
|           ammo_group |> AmmoGroup.update_changeset(ammo_group_params, user) | ||||
|           pack |> Pack.update_changeset(pack_params, user) | ||||
|       end | ||||
|  | ||||
|     changeset = | ||||
| @@ -114,15 +114,14 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do | ||||
|  | ||||
|   defp maybe_get_ammo_type(_params_not_found, _user), do: nil | ||||
|  | ||||
|   defp save_ammo_group( | ||||
|          %{assigns: %{ammo_group: ammo_group, current_user: current_user, return_to: return_to}} = | ||||
|            socket, | ||||
|   defp save_pack( | ||||
|          %{assigns: %{pack: pack, current_user: current_user, return_to: return_to}} = socket, | ||||
|          :edit, | ||||
|          ammo_group_params | ||||
|          pack_params | ||||
|        ) do | ||||
|     socket = | ||||
|       case Ammo.update_ammo_group(ammo_group, ammo_group_params, current_user) do | ||||
|         {:ok, _ammo_group} -> | ||||
|       case Ammo.update_pack(pack, pack_params, current_user) do | ||||
|         {:ok, _pack} -> | ||||
|           prompt = dgettext("prompts", "Ammo updated successfully") | ||||
|           socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) | ||||
|  | ||||
| @@ -133,24 +132,24 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do | ||||
|     {:noreply, socket} | ||||
|   end | ||||
|  | ||||
|   defp save_ammo_group( | ||||
|   defp save_pack( | ||||
|          %{assigns: %{changeset: changeset}} = socket, | ||||
|          action, | ||||
|          %{"multiplier" => multiplier_str} = ammo_group_params | ||||
|          %{"multiplier" => multiplier_str} = pack_params | ||||
|        ) | ||||
|        when action in [:new, :clone] do | ||||
|     socket = | ||||
|       case multiplier_str |> Integer.parse() do | ||||
|         {multiplier, _remainder} | ||||
|         when multiplier >= 1 and multiplier <= @ammo_group_create_limit -> | ||||
|           socket |> create_multiple(ammo_group_params, multiplier) | ||||
|         when multiplier >= 1 and multiplier <= @pack_create_limit -> | ||||
|           socket |> create_multiple(pack_params, multiplier) | ||||
|  | ||||
|         {multiplier, _remainder} -> | ||||
|           error_msg = | ||||
|             dgettext( | ||||
|               "errors", | ||||
|               "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}", | ||||
|               max: @ammo_group_create_limit, | ||||
|               max: @pack_create_limit, | ||||
|               multiplier: multiplier | ||||
|             ) | ||||
|  | ||||
| @@ -176,11 +175,11 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do | ||||
|  | ||||
|   defp create_multiple( | ||||
|          %{assigns: %{current_user: current_user, return_to: return_to}} = socket, | ||||
|          ammo_group_params, | ||||
|          pack_params, | ||||
|          multiplier | ||||
|        ) do | ||||
|     case Ammo.create_ammo_groups(ammo_group_params, multiplier, current_user) do | ||||
|       {:ok, {count, _ammo_groups}} -> | ||||
|     case Ammo.create_packs(pack_params, multiplier, current_user) do | ||||
|       {:ok, {count, _packs}} -> | ||||
|         prompt = | ||||
|           dngettext( | ||||
|             "prompts", | ||||
|   | ||||
| @@ -6,7 +6,7 @@ | ||||
|   <.form | ||||
|     :let={f} | ||||
|     for={@changeset} | ||||
|     id="ammo_group-form" | ||||
|     id="pack-form" | ||||
|     phx-target={@myself} | ||||
|     phx-change="validate" | ||||
|     phx-submit="save" | ||||
| @@ -68,7 +68,7 @@ | ||||
|  | ||||
|         <%= label(f, :multiplier, gettext("Copies"), class: "title text-lg text-primary-600") %> | ||||
|         <%= number_input(f, :multiplier, | ||||
|           max: @ammo_group_create_limit, | ||||
|           max: @pack_create_limit, | ||||
|           class: "text-center input input-primary", | ||||
|           value: 1, | ||||
|           phx_update: "ignore" | ||||
|   | ||||
| @@ -1,28 +1,28 @@ | ||||
| defmodule CanneryWeb.AmmoGroupLive.Index do | ||||
| defmodule CanneryWeb.PackLive.Index do | ||||
|   @moduledoc """ | ||||
|   Liveview to show a Cannery.Ammo.AmmoGroup index | ||||
|   Liveview to show a Cannery.Ammo.Pack index | ||||
|   """ | ||||
|  | ||||
|   use CanneryWeb, :live_view | ||||
|   alias Cannery.{Ammo, Ammo.AmmoGroup, Containers} | ||||
|   alias Cannery.{Ammo, Ammo.Pack, Containers} | ||||
|  | ||||
|   @impl true | ||||
|   def mount(%{"search" => search}, _session, socket) do | ||||
|     socket = | ||||
|       socket | ||||
|       |> assign(class: :all, show_used: false, search: search) | ||||
|       |> display_ammo_groups() | ||||
|       |> display_packs() | ||||
|  | ||||
|     {:ok, socket} | ||||
|   end | ||||
|  | ||||
|   def mount(_params, _session, socket) do | ||||
|     {:ok, socket |> assign(class: :all, show_used: false, search: nil) |> display_ammo_groups()} | ||||
|     {:ok, socket |> assign(class: :all, show_used: false, search: nil) |> display_packs()} | ||||
|   end | ||||
|  | ||||
|   @impl true | ||||
|   def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do | ||||
|     {:noreply, apply_action(socket, live_action, params) |> display_ammo_groups()} | ||||
|     {:noreply, apply_action(socket, live_action, params) |> display_packs()} | ||||
|   end | ||||
|  | ||||
|   defp apply_action( | ||||
| @@ -33,7 +33,7 @@ defmodule CanneryWeb.AmmoGroupLive.Index do | ||||
|     socket | ||||
|     |> assign( | ||||
|       page_title: gettext("Record shots"), | ||||
|       ammo_group: Ammo.get_ammo_group!(id, current_user) | ||||
|       pack: Ammo.get_pack!(id, current_user) | ||||
|     ) | ||||
|   end | ||||
|  | ||||
| @@ -41,7 +41,7 @@ defmodule CanneryWeb.AmmoGroupLive.Index do | ||||
|     socket | ||||
|     |> assign( | ||||
|       page_title: gettext("Move ammo"), | ||||
|       ammo_group: Ammo.get_ammo_group!(id, current_user) | ||||
|       pack: Ammo.get_pack!(id, current_user) | ||||
|     ) | ||||
|   end | ||||
|  | ||||
| @@ -49,7 +49,7 @@ defmodule CanneryWeb.AmmoGroupLive.Index do | ||||
|     socket | ||||
|     |> assign( | ||||
|       page_title: gettext("Edit ammo"), | ||||
|       ammo_group: Ammo.get_ammo_group!(id, current_user) | ||||
|       pack: Ammo.get_pack!(id, current_user) | ||||
|     ) | ||||
|   end | ||||
|  | ||||
| @@ -57,7 +57,7 @@ defmodule CanneryWeb.AmmoGroupLive.Index do | ||||
|     socket | ||||
|     |> assign( | ||||
|       page_title: dgettext("actions", "Add Ammo"), | ||||
|       ammo_group: %{Ammo.get_ammo_group!(id, current_user) | id: nil} | ||||
|       pack: %{Ammo.get_pack!(id, current_user) | id: nil} | ||||
|     ) | ||||
|   end | ||||
|  | ||||
| @@ -65,7 +65,7 @@ defmodule CanneryWeb.AmmoGroupLive.Index do | ||||
|     socket | ||||
|     |> assign( | ||||
|       page_title: dgettext("actions", "Add Ammo"), | ||||
|       ammo_group: %AmmoGroup{} | ||||
|       pack: %Pack{} | ||||
|     ) | ||||
|   end | ||||
|  | ||||
| @@ -74,7 +74,7 @@ defmodule CanneryWeb.AmmoGroupLive.Index do | ||||
|     |> assign( | ||||
|       page_title: gettext("Ammo"), | ||||
|       search: nil, | ||||
|       ammo_group: nil | ||||
|       pack: nil | ||||
|     ) | ||||
|   end | ||||
|  | ||||
| @@ -83,64 +83,62 @@ defmodule CanneryWeb.AmmoGroupLive.Index do | ||||
|     |> assign( | ||||
|       page_title: gettext("Ammo"), | ||||
|       search: search, | ||||
|       ammo_group: nil | ||||
|       pack: nil | ||||
|     ) | ||||
|   end | ||||
|  | ||||
|   @impl true | ||||
|   def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do | ||||
|     Ammo.get_ammo_group!(id, current_user) |> Ammo.delete_ammo_group!(current_user) | ||||
|     Ammo.get_pack!(id, current_user) |> Ammo.delete_pack!(current_user) | ||||
|  | ||||
|     prompt = dgettext("prompts", "Ammo deleted succesfully") | ||||
|  | ||||
|     {:noreply, socket |> put_flash(:info, prompt) |> display_ammo_groups()} | ||||
|     {:noreply, socket |> put_flash(:info, prompt) |> display_packs()} | ||||
|   end | ||||
|  | ||||
|   def handle_event( | ||||
|         "toggle_staged", | ||||
|         %{"ammo_group_id" => id}, | ||||
|         %{"pack_id" => id}, | ||||
|         %{assigns: %{current_user: current_user}} = socket | ||||
|       ) do | ||||
|     ammo_group = Ammo.get_ammo_group!(id, current_user) | ||||
|     pack = Ammo.get_pack!(id, current_user) | ||||
|  | ||||
|     {:ok, _ammo_group} = | ||||
|       ammo_group |> Ammo.update_ammo_group(%{"staged" => !ammo_group.staged}, current_user) | ||||
|     {:ok, _pack} = pack |> Ammo.update_pack(%{"staged" => !pack.staged}, current_user) | ||||
|  | ||||
|     {:noreply, socket |> display_ammo_groups()} | ||||
|     {:noreply, socket |> display_packs()} | ||||
|   end | ||||
|  | ||||
|   def handle_event("toggle_show_used", _params, %{assigns: %{show_used: show_used}} = socket) do | ||||
|     {:noreply, socket |> assign(:show_used, !show_used) |> display_ammo_groups()} | ||||
|     {:noreply, socket |> assign(:show_used, !show_used) |> display_packs()} | ||||
|   end | ||||
|  | ||||
|   def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do | ||||
|     {:noreply, socket |> push_patch(to: Routes.ammo_group_index_path(Endpoint, :index))} | ||||
|     {:noreply, socket |> push_patch(to: Routes.pack_index_path(Endpoint, :index))} | ||||
|   end | ||||
|  | ||||
|   def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do | ||||
|     socket = | ||||
|       socket |> push_patch(to: Routes.ammo_group_index_path(Endpoint, :search, search_term)) | ||||
|     socket = socket |> push_patch(to: Routes.pack_index_path(Endpoint, :search, search_term)) | ||||
|  | ||||
|     {:noreply, socket} | ||||
|   end | ||||
|  | ||||
|   def handle_event("change_class", %{"ammo_type" => %{"class" => "rifle"}}, socket) do | ||||
|     {:noreply, socket |> assign(:class, :rifle) |> display_ammo_groups()} | ||||
|     {:noreply, socket |> assign(:class, :rifle) |> display_packs()} | ||||
|   end | ||||
|  | ||||
|   def handle_event("change_class", %{"ammo_type" => %{"class" => "shotgun"}}, socket) do | ||||
|     {:noreply, socket |> assign(:class, :shotgun) |> display_ammo_groups()} | ||||
|     {:noreply, socket |> assign(:class, :shotgun) |> display_packs()} | ||||
|   end | ||||
|  | ||||
|   def handle_event("change_class", %{"ammo_type" => %{"class" => "pistol"}}, socket) do | ||||
|     {:noreply, socket |> assign(:class, :pistol) |> display_ammo_groups()} | ||||
|     {:noreply, socket |> assign(:class, :pistol) |> display_packs()} | ||||
|   end | ||||
|  | ||||
|   def handle_event("change_class", %{"ammo_type" => %{"class" => _all}}, socket) do | ||||
|     {:noreply, socket |> assign(:class, :all) |> display_ammo_groups()} | ||||
|     {:noreply, socket |> assign(:class, :all) |> display_packs()} | ||||
|   end | ||||
|  | ||||
|   defp display_ammo_groups( | ||||
|   defp display_packs( | ||||
|          %{ | ||||
|            assigns: %{ | ||||
|              class: class, | ||||
| @@ -152,17 +150,17 @@ defmodule CanneryWeb.AmmoGroupLive.Index do | ||||
|        ) do | ||||
|     # get total number of ammo groups to determine whether to display onboarding | ||||
|     # prompts | ||||
|     ammo_groups_count = Ammo.get_ammo_groups_count!(current_user, true) | ||||
|     ammo_groups = Ammo.list_ammo_groups(search, class, current_user, show_used) | ||||
|     packs_count = Ammo.get_packs_count!(current_user, true) | ||||
|     packs = Ammo.list_packs(search, class, current_user, show_used) | ||||
|     ammo_types_count = Ammo.get_ammo_types_count!(current_user) | ||||
|     containers_count = Containers.get_containers_count!(current_user) | ||||
|  | ||||
|     socket | ||||
|     |> assign( | ||||
|       ammo_groups: ammo_groups, | ||||
|       packs: packs, | ||||
|       ammo_types_count: ammo_types_count, | ||||
|       containers_count: containers_count, | ||||
|       ammo_groups_count: ammo_groups_count | ||||
|       packs_count: packs_count | ||||
|     ) | ||||
|   end | ||||
| end | ||||
|   | ||||
| @@ -24,17 +24,17 @@ | ||||
|           <%= dgettext("actions", "add an ammo type first") %> | ||||
|         </.link> | ||||
|       </div> | ||||
|     <% @ammo_groups_count == 0 -> %> | ||||
|     <% @packs_count == 0 -> %> | ||||
|       <h2 class="title text-xl text-primary-600"> | ||||
|         <%= gettext("No ammo") %> | ||||
|         <%= display_emoji("😔") %> | ||||
|       </h2> | ||||
|  | ||||
|       <.link patch={Routes.ammo_group_index_path(Endpoint, :new)} class="btn btn-primary"> | ||||
|       <.link patch={Routes.pack_index_path(Endpoint, :new)} class="btn btn-primary"> | ||||
|         <%= dgettext("actions", "Add your first box!") %> | ||||
|       </.link> | ||||
|     <% true -> %> | ||||
|       <.link patch={Routes.ammo_group_index_path(Endpoint, :new)} class="btn btn-primary"> | ||||
|       <.link patch={Routes.pack_index_path(Endpoint, :new)} class="btn btn-primary"> | ||||
|         <%= dgettext("actions", "Add Ammo") %> | ||||
|       </.link> | ||||
|  | ||||
| @@ -89,16 +89,16 @@ | ||||
|         </.toggle_button> | ||||
|       </div> | ||||
|  | ||||
|       <%= if @ammo_groups |> Enum.empty?() do %> | ||||
|       <%= if @packs |> Enum.empty?() do %> | ||||
|         <h2 class="title text-xl text-primary-600"> | ||||
|           <%= gettext("No Ammo") %> | ||||
|           <%= display_emoji("😔") %> | ||||
|         </h2> | ||||
|       <% else %> | ||||
|         <.live_component | ||||
|           module={CanneryWeb.Components.AmmoGroupTableComponent} | ||||
|           module={CanneryWeb.Components.PackTableComponent} | ||||
|           id="ammo-group-index-table" | ||||
|           ammo_groups={@ammo_groups} | ||||
|           packs={@packs} | ||||
|           current_user={@current_user} | ||||
|           show_used={@show_used} | ||||
|         > | ||||
| @@ -107,28 +107,28 @@ | ||||
|               <%= ammo_type_name %> | ||||
|             </.link> | ||||
|           </:ammo_type> | ||||
|           <:range :let={ammo_group}> | ||||
|           <:range :let={pack}> | ||||
|             <div class="min-w-20 py-2 px-4 h-full flex flew-wrap justify-center items-center"> | ||||
|               <button | ||||
|                 type="button" | ||||
|                 class="mx-2 my-1 text-sm btn btn-primary" | ||||
|                 phx-click="toggle_staged" | ||||
|                 phx-value-ammo_group_id={ammo_group.id} | ||||
|                 phx-value-pack_id={pack.id} | ||||
|               > | ||||
|                 <%= if ammo_group.staged, | ||||
|                 <%= if pack.staged, | ||||
|                   do: dgettext("actions", "Unstage"), | ||||
|                   else: dgettext("actions", "Stage") %> | ||||
|               </button> | ||||
|  | ||||
|               <.link | ||||
|                 patch={Routes.ammo_group_index_path(Endpoint, :add_shot_group, ammo_group)} | ||||
|                 patch={Routes.pack_index_path(Endpoint, :add_shot_group, pack)} | ||||
|                 class="mx-2 my-1 text-sm btn btn-primary" | ||||
|               > | ||||
|                 <%= dgettext("actions", "Record shots") %> | ||||
|               </.link> | ||||
|             </div> | ||||
|           </:range> | ||||
|           <:container :let={{ammo_group, %{name: container_name} = container}}> | ||||
|           <:container :let={{pack, %{name: container_name} = container}}> | ||||
|             <div class="min-w-20 py-2 px-4 h-full flex flew-wrap justify-center items-center"> | ||||
|               <.link | ||||
|                 navigate={Routes.container_show_path(Endpoint, :show, container)} | ||||
| @@ -138,21 +138,21 @@ | ||||
|               </.link> | ||||
|  | ||||
|               <.link | ||||
|                 patch={Routes.ammo_group_index_path(Endpoint, :move, ammo_group)} | ||||
|                 patch={Routes.pack_index_path(Endpoint, :move, pack)} | ||||
|                 class="mx-2 my-1 text-sm btn btn-primary" | ||||
|               > | ||||
|                 <%= dgettext("actions", "Move ammo") %> | ||||
|               </.link> | ||||
|             </div> | ||||
|           </:container> | ||||
|           <:actions :let={%{count: ammo_group_count} = ammo_group}> | ||||
|           <:actions :let={%{count: pack_count} = pack}> | ||||
|             <div class="py-2 px-4 h-full space-x-4 flex justify-center items-center"> | ||||
|               <.link | ||||
|                 navigate={Routes.ammo_group_show_path(Endpoint, :show, ammo_group)} | ||||
|                 navigate={Routes.pack_show_path(Endpoint, :show, pack)} | ||||
|                 class="text-primary-600 link" | ||||
|                 aria-label={ | ||||
|                   dgettext("actions", "View ammo group of %{ammo_group_count} bullets", | ||||
|                     ammo_group_count: ammo_group_count | ||||
|                   dgettext("actions", "View ammo group of %{pack_count} bullets", | ||||
|                     pack_count: pack_count | ||||
|                   ) | ||||
|                 } | ||||
|               > | ||||
| @@ -160,11 +160,11 @@ | ||||
|               </.link> | ||||
|  | ||||
|               <.link | ||||
|                 patch={Routes.ammo_group_index_path(Endpoint, :edit, ammo_group)} | ||||
|                 patch={Routes.pack_index_path(Endpoint, :edit, pack)} | ||||
|                 class="text-primary-600 link" | ||||
|                 aria-label={ | ||||
|                   dgettext("actions", "Edit ammo group of %{ammo_group_count} bullets", | ||||
|                     ammo_group_count: ammo_group_count | ||||
|                   dgettext("actions", "Edit ammo group of %{pack_count} bullets", | ||||
|                     pack_count: pack_count | ||||
|                   ) | ||||
|                 } | ||||
|               > | ||||
| @@ -172,11 +172,11 @@ | ||||
|               </.link> | ||||
|  | ||||
|               <.link | ||||
|                 patch={Routes.ammo_group_index_path(Endpoint, :clone, ammo_group)} | ||||
|                 patch={Routes.pack_index_path(Endpoint, :clone, pack)} | ||||
|                 class="text-primary-600 link" | ||||
|                 aria-label={ | ||||
|                   dgettext("actions", "Clone ammo group of %{ammo_group_count} bullets", | ||||
|                     ammo_group_count: ammo_group_count | ||||
|                   dgettext("actions", "Clone ammo group of %{pack_count} bullets", | ||||
|                     pack_count: pack_count | ||||
|                   ) | ||||
|                 } | ||||
|               > | ||||
| @@ -187,11 +187,11 @@ | ||||
|                 href="#" | ||||
|                 class="text-primary-600 link" | ||||
|                 phx-click="delete" | ||||
|                 phx-value-id={ammo_group.id} | ||||
|                 phx-value-id={pack.id} | ||||
|                 data-confirm={dgettext("prompts", "Are you sure you want to delete this ammo?")} | ||||
|                 aria-label={ | ||||
|                   dgettext("actions", "Delete ammo group of %{ammo_group_count} bullets", | ||||
|                     ammo_group_count: ammo_group_count | ||||
|                   dgettext("actions", "Delete ammo group of %{pack_count} bullets", | ||||
|                     pack_count: pack_count | ||||
|                   ) | ||||
|                 } | ||||
|               > | ||||
| @@ -206,38 +206,38 @@ | ||||
|  | ||||
| <%= case @live_action do %> | ||||
|   <% create when create in [:new, :edit, :clone] -> %> | ||||
|     <.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}> | ||||
|     <.modal return_to={Routes.pack_index_path(Endpoint, :index)}> | ||||
|       <.live_component | ||||
|         module={CanneryWeb.AmmoGroupLive.FormComponent} | ||||
|         id={@ammo_group.id || :new} | ||||
|         module={CanneryWeb.PackLive.FormComponent} | ||||
|         id={@pack.id || :new} | ||||
|         title={@page_title} | ||||
|         action={@live_action} | ||||
|         ammo_group={@ammo_group} | ||||
|         return_to={Routes.ammo_group_index_path(Endpoint, :index)} | ||||
|         pack={@pack} | ||||
|         return_to={Routes.pack_index_path(Endpoint, :index)} | ||||
|         current_user={@current_user} | ||||
|       /> | ||||
|     </.modal> | ||||
|   <% :add_shot_group -> %> | ||||
|     <.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}> | ||||
|     <.modal return_to={Routes.pack_index_path(Endpoint, :index)}> | ||||
|       <.live_component | ||||
|         module={CanneryWeb.Components.AddShotGroupComponent} | ||||
|         id={:new} | ||||
|         title={@page_title} | ||||
|         action={@live_action} | ||||
|         ammo_group={@ammo_group} | ||||
|         return_to={Routes.ammo_group_index_path(Endpoint, :index)} | ||||
|         pack={@pack} | ||||
|         return_to={Routes.pack_index_path(Endpoint, :index)} | ||||
|         current_user={@current_user} | ||||
|       /> | ||||
|     </.modal> | ||||
|   <% :move -> %> | ||||
|     <.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}> | ||||
|     <.modal return_to={Routes.pack_index_path(Endpoint, :index)}> | ||||
|       <.live_component | ||||
|         module={CanneryWeb.Components.MoveAmmoGroupComponent} | ||||
|         id={@ammo_group.id} | ||||
|         module={CanneryWeb.Components.MovePackComponent} | ||||
|         id={@pack.id} | ||||
|         title={@page_title} | ||||
|         action={@live_action} | ||||
|         ammo_group={@ammo_group} | ||||
|         return_to={Routes.ammo_group_index_path(Endpoint, :index)} | ||||
|         pack={@pack} | ||||
|         return_to={Routes.pack_index_path(Endpoint, :index)} | ||||
|         current_user={@current_user} | ||||
|       /> | ||||
|     </.modal> | ||||
|   | ||||
| @@ -1,11 +1,11 @@ | ||||
| defmodule CanneryWeb.AmmoGroupLive.Show do | ||||
| defmodule CanneryWeb.PackLive.Show do | ||||
|   @moduledoc """ | ||||
|   Liveview for showing and editing an Cannery.Ammo.AmmoGroup | ||||
|   Liveview for showing and editing an Cannery.Ammo.Pack | ||||
|   """ | ||||
|  | ||||
|   use CanneryWeb, :live_view | ||||
|   alias Cannery.{ActivityLog, ActivityLog.ShotGroup} | ||||
|   alias Cannery.{Ammo, Ammo.AmmoGroup} | ||||
|   alias Cannery.{Ammo, Ammo.Pack} | ||||
|   alias Cannery.{ComparableDate, Containers} | ||||
|   alias CanneryWeb.Endpoint | ||||
|   alias Phoenix.LiveView.Socket | ||||
| @@ -24,7 +24,7 @@ defmodule CanneryWeb.AmmoGroupLive.Show do | ||||
|     socket = | ||||
|       socket | ||||
|       |> assign(page_title: page_title(live_action), shot_group: shot_group) | ||||
|       |> display_ammo_group(id) | ||||
|       |> display_pack(id) | ||||
|  | ||||
|     {:noreply, socket} | ||||
|   end | ||||
| @@ -33,7 +33,7 @@ defmodule CanneryWeb.AmmoGroupLive.Show do | ||||
|     socket = | ||||
|       socket | ||||
|       |> assign(page_title: page_title(live_action)) | ||||
|       |> display_ammo_group(id) | ||||
|       |> display_pack(id) | ||||
|  | ||||
|     {:noreply, socket} | ||||
|   end | ||||
| @@ -48,12 +48,12 @@ defmodule CanneryWeb.AmmoGroupLive.Show do | ||||
|   def handle_event( | ||||
|         "delete", | ||||
|         _params, | ||||
|         %{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket | ||||
|         %{assigns: %{pack: pack, current_user: current_user}} = socket | ||||
|       ) do | ||||
|     ammo_group |> Ammo.delete_ammo_group!(current_user) | ||||
|     pack |> Ammo.delete_pack!(current_user) | ||||
|  | ||||
|     prompt = dgettext("prompts", "Ammo deleted succesfully") | ||||
|     redirect_to = Routes.ammo_group_index_path(socket, :index) | ||||
|     redirect_to = Routes.pack_index_path(socket, :index) | ||||
|  | ||||
|     {:noreply, socket |> put_flash(:info, prompt) |> push_navigate(to: redirect_to)} | ||||
|   end | ||||
| @@ -61,31 +61,30 @@ defmodule CanneryWeb.AmmoGroupLive.Show do | ||||
|   def handle_event( | ||||
|         "toggle_staged", | ||||
|         _params, | ||||
|         %{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket | ||||
|         %{assigns: %{pack: pack, current_user: current_user}} = socket | ||||
|       ) do | ||||
|     {:ok, ammo_group} = | ||||
|       ammo_group |> Ammo.update_ammo_group(%{"staged" => !ammo_group.staged}, current_user) | ||||
|     {:ok, pack} = pack |> Ammo.update_pack(%{"staged" => !pack.staged}, current_user) | ||||
|  | ||||
|     {:noreply, socket |> display_ammo_group(ammo_group)} | ||||
|     {:noreply, socket |> display_pack(pack)} | ||||
|   end | ||||
|  | ||||
|   def handle_event( | ||||
|         "delete_shot_group", | ||||
|         %{"id" => id}, | ||||
|         %{assigns: %{ammo_group: %{id: ammo_group_id}, current_user: current_user}} = socket | ||||
|         %{assigns: %{pack: %{id: pack_id}, current_user: current_user}} = socket | ||||
|       ) do | ||||
|     {:ok, _} = | ||||
|       ActivityLog.get_shot_group!(id, current_user) | ||||
|       |> ActivityLog.delete_shot_group(current_user) | ||||
|  | ||||
|     prompt = dgettext("prompts", "Shot records deleted succesfully") | ||||
|     {:noreply, socket |> put_flash(:info, prompt) |> display_ammo_group(ammo_group_id)} | ||||
|     {:noreply, socket |> put_flash(:info, prompt) |> display_pack(pack_id)} | ||||
|   end | ||||
|  | ||||
|   @spec display_ammo_group(Socket.t(), AmmoGroup.t() | AmmoGroup.id()) :: Socket.t() | ||||
|   defp display_ammo_group( | ||||
|   @spec display_pack(Socket.t(), Pack.t() | Pack.id()) :: Socket.t() | ||||
|   defp display_pack( | ||||
|          %{assigns: %{current_user: current_user}} = socket, | ||||
|          %AmmoGroup{container_id: container_id} = ammo_group | ||||
|          %Pack{container_id: container_id} = pack | ||||
|        ) do | ||||
|     columns = [ | ||||
|       %{label: gettext("Rounds shot"), key: :count}, | ||||
| @@ -94,19 +93,19 @@ defmodule CanneryWeb.AmmoGroupLive.Show do | ||||
|       %{label: gettext("Actions"), key: :actions, sortable: false} | ||||
|     ] | ||||
|  | ||||
|     shot_groups = ActivityLog.list_shot_groups_for_ammo_group(ammo_group, current_user) | ||||
|     shot_groups = ActivityLog.list_shot_groups_for_pack(pack, current_user) | ||||
|  | ||||
|     rows = | ||||
|       shot_groups | ||||
|       |> Enum.map(fn shot_group -> | ||||
|         ammo_group |> get_table_row_for_shot_group(shot_group, columns) | ||||
|         pack |> get_table_row_for_shot_group(shot_group, columns) | ||||
|       end) | ||||
|  | ||||
|     socket | ||||
|     |> assign( | ||||
|       ammo_group: ammo_group, | ||||
|       original_count: Ammo.get_original_count(ammo_group, current_user), | ||||
|       percentage_remaining: Ammo.get_percentage_remaining(ammo_group, current_user), | ||||
|       pack: pack, | ||||
|       original_count: Ammo.get_original_count(pack, current_user), | ||||
|       percentage_remaining: Ammo.get_percentage_remaining(pack, current_user), | ||||
|       container: container_id && Containers.get_container!(container_id, current_user), | ||||
|       shot_groups: shot_groups, | ||||
|       columns: columns, | ||||
| @@ -114,15 +113,15 @@ defmodule CanneryWeb.AmmoGroupLive.Show do | ||||
|     ) | ||||
|   end | ||||
|  | ||||
|   defp display_ammo_group(%{assigns: %{current_user: current_user}} = socket, id), | ||||
|     do: display_ammo_group(socket, Ammo.get_ammo_group!(id, current_user)) | ||||
|   defp display_pack(%{assigns: %{current_user: current_user}} = socket, id), | ||||
|     do: display_pack(socket, Ammo.get_pack!(id, current_user)) | ||||
|  | ||||
|   @spec display_currency(float()) :: String.t() | ||||
|   defp display_currency(float), do: :erlang.float_to_binary(float, decimals: 2) | ||||
|  | ||||
|   @spec get_table_row_for_shot_group(AmmoGroup.t(), ShotGroup.t(), [map()]) :: map() | ||||
|   defp get_table_row_for_shot_group(ammo_group, %{id: id, date: date} = shot_group, columns) do | ||||
|     assigns = %{ammo_group: ammo_group, shot_group: shot_group} | ||||
|   @spec get_table_row_for_shot_group(Pack.t(), ShotGroup.t(), [map()]) :: map() | ||||
|   defp get_table_row_for_shot_group(pack, %{id: id, date: date} = shot_group, columns) do | ||||
|     assigns = %{pack: pack, shot_group: shot_group} | ||||
|  | ||||
|     columns | ||||
|     |> Map.new(fn %{key: key} -> | ||||
| @@ -140,7 +139,7 @@ defmodule CanneryWeb.AmmoGroupLive.Show do | ||||
|             ~H""" | ||||
|             <div class="px-4 py-2 space-x-4 flex justify-center items-center"> | ||||
|               <.link | ||||
|                 patch={Routes.ammo_group_show_path(Endpoint, :edit_shot_group, @ammo_group, @shot_group)} | ||||
|                 patch={Routes.pack_show_path(Endpoint, :edit_shot_group, @pack, @shot_group)} | ||||
|                 class="text-primary-600 link" | ||||
|                 aria-label={ | ||||
|                   dgettext("actions", "Edit shot group of %{shot_group_count} shots", | ||||
|   | ||||
| @@ -1,12 +1,12 @@ | ||||
| <div class="mx-auto space-y-4 max-w-3xl flex flex-col justify-center items-center"> | ||||
|   <h1 class="title text-2xl title-primary-500"> | ||||
|     <%= @ammo_group.ammo_type.name %> | ||||
|     <%= @pack.ammo_type.name %> | ||||
|   </h1> | ||||
|  | ||||
|   <div class="space-y-2 flex flex-col justify-center items-center"> | ||||
|     <span class="rounded-lg title text-lg"> | ||||
|       <%= gettext("Count:") %> | ||||
|       <%= @ammo_group.count %> | ||||
|       <%= @pack.count %> | ||||
|     </span> | ||||
|  | ||||
|     <span class="rounded-lg title text-lg"> | ||||
| @@ -19,28 +19,28 @@ | ||||
|       <%= gettext("%{percentage}%", percentage: @percentage_remaining) %> | ||||
|     </span> | ||||
|  | ||||
|     <%= if @ammo_group.notes do %> | ||||
|     <%= if @pack.notes do %> | ||||
|       <span class="rounded-lg title text-lg"> | ||||
|         <%= gettext("Notes:") %> | ||||
|         <%= @ammo_group.notes %> | ||||
|         <%= @pack.notes %> | ||||
|       </span> | ||||
|     <% end %> | ||||
|  | ||||
|     <span class="rounded-lg title text-lg"> | ||||
|       <%= gettext("Purchased on:") %> | ||||
|       <.date id={"#{@ammo_group.id}-purchased-on"} date={@ammo_group.purchased_on} /> | ||||
|       <.date id={"#{@pack.id}-purchased-on"} date={@pack.purchased_on} /> | ||||
|     </span> | ||||
|  | ||||
|     <%= if @ammo_group.price_paid do %> | ||||
|     <%= if @pack.price_paid do %> | ||||
|       <span class="rounded-lg title text-lg"> | ||||
|         <%= gettext("Original cost:") %> | ||||
|         <%= gettext("$%{amount}", amount: display_currency(@ammo_group.price_paid)) %> | ||||
|         <%= gettext("$%{amount}", amount: display_currency(@pack.price_paid)) %> | ||||
|       </span> | ||||
|  | ||||
|       <span class="rounded-lg title text-lg"> | ||||
|         <%= gettext("Current value:") %> | ||||
|         <%= gettext("$%{amount}", | ||||
|           amount: display_currency(@ammo_group.price_paid * @percentage_remaining / 100) | ||||
|           amount: display_currency(@pack.price_paid * @percentage_remaining / 100) | ||||
|         ) %> | ||||
|       </span> | ||||
|     <% end %> | ||||
| @@ -49,19 +49,17 @@ | ||||
|   <div class="flex flex-col justify-center items-center"> | ||||
|     <div class="flex flex-wrap justify-center items-center text-primary-600"> | ||||
|       <.link | ||||
|         navigate={Routes.ammo_type_show_path(Endpoint, :show, @ammo_group.ammo_type)} | ||||
|         navigate={Routes.ammo_type_show_path(Endpoint, :show, @pack.ammo_type)} | ||||
|         class="mx-4 my-2 btn btn-primary" | ||||
|       > | ||||
|         <%= dgettext("actions", "View in Catalog") %> | ||||
|       </.link> | ||||
|  | ||||
|       <.link | ||||
|         patch={Routes.ammo_group_show_path(Endpoint, :edit, @ammo_group)} | ||||
|         patch={Routes.pack_show_path(Endpoint, :edit, @pack)} | ||||
|         class="mx-4 my-2 text-primary-600 link" | ||||
|         aria-label={ | ||||
|           dgettext("actions", "Edit ammo group of %{ammo_group_count} bullets", | ||||
|             ammo_group_count: @ammo_group.count | ||||
|           ) | ||||
|           dgettext("actions", "Edit ammo group of %{pack_count} bullets", pack_count: @pack.count) | ||||
|         } | ||||
|       > | ||||
|         <i class="fa-fw fa-lg fas fa-edit"></i> | ||||
| @@ -73,8 +71,8 @@ | ||||
|         phx-click="delete" | ||||
|         data-confirm={dgettext("prompts", "Are you sure you want to delete this ammo?")} | ||||
|         aria-label={ | ||||
|           dgettext("actions", "Delete ammo group of %{ammo_group_count} bullets", | ||||
|             ammo_group_count: @ammo_group.count | ||||
|           dgettext("actions", "Delete ammo group of %{pack_count} bullets", | ||||
|             pack_count: @pack.count | ||||
|           ) | ||||
|         } | ||||
|       > | ||||
| @@ -84,20 +82,17 @@ | ||||
|  | ||||
|     <div class="flex flex-wrap justify-center items-center text-primary-600"> | ||||
|       <button type="button" class="mx-4 my-2 btn btn-primary" phx-click="toggle_staged"> | ||||
|         <%= if @ammo_group.staged, | ||||
|         <%= if @pack.staged, | ||||
|           do: dgettext("actions", "Unstage from range"), | ||||
|           else: dgettext("actions", "Stage for range") %> | ||||
|       </button> | ||||
|  | ||||
|       <.link | ||||
|         patch={Routes.ammo_group_show_path(Endpoint, :move, @ammo_group)} | ||||
|         class="btn btn-primary" | ||||
|       > | ||||
|       <.link patch={Routes.pack_show_path(Endpoint, :move, @pack)} class="btn btn-primary"> | ||||
|         <%= dgettext("actions", "Move ammo") %> | ||||
|       </.link> | ||||
|  | ||||
|       <.link | ||||
|         patch={Routes.ammo_group_show_path(Endpoint, :add_shot_group, @ammo_group)} | ||||
|         patch={Routes.pack_show_path(Endpoint, :add_shot_group, @pack)} | ||||
|         class="mx-4 my-2 btn btn-primary" | ||||
|       > | ||||
|         <%= dgettext("actions", "Record shots") %> | ||||
| @@ -128,7 +123,7 @@ | ||||
|  | ||||
|     <.live_component | ||||
|       module={CanneryWeb.Components.TableComponent} | ||||
|       id="ammo_group_shot_groups_table" | ||||
|       id="pack_shot_groups_table" | ||||
|       columns={@columns} | ||||
|       rows={@rows} | ||||
|     /> | ||||
| @@ -137,50 +132,50 @@ | ||||
|  | ||||
| <%= case @live_action do %> | ||||
|   <% :edit -> %> | ||||
|     <.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}> | ||||
|     <.modal return_to={Routes.pack_show_path(Endpoint, :show, @pack)}> | ||||
|       <.live_component | ||||
|         module={CanneryWeb.AmmoGroupLive.FormComponent} | ||||
|         id={@ammo_group.id} | ||||
|         module={CanneryWeb.PackLive.FormComponent} | ||||
|         id={@pack.id} | ||||
|         title={@page_title} | ||||
|         action={@live_action} | ||||
|         ammo_group={@ammo_group} | ||||
|         return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} | ||||
|         pack={@pack} | ||||
|         return_to={Routes.pack_show_path(Endpoint, :show, @pack)} | ||||
|         current_user={@current_user} | ||||
|       /> | ||||
|     </.modal> | ||||
|   <% :edit_shot_group -> %> | ||||
|     <.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}> | ||||
|     <.modal return_to={Routes.pack_show_path(Endpoint, :show, @pack)}> | ||||
|       <.live_component | ||||
|         module={CanneryWeb.RangeLive.FormComponent} | ||||
|         id={@shot_group.id} | ||||
|         title={@page_title} | ||||
|         action={@live_action} | ||||
|         shot_group={@shot_group} | ||||
|         return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} | ||||
|         return_to={Routes.pack_show_path(Endpoint, :show, @pack)} | ||||
|         current_user={@current_user} | ||||
|       /> | ||||
|     </.modal> | ||||
|   <% :add_shot_group -> %> | ||||
|     <.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}> | ||||
|     <.modal return_to={Routes.pack_show_path(Endpoint, :show, @pack)}> | ||||
|       <.live_component | ||||
|         module={CanneryWeb.Components.AddShotGroupComponent} | ||||
|         id={:new} | ||||
|         title={@page_title} | ||||
|         action={@live_action} | ||||
|         ammo_group={@ammo_group} | ||||
|         return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} | ||||
|         pack={@pack} | ||||
|         return_to={Routes.pack_show_path(Endpoint, :show, @pack)} | ||||
|         current_user={@current_user} | ||||
|       /> | ||||
|     </.modal> | ||||
|   <% :move -> %> | ||||
|     <.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}> | ||||
|     <.modal return_to={Routes.pack_show_path(Endpoint, :show, @pack)}> | ||||
|       <.live_component | ||||
|         module={CanneryWeb.Components.MoveAmmoGroupComponent} | ||||
|         id={@ammo_group.id} | ||||
|         module={CanneryWeb.Components.MovePackComponent} | ||||
|         id={@pack.id} | ||||
|         title={@page_title} | ||||
|         action={@live_action} | ||||
|         ammo_group={@ammo_group} | ||||
|         return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} | ||||
|         pack={@pack} | ||||
|         return_to={Routes.pack_show_path(Endpoint, :show, @pack)} | ||||
|         current_user={@current_user} | ||||
|       /> | ||||
|     </.modal> | ||||
|   | ||||
| @@ -55,7 +55,7 @@ defmodule CanneryWeb.AmmoTypeLive.Show do | ||||
|         ammo_type |> Map.get(field) != default_value | ||||
|       end) | ||||
|  | ||||
|     ammo_groups = ammo_type |> Ammo.list_ammo_groups_for_type(current_user, show_used) | ||||
|     packs = ammo_type |> Ammo.list_packs_for_type(current_user, show_used) | ||||
|  | ||||
|     [ | ||||
|       original_counts, | ||||
| @@ -66,9 +66,9 @@ defmodule CanneryWeb.AmmoTypeLive.Show do | ||||
|     ] = | ||||
|       if show_used do | ||||
|         [ | ||||
|           ammo_groups |> Ammo.get_original_counts(current_user), | ||||
|           ammo_type |> Ammo.get_used_ammo_groups_count_for_type(current_user), | ||||
|           ammo_type |> Ammo.get_ammo_groups_count_for_type(current_user, true), | ||||
|           packs |> Ammo.get_original_counts(current_user), | ||||
|           ammo_type |> Ammo.get_used_packs_count_for_type(current_user), | ||||
|           ammo_type |> Ammo.get_packs_count_for_type(current_user, true), | ||||
|           ammo_type |> ActivityLog.get_used_count_for_ammo_type(current_user), | ||||
|           ammo_type |> Ammo.get_historical_count_for_ammo_type(current_user) | ||||
|         ] | ||||
| @@ -83,7 +83,7 @@ defmodule CanneryWeb.AmmoTypeLive.Show do | ||||
|       end | ||||
|  | ||||
|     containers = | ||||
|       ammo_groups | ||||
|       packs | ||||
|       |> Enum.map(fn %{container_id: container_id} -> container_id end) | ||||
|       |> Containers.get_containers(current_user) | ||||
|  | ||||
| @@ -91,16 +91,16 @@ defmodule CanneryWeb.AmmoTypeLive.Show do | ||||
|     |> assign( | ||||
|       page_title: page_title, | ||||
|       ammo_type: ammo_type, | ||||
|       ammo_groups: ammo_groups, | ||||
|       packs: packs, | ||||
|       containers: containers, | ||||
|       cprs: ammo_groups |> Ammo.get_cprs(current_user), | ||||
|       last_used_dates: ammo_groups |> ActivityLog.get_last_used_dates(current_user), | ||||
|       cprs: packs |> Ammo.get_cprs(current_user), | ||||
|       last_used_dates: packs |> ActivityLog.get_last_used_dates(current_user), | ||||
|       avg_cost_per_round: ammo_type |> Ammo.get_average_cost_for_ammo_type(current_user), | ||||
|       rounds: ammo_type |> Ammo.get_round_count_for_ammo_type(current_user), | ||||
|       original_counts: original_counts, | ||||
|       used_rounds: used_rounds, | ||||
|       historical_round_count: historical_round_count, | ||||
|       packs_count: ammo_type |> Ammo.get_ammo_groups_count_for_type(current_user), | ||||
|       packs_count: ammo_type |> Ammo.get_packs_count_for_type(current_user), | ||||
|       used_packs_count: used_packs_count, | ||||
|       historical_packs_count: historical_packs_count, | ||||
|       fields_to_display: fields_to_display(ammo_type), | ||||
|   | ||||
| @@ -175,7 +175,7 @@ | ||||
|   </div> | ||||
|  | ||||
|   <div class="w-full p-4"> | ||||
|     <%= if @ammo_groups |> Enum.empty?() do %> | ||||
|     <%= if @packs |> Enum.empty?() do %> | ||||
|       <h2 class="px-4 title text-lg text-primary-600"> | ||||
|         <%= gettext("No ammo for this type") %> | ||||
|         <%= display_emoji("😔") %> | ||||
| @@ -183,13 +183,13 @@ | ||||
|     <% else %> | ||||
|       <%= if @view_table do %> | ||||
|         <.live_component | ||||
|           module={CanneryWeb.Components.AmmoGroupTableComponent} | ||||
|           module={CanneryWeb.Components.PackTableComponent} | ||||
|           id="ammo-type-show-table" | ||||
|           ammo_groups={@ammo_groups} | ||||
|           packs={@packs} | ||||
|           current_user={@current_user} | ||||
|           show_used={@show_used} | ||||
|         > | ||||
|           <:container :let={{_ammo_group, %{name: container_name} = container}}> | ||||
|           <:container :let={{_pack, %{name: container_name} = container}}> | ||||
|             <.link | ||||
|               navigate={Routes.container_show_path(Endpoint, :show, container)} | ||||
|               class="mx-2 my-1 link" | ||||
| @@ -197,14 +197,14 @@ | ||||
|               <%= container_name %> | ||||
|             </.link> | ||||
|           </:container> | ||||
|           <:actions :let={%{count: ammo_group_count} = ammo_group}> | ||||
|           <:actions :let={%{count: pack_count} = pack}> | ||||
|             <div class="py-2 px-4 h-full space-x-4 flex justify-center items-center"> | ||||
|               <.link | ||||
|                 navigate={Routes.ammo_group_show_path(Endpoint, :show, ammo_group)} | ||||
|                 navigate={Routes.pack_show_path(Endpoint, :show, pack)} | ||||
|                 class="text-primary-600 link" | ||||
|                 aria-label={ | ||||
|                   dgettext("actions", "View ammo group of %{ammo_group_count} bullets", | ||||
|                     ammo_group_count: ammo_group_count | ||||
|                   dgettext("actions", "View ammo group of %{pack_count} bullets", | ||||
|                     pack_count: pack_count | ||||
|                   ) | ||||
|                 } | ||||
|               > | ||||
| @@ -216,11 +216,11 @@ | ||||
|       <% else %> | ||||
|         <div class="flex flex-wrap justify-center items-stretch"> | ||||
|           <.ammo_group_card | ||||
|             :for={%{id: ammo_group_id, container_id: container_id} = ammo_group <- @ammo_groups} | ||||
|             ammo_group={ammo_group} | ||||
|             original_count={@original_counts && Map.fetch!(@original_counts, ammo_group_id)} | ||||
|             cpr={Map.get(@cprs, ammo_group_id)} | ||||
|             last_used_date={Map.get(@last_used_dates, ammo_group_id)} | ||||
|             :for={%{id: pack_id, container_id: container_id} = pack <- @packs} | ||||
|             pack={pack} | ||||
|             original_count={@original_counts && Map.fetch!(@original_counts, pack_id)} | ||||
|             cpr={Map.get(@cprs, pack_id)} | ||||
|             last_used_date={Map.get(@last_used_dates, pack_id)} | ||||
|             current_user={@current_user} | ||||
|             container={Map.fetch!(@containers, container_id)} | ||||
|           /> | ||||
|   | ||||
| @@ -79,15 +79,15 @@ defmodule CanneryWeb.ContainerLive.Index do | ||||
|               prompt = dgettext("prompts", "%{name} has been deleted", name: container_name) | ||||
|               socket |> put_flash(:info, prompt) |> display_containers() | ||||
|  | ||||
|             {:error, %{action: :delete, errors: [ammo_groups: _error], valid?: false} = changeset} -> | ||||
|               ammo_groups_error = changeset |> changeset_errors(:ammo_groups) |> Enum.join(", ") | ||||
|             {:error, %{action: :delete, errors: [packs: _error], valid?: false} = changeset} -> | ||||
|               packs_error = changeset |> changeset_errors(:packs) |> Enum.join(", ") | ||||
|  | ||||
|               prompt = | ||||
|                 dgettext( | ||||
|                   "errors", | ||||
|                   "Could not delete %{name}: %{error}", | ||||
|                   name: changeset |> Changeset.get_field(:name, "container"), | ||||
|                   error: ammo_groups_error | ||||
|                   error: packs_error | ||||
|                 ) | ||||
|  | ||||
|               socket |> put_flash(:error, prompt) | ||||
|   | ||||
| @@ -64,13 +64,13 @@ defmodule CanneryWeb.ContainerLive.Show do | ||||
|           |> put_flash(:info, prompt) | ||||
|           |> push_navigate(to: Routes.container_index_path(socket, :index)) | ||||
|  | ||||
|         {:error, %{action: :delete, errors: [ammo_groups: _error], valid?: false} = changeset} -> | ||||
|           ammo_groups_error = changeset |> changeset_errors(:ammo_groups) |> Enum.join(", ") | ||||
|         {:error, %{action: :delete, errors: [packs: _error], valid?: false} = changeset} -> | ||||
|           packs_error = changeset |> changeset_errors(:packs) |> Enum.join(", ") | ||||
|  | ||||
|           prompt = | ||||
|             dgettext("errors", "Could not delete %{name}: %{error}", | ||||
|               name: changeset |> Changeset.get_field(:name, "container"), | ||||
|               error: ammo_groups_error | ||||
|               error: packs_error | ||||
|             ) | ||||
|  | ||||
|           socket |> put_flash(:error, prompt) | ||||
| @@ -109,10 +109,10 @@ defmodule CanneryWeb.ContainerLive.Show do | ||||
|          current_user | ||||
|        ) do | ||||
|     %{name: container_name} = container = Containers.get_container!(id, current_user) | ||||
|     ammo_groups = Ammo.list_ammo_groups_for_container(container, class, current_user) | ||||
|     original_counts = ammo_groups |> Ammo.get_original_counts(current_user) | ||||
|     cprs = ammo_groups |> Ammo.get_cprs(current_user) | ||||
|     last_used_dates = ammo_groups |> ActivityLog.get_last_used_dates(current_user) | ||||
|     packs = Ammo.list_packs_for_container(container, class, current_user) | ||||
|     original_counts = packs |> Ammo.get_original_counts(current_user) | ||||
|     cprs = packs |> Ammo.get_cprs(current_user) | ||||
|     last_used_dates = packs |> ActivityLog.get_last_used_dates(current_user) | ||||
|  | ||||
|     page_title = | ||||
|       case live_action do | ||||
| @@ -125,8 +125,8 @@ defmodule CanneryWeb.ContainerLive.Show do | ||||
|     |> assign( | ||||
|       container: container, | ||||
|       round_count: Ammo.get_round_count_for_container!(container, current_user), | ||||
|       ammo_groups_count: Ammo.get_ammo_groups_count_for_container!(container, current_user), | ||||
|       ammo_groups: ammo_groups, | ||||
|       packs_count: Ammo.get_packs_count_for_container!(container, current_user), | ||||
|       packs: packs, | ||||
|       original_counts: original_counts, | ||||
|       cprs: cprs, | ||||
|       last_used_dates: last_used_dates, | ||||
|   | ||||
| @@ -20,7 +20,7 @@ | ||||
|  | ||||
|   <span class="rounded-lg title text-lg"> | ||||
|     <%= gettext("Packs:") %> | ||||
|     <%= @ammo_groups_count %> | ||||
|     <%= @packs_count %> | ||||
|   </span> | ||||
|  | ||||
|   <span class="rounded-lg title text-lg"> | ||||
| @@ -118,16 +118,16 @@ | ||||
|   </div> | ||||
|  | ||||
|   <div class="w-full p-4"> | ||||
|     <%= if @ammo_groups |> Enum.empty?() do %> | ||||
|     <%= if @packs |> Enum.empty?() do %> | ||||
|       <h2 class="mx-4 title text-lg text-primary-600 text-center"> | ||||
|         <%= gettext("No ammo in this container") %> | ||||
|       </h2> | ||||
|     <% else %> | ||||
|       <%= if @view_table do %> | ||||
|         <.live_component | ||||
|           module={CanneryWeb.Components.AmmoGroupTableComponent} | ||||
|           module={CanneryWeb.Components.PackTableComponent} | ||||
|           id="ammo-type-show-table" | ||||
|           ammo_groups={@ammo_groups} | ||||
|           packs={@packs} | ||||
|           current_user={@current_user} | ||||
|           show_used={false} | ||||
|         > | ||||
| @@ -140,11 +140,11 @@ | ||||
|       <% else %> | ||||
|         <div class="flex flex-wrap justify-center items-stretch"> | ||||
|           <.ammo_group_card | ||||
|             :for={%{id: ammo_group_id} = ammo_group <- @ammo_groups} | ||||
|             ammo_group={ammo_group} | ||||
|             original_count={Map.fetch!(@original_counts, ammo_group_id)} | ||||
|             cpr={Map.get(@cprs, ammo_group_id)} | ||||
|             last_used_date={Map.get(@last_used_dates, ammo_group_id)} | ||||
|             :for={%{id: pack_id} = pack <- @packs} | ||||
|             pack={pack} | ||||
|             original_count={Map.fetch!(@original_counts, pack_id)} | ||||
|             cpr={Map.get(@cprs, pack_id)} | ||||
|             last_used_date={Map.get(@last_used_dates, pack_id)} | ||||
|             current_user={@current_user} | ||||
|           /> | ||||
|         </div> | ||||
|   | ||||
| @@ -4,33 +4,33 @@ defmodule CanneryWeb.RangeLive.FormComponent do | ||||
|   """ | ||||
|  | ||||
|   use CanneryWeb, :live_component | ||||
|   alias Cannery.{Accounts.User, ActivityLog, ActivityLog.ShotGroup, Ammo, Ammo.AmmoGroup} | ||||
|   alias Cannery.{Accounts.User, ActivityLog, ActivityLog.ShotGroup, Ammo, Ammo.Pack} | ||||
|   alias Ecto.Changeset | ||||
|   alias Phoenix.LiveView.Socket | ||||
|  | ||||
|   @impl true | ||||
|   def mount(socket), do: {:ok, socket |> assign(:ammo_group, nil)} | ||||
|   def mount(socket), do: {:ok, socket |> assign(:pack, nil)} | ||||
|  | ||||
|   @impl true | ||||
|   @spec update( | ||||
|           %{ | ||||
|             required(:shot_group) => ShotGroup.t(), | ||||
|             required(:current_user) => User.t(), | ||||
|             optional(:ammo_group) => AmmoGroup.t(), | ||||
|             optional(:pack) => Pack.t(), | ||||
|             optional(any()) => any() | ||||
|           }, | ||||
|           Socket.t() | ||||
|         ) :: {:ok, Socket.t()} | ||||
|   def update( | ||||
|         %{ | ||||
|           shot_group: %ShotGroup{ammo_group_id: ammo_group_id}, | ||||
|           shot_group: %ShotGroup{pack_id: pack_id}, | ||||
|           current_user: current_user | ||||
|         } = assigns, | ||||
|         socket | ||||
|       ) | ||||
|       when is_binary(ammo_group_id) do | ||||
|     ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user) | ||||
|     {:ok, socket |> assign(assigns) |> assign(:ammo_group, ammo_group) |> assign_changeset(%{})} | ||||
|       when is_binary(pack_id) do | ||||
|     pack = Ammo.get_pack!(pack_id, current_user) | ||||
|     {:ok, socket |> assign(assigns) |> assign(:pack, pack) |> assign_changeset(%{})} | ||||
|   end | ||||
|  | ||||
|   def update(%{shot_group: %ShotGroup{}} = assigns, socket) do | ||||
| @@ -66,7 +66,7 @@ defmodule CanneryWeb.RangeLive.FormComponent do | ||||
|            assigns: %{ | ||||
|              action: live_action, | ||||
|              current_user: user, | ||||
|              ammo_group: ammo_group, | ||||
|              pack: pack, | ||||
|              shot_group: shot_group | ||||
|            } | ||||
|          } = socket, | ||||
| @@ -81,7 +81,7 @@ defmodule CanneryWeb.RangeLive.FormComponent do | ||||
|  | ||||
|     changeset = | ||||
|       case default_action do | ||||
|         :insert -> shot_group |> ShotGroup.create_changeset(user, ammo_group, shot_group_params) | ||||
|         :insert -> shot_group |> ShotGroup.create_changeset(user, pack, shot_group_params) | ||||
|         :update -> shot_group |> ShotGroup.update_changeset(user, shot_group_params) | ||||
|       end | ||||
|  | ||||
|   | ||||
| @@ -22,7 +22,7 @@ | ||||
|     <%= label(f, :count, gettext("Shots fired"), class: "title text-lg text-primary-600") %> | ||||
|     <%= number_input(f, :count, | ||||
|       min: 1, | ||||
|       max: @shot_group.count + @ammo_group.count, | ||||
|       max: @shot_group.count + @pack.count, | ||||
|       class: "input input-primary col-span-2" | ||||
|     ) %> | ||||
|     <%= error_tag(f, :count, "col-span-3") %> | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| defmodule CanneryWeb.RangeLive.Index do | ||||
|   @moduledoc """ | ||||
|   Main page for range day mode, where `AmmoGroup`s can be used up. | ||||
|   Main page for range day mode, where `Pack`s can be used up. | ||||
|   """ | ||||
|  | ||||
|   use CanneryWeb, :live_view | ||||
| @@ -30,7 +30,7 @@ defmodule CanneryWeb.RangeLive.Index do | ||||
|     socket | ||||
|     |> assign( | ||||
|       page_title: gettext("Record Shots"), | ||||
|       ammo_group: Ammo.get_ammo_group!(id, current_user) | ||||
|       pack: Ammo.get_pack!(id, current_user) | ||||
|     ) | ||||
|   end | ||||
|  | ||||
| @@ -82,13 +82,12 @@ defmodule CanneryWeb.RangeLive.Index do | ||||
|  | ||||
|   def handle_event( | ||||
|         "toggle_staged", | ||||
|         %{"ammo_group_id" => ammo_group_id}, | ||||
|         %{"pack_id" => pack_id}, | ||||
|         %{assigns: %{current_user: current_user}} = socket | ||||
|       ) do | ||||
|     ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user) | ||||
|     pack = Ammo.get_pack!(pack_id, current_user) | ||||
|  | ||||
|     {:ok, _ammo_group} = | ||||
|       ammo_group |> Ammo.update_ammo_group(%{"staged" => !ammo_group.staged}, current_user) | ||||
|     {:ok, _pack} = pack |> Ammo.update_pack(%{"staged" => !pack.staged}, current_user) | ||||
|  | ||||
|     prompt = dgettext("prompts", "Ammo unstaged succesfully") | ||||
|     {:noreply, socket |> put_flash(:info, prompt) |> display_shot_groups()} | ||||
| @@ -123,15 +122,15 @@ defmodule CanneryWeb.RangeLive.Index do | ||||
|          %{assigns: %{class: class, search: search, current_user: current_user}} = socket | ||||
|        ) do | ||||
|     shot_groups = ActivityLog.list_shot_groups(search, class, current_user) | ||||
|     ammo_groups = Ammo.list_staged_ammo_groups(current_user) | ||||
|     packs = Ammo.list_staged_packs(current_user) | ||||
|     chart_data = shot_groups |> get_chart_data_for_shot_group() | ||||
|     original_counts = ammo_groups |> Ammo.get_original_counts(current_user) | ||||
|     cprs = ammo_groups |> Ammo.get_cprs(current_user) | ||||
|     last_used_dates = ammo_groups |> ActivityLog.get_last_used_dates(current_user) | ||||
|     original_counts = packs |> Ammo.get_original_counts(current_user) | ||||
|     cprs = packs |> Ammo.get_cprs(current_user) | ||||
|     last_used_dates = packs |> ActivityLog.get_last_used_dates(current_user) | ||||
|  | ||||
|     socket | ||||
|     |> assign( | ||||
|       ammo_groups: ammo_groups, | ||||
|       packs: packs, | ||||
|       original_counts: original_counts, | ||||
|       cprs: cprs, | ||||
|       last_used_dates: last_used_dates, | ||||
|   | ||||
| @@ -3,43 +3,43 @@ | ||||
|     <%= gettext("Range day") %> | ||||
|   </h1> | ||||
|  | ||||
|   <%= if @ammo_groups |> Enum.empty?() do %> | ||||
|   <%= if @packs |> Enum.empty?() do %> | ||||
|     <h1 class="title text-xl text-primary-600"> | ||||
|       <%= gettext("No ammo staged") %> | ||||
|       <%= display_emoji("😔") %> | ||||
|     </h1> | ||||
|  | ||||
|     <.link navigate={Routes.ammo_group_index_path(Endpoint, :index)} class="btn btn-primary"> | ||||
|     <.link navigate={Routes.pack_index_path(Endpoint, :index)} class="btn btn-primary"> | ||||
|       <%= dgettext("actions", "Why not get some ready to shoot?") %> | ||||
|     </.link> | ||||
|   <% else %> | ||||
|     <.link navigate={Routes.ammo_group_index_path(Endpoint, :index)} class="btn btn-primary"> | ||||
|     <.link navigate={Routes.pack_index_path(Endpoint, :index)} class="btn btn-primary"> | ||||
|       <%= dgettext("actions", "Stage ammo") %> | ||||
|     </.link> | ||||
|  | ||||
|     <div class="w-full flex flex-row flex-wrap justify-center items-stretch"> | ||||
|       <.ammo_group_card | ||||
|         :for={%{id: ammo_group_id} = ammo_group <- @ammo_groups} | ||||
|         ammo_group={ammo_group} | ||||
|         original_count={Map.fetch!(@original_counts, ammo_group_id)} | ||||
|         cpr={Map.get(@cprs, ammo_group_id)} | ||||
|         last_used_date={Map.get(@last_used_dates, ammo_group_id)} | ||||
|         :for={%{id: pack_id} = pack <- @packs} | ||||
|         pack={pack} | ||||
|         original_count={Map.fetch!(@original_counts, pack_id)} | ||||
|         cpr={Map.get(@cprs, pack_id)} | ||||
|         last_used_date={Map.get(@last_used_dates, pack_id)} | ||||
|         current_user={@current_user} | ||||
|       > | ||||
|         <button | ||||
|           type="button" | ||||
|           class="btn btn-primary" | ||||
|           phx-click="toggle_staged" | ||||
|           phx-value-ammo_group_id={ammo_group.id} | ||||
|           phx-value-pack_id={pack.id} | ||||
|           data-confirm={"#{dgettext("prompts", "Are you sure you want to unstage this ammo?")}"} | ||||
|         > | ||||
|           <%= if ammo_group.staged, | ||||
|           <%= if pack.staged, | ||||
|             do: dgettext("actions", "Unstage from range"), | ||||
|             else: dgettext("actions", "Stage for range") %> | ||||
|         </button> | ||||
|  | ||||
|         <.link | ||||
|           patch={Routes.range_index_path(Endpoint, :add_shot_group, ammo_group)} | ||||
|           patch={Routes.range_index_path(Endpoint, :add_shot_group, pack)} | ||||
|           class="btn btn-primary" | ||||
|         > | ||||
|           <%= dgettext("actions", "Record shots") %> | ||||
| @@ -186,7 +186,7 @@ | ||||
|         id={:new} | ||||
|         title={@page_title} | ||||
|         action={@live_action} | ||||
|         ammo_group={@ammo_group} | ||||
|         pack={@pack} | ||||
|         return_to={Routes.range_index_path(Endpoint, :index)} | ||||
|         current_user={@current_user} | ||||
|       /> | ||||
|   | ||||
| @@ -89,19 +89,19 @@ defmodule CanneryWeb.Router do | ||||
|     live "/container/edit/:id", ContainerLive.Show, :edit | ||||
|     live "/container/edit_tags/:id", ContainerLive.Show, :edit_tags | ||||
|  | ||||
|     live "/ammo", AmmoGroupLive.Index, :index | ||||
|     live "/ammo/new", AmmoGroupLive.Index, :new | ||||
|     live "/ammo/edit/:id", AmmoGroupLive.Index, :edit | ||||
|     live "/ammo/clone/:id", AmmoGroupLive.Index, :clone | ||||
|     live "/ammo/add_shot_group/:id", AmmoGroupLive.Index, :add_shot_group | ||||
|     live "/ammo/move/:id", AmmoGroupLive.Index, :move | ||||
|     live "/ammo/search/:search", AmmoGroupLive.Index, :search | ||||
|     live "/ammo", PackLive.Index, :index | ||||
|     live "/ammo/new", PackLive.Index, :new | ||||
|     live "/ammo/edit/:id", PackLive.Index, :edit | ||||
|     live "/ammo/clone/:id", PackLive.Index, :clone | ||||
|     live "/ammo/add_shot_group/:id", PackLive.Index, :add_shot_group | ||||
|     live "/ammo/move/:id", PackLive.Index, :move | ||||
|     live "/ammo/search/:search", PackLive.Index, :search | ||||
|  | ||||
|     live "/ammo/show/:id", AmmoGroupLive.Show, :show | ||||
|     live "/ammo/show/edit/:id", AmmoGroupLive.Show, :edit | ||||
|     live "/ammo/show/add_shot_group/:id", AmmoGroupLive.Show, :add_shot_group | ||||
|     live "/ammo/show/move/:id", AmmoGroupLive.Show, :move | ||||
|     live "/ammo/show/:id/edit/:shot_group_id", AmmoGroupLive.Show, :edit_shot_group | ||||
|     live "/ammo/show/:id", PackLive.Show, :show | ||||
|     live "/ammo/show/edit/:id", PackLive.Show, :edit | ||||
|     live "/ammo/show/add_shot_group/:id", PackLive.Show, :add_shot_group | ||||
|     live "/ammo/show/move/:id", PackLive.Show, :move | ||||
|     live "/ammo/show/:id/edit/:shot_group_id", PackLive.Show, :edit_shot_group | ||||
|  | ||||
|     live "/range", RangeLive.Index, :index | ||||
|     live "/range/edit/:id", RangeLive.Index, :edit | ||||
|   | ||||
		Reference in New Issue
	
	Block a user