improve Ammo.list_packs

This commit is contained in:
shibao 2023-06-04 00:00:51 -04:00
parent 71fdd42d96
commit e80c2018be
20 changed files with 184 additions and 263 deletions

View File

@ -373,107 +373,144 @@ defmodule Cannery.Ammo do
type type
end end
# Packs
@type list_packs_option ::
{:type_id, Type.id()}
| {:container_id, Container.id()}
| {:class, Type.class() | :all}
| {:show_used, boolean() | nil}
| {:search, String.t() | nil}
| {:staged, boolean() | nil}
@type list_packs_options :: [list_packs_option()]
@doc """ @doc """
Returns the list of packs for a user and type. Returns the list of packs for a user and type.
## Examples ## Examples
iex> list_packs_for_type( iex> list_packs(%User{id: 456})
...> %Type{id: 123, user_id: 456},
...> %User{id: 456}
...> )
[%Pack{}, ...] [%Pack{}, ...]
iex> list_packs_for_type( iex> list_packs(
...> %Type{id: 123, user_id: 456},
...> %User{id: 456}, ...> %User{id: 456},
...> true ...> show_used: true,
...> type_id: 123,
...> container_id: 789,
...> search: "something",
...> staged: true
...> ) ...> )
[%Pack{}, %Pack{}, ...] [%Pack{}, %Pack{}, ...]
""" """
@spec list_packs_for_type(Type.t(), User.t()) :: [Pack.t()] @spec list_packs(User.t()) :: [Pack.t()]
@spec list_packs_for_type(Type.t(), User.t(), show_used :: boolean()) :: @spec list_packs(User.t(), list_packs_options()) :: [Pack.t()]
[Pack.t()] def list_packs(%User{id: user_id}, opts \\ []) do
def list_packs_for_type(type, user, show_used \\ false)
def list_packs_for_type(
%Type{id: type_id, user_id: user_id},
%User{id: user_id},
show_used
) do
from(p in Pack,
as: :p,
where: p.type_id == ^type_id,
where: p.user_id == ^user_id,
preload: ^@pack_preloads
)
|> list_packs_for_type_show_used(show_used)
|> Repo.all()
end
@spec list_packs_for_type_show_used(Queryable.t(), show_used :: boolean()) ::
Queryable.t()
def list_packs_for_type_show_used(query, false),
do: query |> where([p: p], p.count > 0)
def list_packs_for_type_show_used(query, _true), do: query
@doc """
Returns the list of packs for a user and container.
## Examples
iex> list_packs_for_container(
...> %Container{id: 123, user_id: 456},
...> :rifle,
...> %User{id: 456}
...> )
[%Pack{}, ...]
iex> list_packs_for_container(
...> %Container{id: 123, user_id: 456},
...> :all,
...> %User{id: 456}
...> )
[%Pack{}, %Pack{}, ...]
"""
@spec list_packs_for_container(
Container.t(),
Type.t() | :all,
User.t()
) :: [Pack.t()]
def list_packs_for_container(
%Container{id: container_id, user_id: user_id},
type,
%User{id: user_id}
) do
from(p in Pack, from(p in Pack,
as: :p, as: :p,
join: t in assoc(p, :type), join: t in assoc(p, :type),
on: p.user_id == t.user_id,
as: :t, as: :t,
where: p.container_id == ^container_id, join: c in Container,
on: p.container_id == c.id,
on: p.user_id == c.user_id,
as: :c,
where: p.user_id == c.user_id,
left_join: ct in ContainerTag,
on: c.id == ct.container_id,
left_join: tag in Tag,
on: ct.tag_id == tag.id,
on: p.user_id == tag.user_id,
as: :tag,
where: p.user_id == ^user_id, where: p.user_id == ^user_id,
where: p.count > 0, distinct: p.id,
preload: ^@pack_preloads preload: ^@pack_preloads
) )
|> list_packs_for_container_filter_type(type) |> list_packs_search(Keyword.get(opts, :search))
|> list_packs_class(Keyword.get(opts, :class, :all))
|> list_packs_show_used(Keyword.get(opts, :show_used))
|> list_packs_staged(Keyword.get(opts, :staged))
|> list_packs_container_id(Keyword.get(opts, :container_id))
|> list_packs_type_id(Keyword.get(opts, :type_id))
|> Repo.all() |> Repo.all()
end end
@spec list_packs_for_container_filter_type(Queryable.t(), Type.class() | :all) :: @spec list_packs_search(Queryable.t(), search :: String.t() | nil) :: Queryable.t()
Queryable.t() defp list_packs_search(query, search) when search in ["", nil], do: query
defp list_packs_for_container_filter_type(query, :rifle),
defp list_packs_search(query, search) when search |> is_binary() do
trimmed_search = String.trim(search)
query
|> where(
[p: p, t: t, c: c, tag: tag],
fragment(
"? @@ websearch_to_tsquery('english', ?)",
p.search,
^trimmed_search
) or
fragment(
"? @@ websearch_to_tsquery('english', ?)",
t.search,
^trimmed_search
) or
fragment(
"? @@ websearch_to_tsquery('english', ?)",
c.search,
^trimmed_search
) or
fragment(
"? @@ websearch_to_tsquery('english', ?)",
tag.search,
^trimmed_search
)
)
|> order_by(
[p: p],
desc:
fragment(
"ts_rank_cd(?, websearch_to_tsquery('english', ?), 4)",
p.search,
^trimmed_search
)
)
end
@spec list_packs_class(Queryable.t(), Type.class() | :all) :: Queryable.t()
defp list_packs_class(query, :rifle),
do: query |> where([t: t], t.class == :rifle) do: query |> where([t: t], t.class == :rifle)
defp list_packs_for_container_filter_type(query, :pistol), defp list_packs_class(query, :pistol),
do: query |> where([t: t], t.class == :pistol) do: query |> where([t: t], t.class == :pistol)
defp list_packs_for_container_filter_type(query, :shotgun), defp list_packs_class(query, :shotgun),
do: query |> where([t: t], t.class == :shotgun) do: query |> where([t: t], t.class == :shotgun)
defp list_packs_for_container_filter_type(query, _all), do: query defp list_packs_class(query, _all), do: query
@spec list_packs_show_used(Queryable.t(), show_used :: boolean() | nil) :: Queryable.t()
defp list_packs_show_used(query, true), do: query
defp list_packs_show_used(query, _false),
do: query |> where([p: p], not (p.count == 0))
@spec list_packs_container_id(Queryable.t(), Container.id() | nil) :: Queryable.t()
defp list_packs_container_id(query, container_id) when container_id |> is_binary(),
do: query |> where([p: p], p.container_id == ^container_id)
defp list_packs_container_id(query, _nil), do: query
@spec list_packs_type_id(Queryable.t(), Type.id() | nil) :: Queryable.t()
defp list_packs_type_id(query, type_id) when type_id |> is_binary(),
do: query |> where([p: p], p.type_id == ^type_id)
defp list_packs_type_id(query, _nil), do: query
@spec list_packs_staged(Queryable.t(), staged :: boolean() | nil) :: Queryable.t()
defp list_packs_staged(query, staged) when staged |> is_boolean(),
do: query |> where([p: p], p.staged == ^staged)
defp list_packs_staged(query, _nil), do: query
@doc """ @doc """
Returns a count of packs. Returns a count of packs.
@ -734,131 +771,6 @@ defmodule Cannery.Ammo do
|> Map.new() |> Map.new()
end end
@doc """
Returns the list of packs.
## Examples
iex> list_packs(%User{id: 123})
[%Pack{}, ...]
iex> list_packs("cool", %User{id: 123}, true)
[%Pack{notes: "My cool pack"}, ...]
"""
@spec list_packs(search :: String.t() | nil, Type.class() | :all, User.t()) ::
[Pack.t()]
@spec list_packs(
search :: nil | String.t(),
Type.class() | :all,
User.t(),
show_used :: boolean()
) :: [Pack.t()]
def list_packs(search, class, %User{id: user_id}, show_used \\ false) do
from(p in Pack,
as: :p,
join: t in assoc(p, :type),
as: :t,
join: c in Container,
on: p.container_id == c.id,
on: p.user_id == c.user_id,
as: :c,
left_join: ct in ContainerTag,
on: c.id == ct.container_id,
left_join: tag in Tag,
on: ct.tag_id == tag.id,
on: c.user_id == tag.user_id,
as: :tag,
where: p.user_id == ^user_id,
distinct: p.id,
preload: ^@pack_preloads
)
|> list_packs_class(class)
|> list_packs_show_used(show_used)
|> list_packs_search(search)
|> Repo.all()
end
@spec list_packs_class(Queryable.t(), Type.class() | :all) :: Queryable.t()
defp list_packs_class(query, :rifle),
do: query |> where([t: t], t.class == :rifle)
defp list_packs_class(query, :pistol),
do: query |> where([t: t], t.class == :pistol)
defp list_packs_class(query, :shotgun),
do: query |> where([t: t], t.class == :shotgun)
defp list_packs_class(query, _all), 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_packs_show_used(query, _false) do
query |> where([p: p], not (p.count == 0))
end
@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_packs_search(query, search) do
trimmed_search = String.trim(search)
query
|> where(
[p: p, t: t, c: c, tag: tag],
fragment(
"? @@ websearch_to_tsquery('english', ?)",
p.search,
^trimmed_search
) or
fragment(
"? @@ websearch_to_tsquery('english', ?)",
t.search,
^trimmed_search
) or
fragment(
"? @@ websearch_to_tsquery('english', ?)",
c.search,
^trimmed_search
) or
fragment(
"? @@ websearch_to_tsquery('english', ?)",
tag.search,
^trimmed_search
)
)
|> order_by(
[p: p],
desc:
fragment(
"ts_rank_cd(?, websearch_to_tsquery('english', ?), 4)",
p.search,
^trimmed_search
)
)
end
@doc """
Returns the list of staged packs for a user.
## Examples
iex> list_staged_packs(%User{id: 123})
[%Pack{}, ...]
"""
@spec list_staged_packs(User.t()) :: [Pack.t()]
def list_staged_packs(%User{id: user_id}) do
Repo.all(
from p in Pack,
where: p.user_id == ^user_id,
where: p.staged == true,
preload: ^@pack_preloads
)
end
@doc """ @doc """
Gets a single pack. Gets a single pack.

