add ammo type round totals and total rounds shot

This commit is contained in:
shibao 2022-02-19 00:18:24 -05:00
parent dba53106fb
commit a19ec682e6
4 changed files with 77 additions and 0 deletions

View File

@ -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!

View File

@ -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.

View File

@ -28,6 +28,9 @@
<%= field_name %>
</th>
<% end %>
<th class="p-2">
<%= gettext("Total # of rounds") %>
</th>
<th class="p-2"></th>
</tr>
@ -46,6 +49,10 @@
</td>
<% end %>
<td class="p-2">
<%= ammo_type |> Ammo.get_round_count_for_ammo_type(@current_user) %>
</td>
<td class="p-2">
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
<%= live_redirect to: Routes.ammo_type_show_path(Endpoint, :show, ammo_type),

View File

@ -73,6 +73,22 @@
<% end %>
<% end %>
<h3 class="title text-lg">
<%= gettext("Current # of rounds:") %>
</h3>
<span class="text-primary-600">
<%= @ammo_type |> Ammo.get_round_count_for_ammo_type(@current_user) %>
</span>
<h3 class="title text-lg">
<%= gettext("Total rounds shot:") %>
</h3>
<span class="text-primary-600">
<%= @ammo_type |> Ammo.get_used_count_for_ammo_type(@current_user) %>
</span>
<%= if @avg_cost_per_round do %>
<h3 class="title text-lg">
<%= gettext("Average Price paid") %>: