From 8e99a57994fff5637bc59b7d7b4e0c2f170a96ef Mon Sep 17 00:00:00 2001 From: shibao Date: Sat, 3 Jun 2023 20:12:06 -0400 Subject: [PATCH] improve Containers.list_containers --- lib/cannery/containers.ex | 20 ++++++++++++-------- lib/cannery_web/live/container_live/index.ex | 2 +- priv/gettext/de/LC_MESSAGES/errors.po | 2 +- priv/gettext/en/LC_MESSAGES/errors.po | 2 +- priv/gettext/errors.pot | 2 +- priv/gettext/es/LC_MESSAGES/errors.po | 2 +- priv/gettext/fr/LC_MESSAGES/errors.po | 2 +- priv/gettext/ga/LC_MESSAGES/errors.po | 2 +- test/cannery/containers_test.exs | 14 +++++++------- 9 files changed, 26 insertions(+), 22 deletions(-) diff --git a/lib/cannery/containers.ex b/lib/cannery/containers.ex index 4bf0ded1..c0a24a24 100644 --- a/lib/cannery/containers.ex +++ b/lib/cannery/containers.ex @@ -11,6 +11,9 @@ defmodule Cannery.Containers do @container_preloads [:tags] + @type list_containers_option :: {:search, String.t() | nil} + @type list_containers_options :: [list_containers_option()] + @doc """ Returns the list of containers. @@ -19,30 +22,31 @@ defmodule Cannery.Containers do iex> list_containers(%User{id: 123}) [%Container{}, ...] - iex> list_containers("cool", %User{id: 123}) + iex> list_containers(%User{id: 123}, search: "cool") [%Container{name: "my cool container"}, ...] """ @spec list_containers(User.t()) :: [Container.t()] - @spec list_containers(search :: nil | String.t(), User.t()) :: [Container.t()] - def list_containers(search \\ nil, %User{id: user_id}) do + @spec list_containers(User.t(), list_containers_options()) :: [Container.t()] + def list_containers(%User{id: user_id}, opts \\ []) do from(c in Container, as: :c, left_join: t in assoc(c, :tags), + on: c.user_id == t.user_id, as: :t, where: c.user_id == ^user_id, - order_by: c.name, distinct: c.id, preload: ^@container_preloads ) - |> list_containers_search(search) + |> list_containers_search(Keyword.get(opts, :search)) |> Repo.all() end - defp list_containers_search(query, nil), do: query - defp list_containers_search(query, ""), do: query + @spec list_containers_search(Queryable.t(), search :: String.t() | nil) :: Queryable.t() + defp list_containers_search(query, search) when search in ["", nil], + do: query |> order_by([c: c], c.name) - defp list_containers_search(query, search) do + defp list_containers_search(query, search) when search |> is_binary() do trimmed_search = String.trim(search) query diff --git a/lib/cannery_web/live/container_live/index.ex b/lib/cannery_web/live/container_live/index.ex index 2d4fb32f..79fe1675 100644 --- a/lib/cannery_web/live/container_live/index.ex +++ b/lib/cannery_web/live/container_live/index.ex @@ -113,6 +113,6 @@ defmodule CanneryWeb.ContainerLive.Index do end defp display_containers(%{assigns: %{search: search, current_user: current_user}} = socket) do - socket |> assign(:containers, Containers.list_containers(search, current_user)) + socket |> assign(:containers, Containers.list_containers(current_user, search: search)) end end diff --git a/priv/gettext/de/LC_MESSAGES/errors.po b/priv/gettext/de/LC_MESSAGES/errors.po index 84db6bf3..f32ce75d 100644 --- a/priv/gettext/de/LC_MESSAGES/errors.po +++ b/priv/gettext/de/LC_MESSAGES/errors.po @@ -23,7 +23,7 @@ msgstr "" ## Run "mix gettext.extract" to bring this file up to ## date. Leave "msgstr"s empty as changing them here has no ## effect: edit them in PO (.po) files instead. -#: lib/cannery/containers.ex:220 +#: lib/cannery/containers.ex:224 #, elixir-autogen, elixir-format msgid "Container must be empty before deleting" msgstr "Behälter muss vor dem Löschen leer sein" diff --git a/priv/gettext/en/LC_MESSAGES/errors.po b/priv/gettext/en/LC_MESSAGES/errors.po index 9787db56..48f7de1b 100644 --- a/priv/gettext/en/LC_MESSAGES/errors.po +++ b/priv/gettext/en/LC_MESSAGES/errors.po @@ -10,7 +10,7 @@ msgid "" msgstr "" "Language: en\n" -#: lib/cannery/containers.ex:220 +#: lib/cannery/containers.ex:224 #, elixir-autogen, elixir-format msgid "Container must be empty before deleting" msgstr "" diff --git a/priv/gettext/errors.pot b/priv/gettext/errors.pot index e078265e..a7e4a028 100644 --- a/priv/gettext/errors.pot +++ b/priv/gettext/errors.pot @@ -10,7 +10,7 @@ msgid "" msgstr "" -#: lib/cannery/containers.ex:220 +#: lib/cannery/containers.ex:224 #, elixir-autogen, elixir-format msgid "Container must be empty before deleting" msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/errors.po b/priv/gettext/es/LC_MESSAGES/errors.po index 5d300ba7..8697f61b 100644 --- a/priv/gettext/es/LC_MESSAGES/errors.po +++ b/priv/gettext/es/LC_MESSAGES/errors.po @@ -23,7 +23,7 @@ msgstr "" ## Run "mix gettext.extract" to bring this file up to ## date. Leave "msgstr"s empty as changing them here has no ## effect: edit them in PO (.po) files instead. -#: lib/cannery/containers.ex:220 +#: lib/cannery/containers.ex:224 #, elixir-autogen, elixir-format msgid "Container must be empty before deleting" msgstr "El contenedor debe estar vacío antes de ser borrado" diff --git a/priv/gettext/fr/LC_MESSAGES/errors.po b/priv/gettext/fr/LC_MESSAGES/errors.po index 6a706cfb..644fa812 100644 --- a/priv/gettext/fr/LC_MESSAGES/errors.po +++ b/priv/gettext/fr/LC_MESSAGES/errors.po @@ -23,7 +23,7 @@ msgstr "" # # Run "mix gettext.extract" to bring this file up to # # date. Leave "msgstr"s empty as changing them here has no # # effect: edit them in PO (.po) files instead. -#: lib/cannery/containers.ex:220 +#: lib/cannery/containers.ex:224 #, elixir-autogen, elixir-format msgid "Container must be empty before deleting" msgstr "Le conteneur doit être vide pour être supprimé" diff --git a/priv/gettext/ga/LC_MESSAGES/errors.po b/priv/gettext/ga/LC_MESSAGES/errors.po index 7d41f744..a589b65a 100644 --- a/priv/gettext/ga/LC_MESSAGES/errors.po +++ b/priv/gettext/ga/LC_MESSAGES/errors.po @@ -24,7 +24,7 @@ msgstr "" ## Run "mix gettext.extract" to bring this file up to ## date. Leave "msgstr"s empty as changing them here has no ## effect: edit them in PO (.po) files instead. -#: lib/cannery/containers.ex:220 +#: lib/cannery/containers.ex:224 #, elixir-autogen, elixir-format msgid "Container must be empty before deleting" msgstr "Caithfidh an coimeádán a bheidh follamh roimh scriosadh" diff --git a/test/cannery/containers_test.exs b/test/cannery/containers_test.exs index a80ecbbb..5e883078 100644 --- a/test/cannery/containers_test.exs +++ b/test/cannery/containers_test.exs @@ -77,16 +77,16 @@ defmodule Cannery.ContainersTest do _shouldnt_return = container_fixture(%{name: "another person's container"}, user_fixture()) # attributes - assert Containers.list_containers("cool", current_user) == [container_a] - assert Containers.list_containers("fascinating", current_user) == [container_b] - assert Containers.list_containers("secret", current_user) == [container_c] - assert Containers.list_containers("box", current_user) == [container_d] + assert Containers.list_containers(current_user, search: "cool") == [container_a] + assert Containers.list_containers(current_user, search: "fascinating") == [container_b] + assert Containers.list_containers(current_user, search: "secret") == [container_c] + assert Containers.list_containers(current_user, search: "box") == [container_d] # tags - assert Containers.list_containers("stupendous", current_user) == [container_c] - assert Containers.list_containers("amazing", current_user) == [container_d] + assert Containers.list_containers(current_user, search: "stupendous") == [container_c] + assert Containers.list_containers(current_user, search: "amazing") == [container_d] - assert Containers.list_containers("asajslkdflskdf", current_user) == [] + assert Containers.list_containers(current_user, search: "asajslkdflskdf") == [] end test "get_container!/2 returns the container with given id",