View File

@ -27,7 +27,7 @@ defmodule CanneryWeb.ExportController do
}) })
end) end)
packs = Ammo.list_packs(nil, :all, current_user, true) packs = Ammo.list_packs(current_user, show_used: true)
used_counts = packs |> ActivityLog.get_used_counts(current_user) used_counts = packs |> ActivityLog.get_used_counts(current_user)
original_counts = packs |> Ammo.get_original_counts(current_user) original_counts = packs |> Ammo.get_original_counts(current_user)
cprs = packs |> Ammo.get_cprs(current_user) cprs = packs |> Ammo.get_cprs(current_user)

View File

@ -104,8 +104,10 @@ defmodule CanneryWeb.ContainerLive.Show do
id, id,
current_user current_user
) do ) do
%{name: container_name} = container = Containers.get_container!(id, current_user) %{id: container_id, name: container_name} =
packs = Ammo.list_packs_for_container(container, class, current_user) container = Containers.get_container!(id, current_user)
packs = Ammo.list_packs(current_user, container_id: container_id, class: class)
original_counts = packs |> Ammo.get_original_counts(current_user) original_counts = packs |> Ammo.get_original_counts(current_user)
cprs = packs |> Ammo.get_cprs(current_user) cprs = packs |> Ammo.get_cprs(current_user)
last_used_dates = packs |> ActivityLog.get_last_used_dates(current_user) last_used_dates = packs |> ActivityLog.get_last_used_dates(current_user)

