From e80c2018be2390f16a00b14cf45b08c914ff5cee Mon Sep 17 00:00:00 2001 From: shibao Date: Sun, 4 Jun 2023 00:00:51 -0400 Subject: [PATCH] improve Ammo.list_packs --- lib/cannery/ammo.ex | 308 +++++++----------- .../controllers/export_controller.ex | 2 +- lib/cannery_web/live/container_live/show.ex | 6 +- lib/cannery_web/live/pack_live/index.ex | 2 +- lib/cannery_web/live/range_live/index.ex | 2 +- lib/cannery_web/live/type_live/show.ex | 4 +- priv/gettext/de/LC_MESSAGES/default.po | 4 +- priv/gettext/de/LC_MESSAGES/errors.po | 2 +- priv/gettext/default.pot | 4 +- priv/gettext/en/LC_MESSAGES/default.po | 4 +- priv/gettext/en/LC_MESSAGES/errors.po | 2 +- priv/gettext/errors.pot | 2 +- priv/gettext/es/LC_MESSAGES/default.po | 4 +- priv/gettext/es/LC_MESSAGES/errors.po | 2 +- priv/gettext/fr/LC_MESSAGES/default.po | 4 +- priv/gettext/fr/LC_MESSAGES/errors.po | 2 +- priv/gettext/ga/LC_MESSAGES/default.po | 4 +- priv/gettext/ga/LC_MESSAGES/errors.po | 2 +- test/cannery/ammo_test.exs | 85 ++--- test/cannery_web/live/pack_live_test.exs | 2 +- 20 files changed, 184 insertions(+), 263 deletions(-) diff --git a/lib/cannery/ammo.ex b/lib/cannery/ammo.ex index 078b25d..1fc901a 100644 --- a/lib/cannery/ammo.ex +++ b/lib/cannery/ammo.ex @@ -373,107 +373,144 @@ defmodule Cannery.Ammo do type 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 """ Returns the list of packs for a user and type. ## Examples - iex> list_packs_for_type( - ...> %Type{id: 123, user_id: 456}, - ...> %User{id: 456} - ...> ) + iex> list_packs(%User{id: 456}) [%Pack{}, ...] - iex> list_packs_for_type( - ...> %Type{id: 123, user_id: 456}, + iex> list_packs( ...> %User{id: 456}, - ...> true + ...> show_used: true, + ...> type_id: 123, + ...> container_id: 789, + ...> search: "something", + ...> staged: true ...> ) [%Pack{}, %Pack{}, ...] """ - @spec list_packs_for_type(Type.t(), User.t()) :: [Pack.t()] - @spec list_packs_for_type(Type.t(), User.t(), show_used :: boolean()) :: - [Pack.t()] - 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 + @spec list_packs(User.t()) :: [Pack.t()] + @spec list_packs(User.t(), list_packs_options()) :: [Pack.t()] + def list_packs(%User{id: user_id}, opts \\ []) do from(p in Pack, as: :p, join: t in assoc(p, :type), + on: p.user_id == t.user_id, 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.count > 0, + distinct: p.id, 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() end - @spec list_packs_for_container_filter_type(Queryable.t(), Type.class() | :all) :: - Queryable.t() - defp list_packs_for_container_filter_type(query, :rifle), + @spec list_packs_search(Queryable.t(), search :: String.t() | nil) :: Queryable.t() + defp list_packs_search(query, search) when search in ["", nil], do: query + + 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) - defp list_packs_for_container_filter_type(query, :pistol), + defp list_packs_class(query, :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) - 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 """ Returns a count of packs. @@ -734,131 +771,6 @@ defmodule Cannery.Ammo do |> Map.new() 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 """ Gets a single pack. diff --git a/lib/cannery_web/controllers/export_controller.ex b/lib/cannery_web/controllers/export_controller.ex index c050a20..e596a04 100644 --- a/lib/cannery_web/controllers/export_controller.ex +++ b/lib/cannery_web/controllers/export_controller.ex @@ -27,7 +27,7 @@ defmodule CanneryWeb.ExportController do }) 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) original_counts = packs |> Ammo.get_original_counts(current_user) cprs = packs |> Ammo.get_cprs(current_user) diff --git a/lib/cannery_web/live/container_live/show.ex b/lib/cannery_web/live/container_live/show.ex index c633723..1957292 100644 --- a/lib/cannery_web/live/container_live/show.ex +++ b/lib/cannery_web/live/container_live/show.ex @@ -104,8 +104,10 @@ defmodule CanneryWeb.ContainerLive.Show do id, current_user ) do - %{name: container_name} = container = Containers.get_container!(id, current_user) - packs = Ammo.list_packs_for_container(container, class, current_user) + %{id: container_id, name: container_name} = + 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) cprs = packs |> Ammo.get_cprs(current_user) last_used_dates = packs |> ActivityLog.get_last_used_dates(current_user) diff --git a/lib/cannery_web/live/pack_live/index.ex b/lib/cannery_web/live/pack_live/index.ex index efe95b2..96a8eb5 100644 --- a/lib/cannery_web/live/pack_live/index.ex +++ b/lib/cannery_web/live/pack_live/index.ex @@ -149,7 +149,7 @@ defmodule CanneryWeb.PackLive.Index do # get total number of packs to determine whether to display onboarding # prompts 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) containers_count = Containers.get_containers_count!(current_user) diff --git a/lib/cannery_web/live/range_live/index.ex b/lib/cannery_web/live/range_live/index.ex index 5f5e93f..29cbed7 100644 --- a/lib/cannery_web/live/range_live/index.ex +++ b/lib/cannery_web/live/range_live/index.ex @@ -121,7 +121,7 @@ defmodule CanneryWeb.RangeLive.Index do %{assigns: %{class: class, search: search, current_user: current_user}} = socket ) do 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() original_counts = packs |> Ammo.get_original_counts(current_user) cprs = packs |> Ammo.get_cprs(current_user) diff --git a/lib/cannery_web/live/type_live/show.ex b/lib/cannery_web/live/type_live/show.ex index 29e9817..d2b27fe 100644 --- a/lib/cannery_web/live/type_live/show.ex +++ b/lib/cannery_web/live/type_live/show.ex @@ -40,7 +40,7 @@ defmodule CanneryWeb.TypeLive.Show do defp display_type( %{assigns: %{live_action: live_action, current_user: current_user, show_used: show_used}} = socket, - %Type{name: type_name} = type + %Type{id: type_id, name: type_name} = type ) do custom_fields? = fields_to_display(type) @@ -54,7 +54,7 @@ defmodule CanneryWeb.TypeLive.Show do type |> Map.get(field) != default_value 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, diff --git a/priv/gettext/de/LC_MESSAGES/default.po b/priv/gettext/de/LC_MESSAGES/default.po index 1b8c9ad..6c62aac 100644 --- a/priv/gettext/de/LC_MESSAGES/default.po +++ b/priv/gettext/de/LC_MESSAGES/default.po @@ -574,13 +574,13 @@ msgid "Reconnecting..." msgstr "Neu verbinden..." #: 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 msgid "Edit %{name}" msgstr "%{name} bearbeiten" #: 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 msgid "Edit %{name} tags" msgstr "Editiere %{name} Tags" diff --git a/priv/gettext/de/LC_MESSAGES/errors.po b/priv/gettext/de/LC_MESSAGES/errors.po index f32ce75..0a24423 100644 --- a/priv/gettext/de/LC_MESSAGES/errors.po +++ b/priv/gettext/de/LC_MESSAGES/errors.po @@ -170,7 +170,7 @@ msgstr "" "Ungültige Nummer an Kopien. Muss zwischen 1 and %{max} liegen. War " "%{multiplier}" -#: lib/cannery/ammo.ex:1121 +#: lib/cannery/ammo.ex:1033 #, elixir-autogen, elixir-format msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index d894972..792d938 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -568,13 +568,13 @@ msgid "Reconnecting..." msgstr "" #: 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 msgid "Edit %{name}" msgstr "" #: 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 msgid "Edit %{name} tags" msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index f7363a5..1f3b9d8 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -568,13 +568,13 @@ msgid "Reconnecting..." msgstr "" #: 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 msgid "Edit %{name}" msgstr "" #: 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 msgid "Edit %{name} tags" msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/errors.po b/priv/gettext/en/LC_MESSAGES/errors.po index 48f7de1..0152515 100644 --- a/priv/gettext/en/LC_MESSAGES/errors.po +++ b/priv/gettext/en/LC_MESSAGES/errors.po @@ -153,7 +153,7 @@ msgstr "" msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgstr "" -#: lib/cannery/ammo.ex:1121 +#: lib/cannery/ammo.ex:1033 #, elixir-autogen, elixir-format msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/errors.pot b/priv/gettext/errors.pot index a7e4a02..8218e8f 100644 --- a/priv/gettext/errors.pot +++ b/priv/gettext/errors.pot @@ -152,7 +152,7 @@ msgstr "" msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgstr "" -#: lib/cannery/ammo.ex:1121 +#: lib/cannery/ammo.ex:1033 #, elixir-autogen, elixir-format msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/default.po b/priv/gettext/es/LC_MESSAGES/default.po index 45cb726..4cf7ca4 100644 --- a/priv/gettext/es/LC_MESSAGES/default.po +++ b/priv/gettext/es/LC_MESSAGES/default.po @@ -575,13 +575,13 @@ msgid "Reconnecting..." msgstr "Reconectando..." #: 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 msgid "Edit %{name}" msgstr "Editar %{name}" #: 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 msgid "Edit %{name} tags" msgstr "Editar etiquetas de %{name}" diff --git a/priv/gettext/es/LC_MESSAGES/errors.po b/priv/gettext/es/LC_MESSAGES/errors.po index 8697f61..eee6849 100644 --- a/priv/gettext/es/LC_MESSAGES/errors.po +++ b/priv/gettext/es/LC_MESSAGES/errors.po @@ -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}" 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 msgid "Invalid multiplier" msgstr "Multiplicador inválido" diff --git a/priv/gettext/fr/LC_MESSAGES/default.po b/priv/gettext/fr/LC_MESSAGES/default.po index e25dfc0..fed2f33 100644 --- a/priv/gettext/fr/LC_MESSAGES/default.po +++ b/priv/gettext/fr/LC_MESSAGES/default.po @@ -576,13 +576,13 @@ msgid "Reconnecting..." msgstr "Reconnexion en cours…" #: 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 msgid "Edit %{name}" msgstr "Éditer %{name}" #: 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 msgid "Edit %{name} tags" msgstr "Éditer les tags de %{name}" diff --git a/priv/gettext/fr/LC_MESSAGES/errors.po b/priv/gettext/fr/LC_MESSAGES/errors.po index 644fa81..4d250d2 100644 --- a/priv/gettext/fr/LC_MESSAGES/errors.po +++ b/priv/gettext/fr/LC_MESSAGES/errors.po @@ -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}" 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 msgid "Invalid multiplier" msgstr "Multiplicateur invalide" diff --git a/priv/gettext/ga/LC_MESSAGES/default.po b/priv/gettext/ga/LC_MESSAGES/default.po index cab1ab7..02a48d5 100644 --- a/priv/gettext/ga/LC_MESSAGES/default.po +++ b/priv/gettext/ga/LC_MESSAGES/default.po @@ -570,13 +570,13 @@ msgid "Reconnecting..." msgstr "" #: 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 msgid "Edit %{name}" msgstr "" #: 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 msgid "Edit %{name} tags" msgstr "" diff --git a/priv/gettext/ga/LC_MESSAGES/errors.po b/priv/gettext/ga/LC_MESSAGES/errors.po index a589b65..8c822dc 100644 --- a/priv/gettext/ga/LC_MESSAGES/errors.po +++ b/priv/gettext/ga/LC_MESSAGES/errors.po @@ -168,7 +168,7 @@ msgstr "" msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgstr "" -#: lib/cannery/ammo.ex:1121 +#: lib/cannery/ammo.ex:1033 #, elixir-autogen, elixir-format msgid "Invalid multiplier" msgstr "" diff --git a/test/cannery/ammo_test.exs b/test/cannery/ammo_test.exs index f6fdaf6..c4c11cd 100644 --- a/test/cannery/ammo_test.exs +++ b/test/cannery/ammo_test.exs @@ -716,7 +716,7 @@ defmodule Cannery.AmmoTest do assert Ammo.get_packs_count!(other_user, true) == 1 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() container = container_fixture(current_user) @@ -727,24 +727,24 @@ defmodule Cannery.AmmoTest do pistol_type = type_fixture(%{class: :pistol}, current_user) {1, [pistol_pack]} = pack_fixture(pistol_type, container, current_user) - assert [^rifle_pack] = Ammo.list_packs(nil, :rifle, current_user, false) - assert [^shotgun_pack] = Ammo.list_packs(nil, :shotgun, current_user, false) - assert [^pistol_pack] = Ammo.list_packs(nil, :pistol, current_user, false) + assert [^rifle_pack] = Ammo.list_packs(current_user, class: :rifle) + assert [^shotgun_pack] = Ammo.list_packs(current_user, class: :shotgun) + 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 rifle_pack in packs assert shotgun_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 rifle_pack in packs assert shotgun_pack in packs assert pistol_pack in packs end - test "list_packs/4 returns all relevant packs including used", %{ + test "list_packs/2 returns all relevant packs including used", %{ type: type, pack: pack, container: container, @@ -756,15 +756,15 @@ defmodule Cannery.AmmoTest do shot_record_fixture(%{count: 30}, current_user, another_pack) 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 another_pack in packs assert pack in packs end - test "list_packs/4 returns relevant packs when searched", %{ + test "list_packs/2 returns relevant packs when searched", %{ type: type, pack: pack, container: container, @@ -784,7 +784,7 @@ defmodule Cannery.AmmoTest do {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 fantastic_pack in packs assert amazing_pack in packs @@ -792,37 +792,51 @@ defmodule Cannery.AmmoTest do assert pack in packs # 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 - 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 - assert Ammo.list_packs("fantastic", :all, current_user, true) == - [fantastic_pack] + assert Ammo.list_packs(current_user, search: "fantastic") == [fantastic_pack] # search works for container tag attributes - assert Ammo.list_packs("stupendous", :all, current_user, true) == - [fantastic_pack] - - assert Ammo.list_packs("random", :all, current_user, true) == [] + assert Ammo.list_packs(current_user, search: "stupendous") == [fantastic_pack] + assert Ammo.list_packs(current_user, search: "random") == [] 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, current_user: current_user } do type = type_fixture(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) pack = Ammo.get_pack!(pack.id, current_user) - assert [] == Ammo.list_packs_for_type(type, current_user) - assert [^pack] = Ammo.list_packs_for_type(type, current_user, true) + assert [] == Ammo.list_packs(current_user, type_id: type.id) + assert [^pack] = Ammo.list_packs(current_user, type_id: type.id, show_used: true) 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() container = container_fixture(current_user) @@ -838,19 +852,22 @@ defmodule Cannery.AmmoTest do pack_fixture(shotgun_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 rifle_pack in packs assert shotgun_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 rifle_pack in packs assert shotgun_pack in packs @@ -900,16 +917,6 @@ defmodule Cannery.AmmoTest do assert %{^another_type_id => 1} = packs_count 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", %{pack: %{id: pack_id} = pack, current_user: current_user} do assert Ammo.get_pack!(pack_id, current_user) == pack diff --git a/test/cannery_web/live/pack_live_test.exs b/test/cannery_web/live/pack_live_test.exs index 7bb3a0d..7146090 100644 --- a/test/cannery_web/live/pack_live_test.exs +++ b/test/cannery_web/live/pack_live_test.exs @@ -177,7 +177,7 @@ defmodule CanneryWeb.PackLiveTest do |> follow_redirect(conn, ~p"/ammo") 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 test "does not save invalid number of new packs", %{conn: conn} do