diff --git a/CHANGELOG.md b/CHANGELOG.md index 9edf9edb..e3521782 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Fix ammo type firing type display - Show original count, current value, and percentage remaining for ammo groups - Show shot history for an ammo group +- Show ammo round totals and total shot for ammo types # 0.1.0 - Initial release! diff --git a/lib/cannery/ammo.ex b/lib/cannery/ammo.ex index 6800c59a..67c145bb 100644 --- a/lib/cannery/ammo.ex +++ b/lib/cannery/ammo.ex @@ -67,6 +67,59 @@ defmodule Cannery.Ammo do ) end + @doc """ + Gets the total number of rounds for an ammo type + + Raises `Ecto.NoResultsError` if the Ammo type does not exist. + + ## Examples + + iex> get_round_count_for_ammo_type(123, %User{id: 123}) + %AmmoType{} + + iex> get_round_count_for_ammo_type(456, %User{id: 123}) + ** (Ecto.NoResultsError) + + """ + @spec get_round_count_for_ammo_type(AmmoType.t(), User.t()) :: non_neg_integer() + def get_round_count_for_ammo_type( + %AmmoType{id: ammo_type_id, user_id: user_id}, + %User{id: user_id} + ) do + Repo.one!( + from ag in AmmoGroup, + where: ag.ammo_type_id == ^ammo_type_id, + select: sum(ag.count) + ) + end + + @doc """ + Gets the total number of rounds shot for an ammo type + + Raises `Ecto.NoResultsError` if the Ammo type does not exist. + + ## Examples + + iex> get_used_count_for_ammo_type(123, %User{id: 123}) + %AmmoType{} + + iex> get_used_count_for_ammo_type(456, %User{id: 123}) + ** (Ecto.NoResultsError) + + """ + @spec get_used_count_for_ammo_type(AmmoType.t(), User.t()) :: non_neg_integer() + def get_used_count_for_ammo_type( + %AmmoType{id: ammo_type_id, user_id: user_id}, + %User{id: user_id} + ) do + Repo.one!( + from ag in AmmoGroup, + left_join: sg in assoc(ag, :shot_groups), + where: ag.ammo_type_id == ^ammo_type_id, + select: sum(sg.count) + ) + end + @doc """ Creates a ammo_type. diff --git a/lib/cannery_web/live/ammo_type_live/index.html.heex b/lib/cannery_web/live/ammo_type_live/index.html.heex index bd748772..b3fd4ee3 100644 --- a/lib/cannery_web/live/ammo_type_live/index.html.heex +++ b/lib/cannery_web/live/ammo_type_live/index.html.heex @@ -28,6 +28,9 @@ <%= field_name %> <% end %> +