View File

@ -149,7 +149,7 @@ defmodule CanneryWeb.PackLive.Index do
# get total number of packs to determine whether to display onboarding # get total number of packs to determine whether to display onboarding
# prompts # prompts
packs_count = Ammo.get_packs_count!(current_user, true) packs_count = Ammo.get_packs_count!(current_user, true)
packs = Ammo.list_packs(search, class, current_user, show_used) packs = Ammo.list_packs(current_user, search: search, class: class, show_used: show_used)
types_count = Ammo.get_types_count!(current_user) types_count = Ammo.get_types_count!(current_user)
containers_count = Containers.get_containers_count!(current_user) containers_count = Containers.get_containers_count!(current_user)

View File

@ -121,7 +121,7 @@ defmodule CanneryWeb.RangeLive.Index do
%{assigns: %{class: class, search: search, current_user: current_user}} = socket %{assigns: %{class: class, search: search, current_user: current_user}} = socket
) do ) do
shot_records = ActivityLog.list_shot_records(search, class, current_user) shot_records = ActivityLog.list_shot_records(search, class, current_user)
packs = Ammo.list_staged_packs(current_user) packs = Ammo.list_packs(current_user, staged: true)
chart_data = shot_records |> get_chart_data_for_shot_record() chart_data = shot_records |> get_chart_data_for_shot_record()
original_counts = packs |> Ammo.get_original_counts(current_user) original_counts = packs |> Ammo.get_original_counts(current_user)
cprs = packs |> Ammo.get_cprs(current_user) cprs = packs |> Ammo.get_cprs(current_user)

