From a19ec682e6be27d27dfe0cdba93ffb90aa84a3ec Mon Sep 17 00:00:00 2001 From: shibao Date: Sat, 19 Feb 2022 00:18:24 -0500 Subject: [PATCH] add ammo type round totals and total rounds shot --- CHANGELOG.md | 1 + lib/cannery/ammo.ex | 53 +++++++++++++++++++ .../live/ammo_type_live/index.html.heex | 7 +++ .../live/ammo_type_live/show.html.heex | 16 ++++++ 4 files changed, 77 insertions(+) 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 %> + + <%= gettext("Total # of rounds") %> + @@ -46,6 +49,10 @@ <% end %> + + <%= ammo_type |> Ammo.get_round_count_for_ammo_type(@current_user) %> + +
<%= live_redirect to: Routes.ammo_type_show_path(Endpoint, :show, ammo_type), diff --git a/lib/cannery_web/live/ammo_type_live/show.html.heex b/lib/cannery_web/live/ammo_type_live/show.html.heex index 61f6e12e..1ad98f8b 100644 --- a/lib/cannery_web/live/ammo_type_live/show.html.heex +++ b/lib/cannery_web/live/ammo_type_live/show.html.heex @@ -73,6 +73,22 @@ <% end %> <% end %> +

+ <%= gettext("Current # of rounds:") %> +

+ + + <%= @ammo_type |> Ammo.get_round_count_for_ammo_type(@current_user) %> + + +

+ <%= gettext("Total rounds shot:") %> +

+ + + <%= @ammo_type |> Ammo.get_used_count_for_ammo_type(@current_user) %> + + <%= if @avg_cost_per_round do %>

<%= gettext("Average Price paid") %>: