improve Ammo.get_round_count

This commit is contained in:
2023-06-05 20:47:12 -04:00
parent 8466fcd1f9
commit 9643e9f46d
13 changed files with 56 additions and 54 deletions

View File

@ -185,25 +185,49 @@ defmodule Cannery.Ammo do
|> Map.new()
end
@type get_round_count_option :: {:type_id, Type.id() | nil} | {:container_id, Container.id()}
@type get_round_count_options :: [get_round_count_option()]
@doc """
Gets the total number of rounds for a type
## Examples
iex> get_round_count_for_type(
...> %Type{id: 123, user_id: 456},
...> %User{id: 456}
...> )
iex> get_round_count(%User{id: 456}, type_id: 123)
35
iex> get_round_count(%User{id: 456}, container_id: 123)
25
"""
@spec get_round_count_for_type(Type.t(), User.t()) :: non_neg_integer()
def get_round_count_for_type(%Type{id: type_id} = type, user) do
[type]
|> get_round_count_for_types(user)
|> Map.get(type_id, 0)
@spec get_round_count(User.t()) :: non_neg_integer()
@spec get_round_count(User.t(), get_round_count_options()) :: non_neg_integer()
def get_round_count(%User{id: user_id}, opts \\ []) do
from(p in Pack,
as: :p,
where: p.user_id == ^user_id,
select: sum(p.count),
distinct: true
)
|> get_round_count_type_id(Keyword.get(opts, :type_id))
|> get_round_count_container_id(Keyword.get(opts, :container_id))
|> Repo.one() || 0
end
@spec get_round_count_type_id(Queryable.t(), Type.id() | nil) :: Queryable.t()
defp get_round_count_type_id(query, type_id) when type_id |> is_binary() do
query |> where([p: p], p.type_id == ^type_id)
end
defp get_round_count_type_id(query, _nil), do: query
@spec get_round_count_container_id(Queryable.t(), Container.id() | nil) :: Queryable.t()
defp get_round_count_container_id(query, container_id) when container_id |> is_binary() do
query |> where([p: p], p.container_id == ^container_id)
end
defp get_round_count_container_id(query, _nil), do: query
@doc """
Gets the total number of rounds for multiple types
@ -670,25 +694,6 @@ defmodule Cannery.Ammo do
query |> where([p: p], not (p.count == 0))
end
@doc """
Returns number of rounds in a container.
## Examples
iex> get_round_count_for_container(
...> %Container{id: 123, user_id: 456},
...> %User{id: 456}
...> )
5
"""
@spec get_round_count_for_container!(Container.t(), User.t()) :: non_neg_integer()
def get_round_count_for_container!(%Container{id: container_id} = container, user) do
[container]
|> get_round_count_for_containers(user)
|> Map.get(container_id, 0)
end
@doc """
Returns number of ammo packs in multiple containers.

View File

@ -35,7 +35,7 @@
<span class="rounded-lg title text-lg">
<%= gettext("Rounds:") %>
<%= @container |> Ammo.get_round_count_for_container!(@current_user) %>
<%= Ammo.get_round_count(@current_user, container_id: @container.id) %>
</span>
<% end %>

View File

@ -59,15 +59,12 @@ defmodule CanneryWeb.ExportController do
containers =
Containers.list_containers(current_user)
|> Enum.map(fn container ->
pack_count = Ammo.get_packs_count(current_user, container_id: container.id)
round_count = container |> Ammo.get_round_count_for_container!(current_user)
container
|> Jason.encode!()
|> Jason.decode!()
|> Map.merge(%{
"pack_count" => pack_count,
"round_count" => round_count
"pack_count" => Ammo.get_packs_count(current_user, container_id: container.id),
"round_count" => Ammo.get_round_count(current_user, container_id: container.id)
})
end)

View File

@ -122,7 +122,7 @@ defmodule CanneryWeb.ContainerLive.Show do
socket
|> assign(
container: container,
round_count: container |> Ammo.get_round_count_for_container!(current_user),
round_count: Ammo.get_round_count(current_user, container_id: container.id),
packs_count: Ammo.get_packs_count(current_user, container_id: container.id),
packs: packs,
original_counts: original_counts,

View File

@ -95,7 +95,7 @@ defmodule CanneryWeb.TypeLive.Show do
cprs: packs |> Ammo.get_cprs(current_user),
last_used_dates: packs |> ActivityLog.get_last_used_dates(current_user),
avg_cost_per_round: type |> Ammo.get_average_cost_for_type(current_user),
rounds: type |> Ammo.get_round_count_for_type(current_user),
rounds: Ammo.get_round_count(current_user, type_id: type.id),
original_counts: original_counts,
used_rounds: used_rounds,
historical_round_count: historical_round_count,