View File

@ -40,7 +40,7 @@ defmodule CanneryWeb.TypeLive.Show do
defp display_type( defp display_type(
%{assigns: %{live_action: live_action, current_user: current_user, show_used: show_used}} = %{assigns: %{live_action: live_action, current_user: current_user, show_used: show_used}} =
socket, socket,
%Type{name: type_name} = type %Type{id: type_id, name: type_name} = type
) do ) do
custom_fields? = custom_fields? =
fields_to_display(type) fields_to_display(type)
@ -54,7 +54,7 @@ defmodule CanneryWeb.TypeLive.Show do
type |> Map.get(field) != default_value type |> Map.get(field) != default_value
end) end)
packs = type |> Ammo.list_packs_for_type(current_user, show_used) packs = Ammo.list_packs(current_user, type_id: type_id, show_used: show_used)
[ [
original_counts, original_counts,

View File

@ -574,13 +574,13 @@ msgid "Reconnecting..."
msgstr "Neu verbinden..." msgstr "Neu verbinden..."
#: lib/cannery_web/live/container_live/index.ex:28 #: lib/cannery_web/live/container_live/index.ex:28
#: lib/cannery_web/live/container_live/show.ex:116 #: lib/cannery_web/live/container_live/show.ex:118
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Edit %{name}" msgid "Edit %{name}"
msgstr "%{name} bearbeiten" msgstr "%{name} bearbeiten"
#: lib/cannery_web/live/container_live/index.ex:63 #: lib/cannery_web/live/container_live/index.ex:63
#: lib/cannery_web/live/container_live/show.ex:117 #: lib/cannery_web/live/container_live/show.ex:119
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Edit %{name} tags" msgid "Edit %{name} tags"
msgstr "Editiere %{name} Tags" msgstr "Editiere %{name} Tags"

View File

@ -170,7 +170,7 @@ msgstr ""
"Ungültige Nummer an Kopien. Muss zwischen 1 and %{max} liegen. War " "Ungültige Nummer an Kopien. Muss zwischen 1 and %{max} liegen. War "
"%{multiplier}" "%{multiplier}"
#: lib/cannery/ammo.ex:1121 #: lib/cannery/ammo.ex:1033
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Invalid multiplier" msgid "Invalid multiplier"
msgstr "" msgstr ""

View File

@ -568,13 +568,13 @@ msgid "Reconnecting..."
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/index.ex:28 #: lib/cannery_web/live/container_live/index.ex:28
#: lib/cannery_web/live/container_live/show.ex:116 #: lib/cannery_web/live/container_live/show.ex:118
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Edit %{name}" msgid "Edit %{name}"
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/index.ex:63 #: lib/cannery_web/live/container_live/index.ex:63
#: lib/cannery_web/live/container_live/show.ex:117 #: lib/cannery_web/live/container_live/show.ex:119
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Edit %{name} tags" msgid "Edit %{name} tags"
msgstr "" msgstr ""

View File

@ -568,13 +568,13 @@ msgid "Reconnecting..."
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/index.ex:28 #: lib/cannery_web/live/container_live/index.ex:28
#: lib/cannery_web/live/container_live/show.ex:116 #: lib/cannery_web/live/container_live/show.ex:118
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Edit %{name}" msgid "Edit %{name}"
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/index.ex:63 #: lib/cannery_web/live/container_live/index.ex:63
#: lib/cannery_web/live/container_live/show.ex:117 #: lib/cannery_web/live/container_live/show.ex:119
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Edit %{name} tags" msgid "Edit %{name} tags"
msgstr "" msgstr ""

View File

@ -153,7 +153,7 @@ msgstr ""
msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}"
msgstr "" msgstr ""
#: lib/cannery/ammo.ex:1121 #: lib/cannery/ammo.ex:1033
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Invalid multiplier" msgid "Invalid multiplier"
msgstr "" msgstr ""

View File

@ -152,7 +152,7 @@ msgstr ""
msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}"
msgstr "" msgstr ""
#: lib/cannery/ammo.ex:1121 #: lib/cannery/ammo.ex:1033
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Invalid multiplier" msgid "Invalid multiplier"
msgstr "" msgstr ""

View File

@ -575,13 +575,13 @@ msgid "Reconnecting..."
msgstr "Reconectando..." msgstr "Reconectando..."
#: lib/cannery_web/live/container_live/index.ex:28 #: lib/cannery_web/live/container_live/index.ex:28
#: lib/cannery_web/live/container_live/show.ex:116 #: lib/cannery_web/live/container_live/show.ex:118
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Edit %{name}" msgid "Edit %{name}"
msgstr "Editar %{name}" msgstr "Editar %{name}"
#: lib/cannery_web/live/container_live/index.ex:63 #: lib/cannery_web/live/container_live/index.ex:63
#: lib/cannery_web/live/container_live/show.ex:117 #: lib/cannery_web/live/container_live/show.ex:119
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Edit %{name} tags" msgid "Edit %{name} tags"
msgstr "Editar etiquetas de %{name}" msgstr "Editar etiquetas de %{name}"

View File

@ -168,7 +168,7 @@ msgstr "No se ha podido procesar el número de copias"
msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}"
msgstr "Número inválido de copias, debe ser entre 1 y %{max}. Fue %{multiplier" msgstr "Número inválido de copias, debe ser entre 1 y %{max}. Fue %{multiplier"
#: lib/cannery/ammo.ex:1121 #: lib/cannery/ammo.ex:1033
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Invalid multiplier" msgid "Invalid multiplier"
msgstr "Multiplicador inválido" msgstr "Multiplicador inválido"

View File

@ -576,13 +576,13 @@ msgid "Reconnecting..."
msgstr "Reconnexion en cours…" msgstr "Reconnexion en cours…"
#: lib/cannery_web/live/container_live/index.ex:28 #: lib/cannery_web/live/container_live/index.ex:28
#: lib/cannery_web/live/container_live/show.ex:116 #: lib/cannery_web/live/container_live/show.ex:118
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Edit %{name}" msgid "Edit %{name}"
msgstr "Éditer %{name}" msgstr "Éditer %{name}"
#: lib/cannery_web/live/container_live/index.ex:63 #: lib/cannery_web/live/container_live/index.ex:63
#: lib/cannery_web/live/container_live/show.ex:117 #: lib/cannery_web/live/container_live/show.ex:119
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Edit %{name} tags" msgid "Edit %{name} tags"
msgstr "Éditer les tags de %{name}" msgstr "Éditer les tags de %{name}"

View File

@ -169,7 +169,7 @@ msgstr "Impossible d'analyser le nombre de copies"
msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}"
msgstr "Nombre de copies invalide, doit être 1 et %{max}. Été %{multiplier}" msgstr "Nombre de copies invalide, doit être 1 et %{max}. Été %{multiplier}"
#: lib/cannery/ammo.ex:1121 #: lib/cannery/ammo.ex:1033
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Invalid multiplier" msgid "Invalid multiplier"
msgstr "Multiplicateur invalide" msgstr "Multiplicateur invalide"

View File

@ -570,13 +570,13 @@ msgid "Reconnecting..."
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/index.ex:28 #: lib/cannery_web/live/container_live/index.ex:28
#: lib/cannery_web/live/container_live/show.ex:116 #: lib/cannery_web/live/container_live/show.ex:118
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Edit %{name}" msgid "Edit %{name}"
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/index.ex:63 #: lib/cannery_web/live/container_live/index.ex:63
#: lib/cannery_web/live/container_live/show.ex:117 #: lib/cannery_web/live/container_live/show.ex:119
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Edit %{name} tags" msgid "Edit %{name} tags"
msgstr "" msgstr ""

View File

@ -168,7 +168,7 @@ msgstr ""
msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}"
msgstr "" msgstr ""
#: lib/cannery/ammo.ex:1121 #: lib/cannery/ammo.ex:1033
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Invalid multiplier" msgid "Invalid multiplier"
msgstr "" msgstr ""

View File

@ -716,7 +716,7 @@ defmodule Cannery.AmmoTest do
assert Ammo.get_packs_count!(other_user, true) == 1 assert Ammo.get_packs_count!(other_user, true) == 1
end end
test "list_packs/4 returns all packs for a type" do test "list_packs/2 returns all packs for a type" do
current_user = user_fixture() current_user = user_fixture()
container = container_fixture(current_user) container = container_fixture(current_user)
@ -727,24 +727,24 @@ defmodule Cannery.AmmoTest do
pistol_type = type_fixture(%{class: :pistol}, current_user) pistol_type = type_fixture(%{class: :pistol}, current_user)
{1, [pistol_pack]} = pack_fixture(pistol_type, container, current_user) {1, [pistol_pack]} = pack_fixture(pistol_type, container, current_user)
assert [^rifle_pack] = Ammo.list_packs(nil, :rifle, current_user, false) assert [^rifle_pack] = Ammo.list_packs(current_user, class: :rifle)
assert [^shotgun_pack] = Ammo.list_packs(nil, :shotgun, current_user, false) assert [^shotgun_pack] = Ammo.list_packs(current_user, class: :shotgun)
assert [^pistol_pack] = Ammo.list_packs(nil, :pistol, current_user, false) assert [^pistol_pack] = Ammo.list_packs(current_user, class: :pistol)
packs = Ammo.list_packs(nil, :all, current_user, false) packs = Ammo.list_packs(current_user, class: :all)
assert Enum.count(packs) == 3 assert Enum.count(packs) == 3
assert rifle_pack in packs assert rifle_pack in packs
assert shotgun_pack in packs assert shotgun_pack in packs
assert pistol_pack in packs assert pistol_pack in packs
packs = Ammo.list_packs(nil, nil, current_user, false) packs = Ammo.list_packs(current_user)
assert Enum.count(packs) == 3 assert Enum.count(packs) == 3
assert rifle_pack in packs assert rifle_pack in packs
assert shotgun_pack in packs assert shotgun_pack in packs
assert pistol_pack in packs assert pistol_pack in packs
end end
test "list_packs/4 returns all relevant packs including used", %{ test "list_packs/2 returns all relevant packs including used", %{
type: type, type: type,
pack: pack, pack: pack,
container: container, container: container,
@ -756,15 +756,15 @@ defmodule Cannery.AmmoTest do
shot_record_fixture(%{count: 30}, current_user, another_pack) shot_record_fixture(%{count: 30}, current_user, another_pack)
another_pack = Ammo.get_pack!(another_pack_id, current_user) another_pack = Ammo.get_pack!(another_pack_id, current_user)
assert Ammo.list_packs(nil, :all, current_user, false) == [pack] assert Ammo.list_packs(current_user, show_used: false) == [pack]
packs = Ammo.list_packs(nil, :all, current_user, true) packs = Ammo.list_packs(current_user, show_used: true)
assert Enum.count(packs) == 2 assert Enum.count(packs) == 2
assert another_pack in packs assert another_pack in packs
assert pack in packs assert pack in packs
end end
test "list_packs/4 returns relevant packs when searched", %{ test "list_packs/2 returns relevant packs when searched", %{
type: type, type: type,
pack: pack, pack: pack,
container: container, container: container,
@ -784,7 +784,7 @@ defmodule Cannery.AmmoTest do
{1, [fantastic_pack]} = pack_fixture(%{count: 47}, type, another_container, current_user) {1, [fantastic_pack]} = pack_fixture(%{count: 47}, type, another_container, current_user)
packs = Ammo.list_packs(nil, :all, current_user, false) packs = Ammo.list_packs(current_user, search: nil)
assert Enum.count(packs) == 4 assert Enum.count(packs) == 4
assert fantastic_pack in packs assert fantastic_pack in packs
assert amazing_pack in packs assert amazing_pack in packs
@ -792,37 +792,51 @@ defmodule Cannery.AmmoTest do
assert pack in packs assert pack in packs
# search works for pack attributes # search works for pack attributes
assert Ammo.list_packs("cool", :all, current_user, true) == [another_pack] assert Ammo.list_packs(current_user, search: "cool") == [another_pack]
# search works for type attributes # search works for type attributes
assert Ammo.list_packs("amazing", :all, current_user, true) == [amazing_pack] assert Ammo.list_packs(current_user, search: "amazing") == [amazing_pack]
# search works for container attributes # search works for container attributes
assert Ammo.list_packs("fantastic", :all, current_user, true) == assert Ammo.list_packs(current_user, search: "fantastic") == [fantastic_pack]
[fantastic_pack]
# search works for container tag attributes # search works for container tag attributes
assert Ammo.list_packs("stupendous", :all, current_user, true) == assert Ammo.list_packs(current_user, search: "stupendous") == [fantastic_pack]
[fantastic_pack] assert Ammo.list_packs(current_user, search: "random") == []
assert Ammo.list_packs("random", :all, current_user, true) == []
end end
test "list_packs_for_type/3 returns all packs for a type", %{ test "list_packs/2 returns all relevant packs including staged", %{
type: type,
container: container,
pack: unstaged_pack,
current_user: current_user
} do
{1, [staged_pack]} = pack_fixture(%{staged: true}, type, container, current_user)
assert Ammo.list_packs(current_user, staged: false) == [unstaged_pack]
assert Ammo.list_packs(current_user, staged: true) == [staged_pack]
packs = Ammo.list_packs(current_user)
assert Enum.count(packs) == 2
assert unstaged_pack in packs
assert staged_pack in packs
end
test "list_packs/2 returns all relevant packs for a type", %{
container: container, container: container,
current_user: current_user current_user: current_user
} do } do
type = type_fixture(current_user) type = type_fixture(current_user)
{1, [pack]} = pack_fixture(type, container, current_user) {1, [pack]} = pack_fixture(type, container, current_user)
assert [^pack] = Ammo.list_packs_for_type(type, current_user) assert [^pack] = Ammo.list_packs(current_user, type_id: type.id)
shot_record_fixture(current_user, pack) shot_record_fixture(current_user, pack)
pack = Ammo.get_pack!(pack.id, current_user) pack = Ammo.get_pack!(pack.id, current_user)
assert [] == Ammo.list_packs_for_type(type, current_user) assert [] == Ammo.list_packs(current_user, type_id: type.id)
assert [^pack] = Ammo.list_packs_for_type(type, current_user, true) assert [^pack] = Ammo.list_packs(current_user, type_id: type.id, show_used: true)
end end
test "list_packs_for_container/3 returns all packs for a container" do test "list_packs/2 returns all relevant packs for a container" do
current_user = user_fixture() current_user = user_fixture()
container = container_fixture(current_user) container = container_fixture(current_user)
@ -838,19 +852,22 @@ defmodule Cannery.AmmoTest do
pack_fixture(shotgun_type, another_container, current_user) pack_fixture(shotgun_type, another_container, current_user)
pack_fixture(pistol_type, another_container, current_user) pack_fixture(pistol_type, another_container, current_user)
assert [^rifle_pack] = Ammo.list_packs_for_container(container, :rifle, current_user) assert [^rifle_pack] =
Ammo.list_packs(current_user, container_id: container.id, class: :rifle)
assert [^shotgun_pack] = Ammo.list_packs_for_container(container, :shotgun, current_user) assert [^shotgun_pack] =
Ammo.list_packs(current_user, container_id: container.id, class: :shotgun)
assert [^pistol_pack] = Ammo.list_packs_for_container(container, :pistol, current_user) assert [^pistol_pack] =
Ammo.list_packs(current_user, container_id: container.id, class: :pistol)
packs = Ammo.list_packs_for_container(container, :all, current_user) packs = Ammo.list_packs(current_user, container_id: container.id, class: :all)
assert Enum.count(packs) == 3 assert Enum.count(packs) == 3
assert rifle_pack in packs assert rifle_pack in packs
assert shotgun_pack in packs assert shotgun_pack in packs
assert pistol_pack in packs assert pistol_pack in packs
packs = Ammo.list_packs_for_container(container, nil, current_user) packs = Ammo.list_packs(current_user, container_id: container.id)
assert Enum.count(packs) == 3 assert Enum.count(packs) == 3
assert rifle_pack in packs assert rifle_pack in packs
assert shotgun_pack in packs assert shotgun_pack in packs
@ -900,16 +917,6 @@ defmodule Cannery.AmmoTest do
assert %{^another_type_id => 1} = packs_count assert %{^another_type_id => 1} = packs_count
end end
test "list_staged_packs/1 returns all packs that are staged", %{
type: type,
container: container,
current_user: current_user
} do
{1, [another_pack]} = pack_fixture(%{staged: true}, type, container, current_user)
assert Ammo.list_staged_packs(current_user) == [another_pack]
end
test "get_pack!/2 returns the pack with given id", test "get_pack!/2 returns the pack with given id",
%{pack: %{id: pack_id} = pack, current_user: current_user} do %{pack: %{id: pack_id} = pack, current_user: current_user} do
assert Ammo.get_pack!(pack_id, current_user) == pack assert Ammo.get_pack!(pack_id, current_user) == pack

View File

@ -177,7 +177,7 @@ defmodule CanneryWeb.PackLiveTest do
|> follow_redirect(conn, ~p"/ammo") |> follow_redirect(conn, ~p"/ammo")
assert html =~ "Ammo added successfully" assert html =~ "Ammo added successfully"
assert Ammo.list_packs(nil, :all, current_user) |> Enum.count() == multiplier + 1 assert Ammo.list_packs(current_user) |> Enum.count() == multiplier + 1
end end
test "does not save invalid number of new packs", %{conn: conn} do test "does not save invalid number of new packs", %{conn: conn} do