improve Containers.list_containers

This commit is contained in:
shibao 2023-06-03 20:12:06 -04:00
parent 7c42dd8a3a
commit 8e99a57994
9 changed files with 26 additions and 22 deletions

View File

@ -11,6 +11,9 @@ defmodule Cannery.Containers do
@container_preloads [:tags] @container_preloads [:tags]
@type list_containers_option :: {:search, String.t() | nil}
@type list_containers_options :: [list_containers_option()]
@doc """ @doc """
Returns the list of containers. Returns the list of containers.
@ -19,30 +22,31 @@ defmodule Cannery.Containers do
iex> list_containers(%User{id: 123}) iex> list_containers(%User{id: 123})
[%Container{}, ...] [%Container{}, ...]
iex> list_containers("cool", %User{id: 123}) iex> list_containers(%User{id: 123}, search: "cool")
[%Container{name: "my cool container"}, ...] [%Container{name: "my cool container"}, ...]
""" """
@spec list_containers(User.t()) :: [Container.t()] @spec list_containers(User.t()) :: [Container.t()]
@spec list_containers(search :: nil | String.t(), User.t()) :: [Container.t()] @spec list_containers(User.t(), list_containers_options()) :: [Container.t()]
def list_containers(search \\ nil, %User{id: user_id}) do def list_containers(%User{id: user_id}, opts \\ []) do
from(c in Container, from(c in Container,
as: :c, as: :c,
left_join: t in assoc(c, :tags), left_join: t in assoc(c, :tags),
on: c.user_id == t.user_id,
as: :t, as: :t,
where: c.user_id == ^user_id, where: c.user_id == ^user_id,
order_by: c.name,
distinct: c.id, distinct: c.id,
preload: ^@container_preloads preload: ^@container_preloads
) )
|> list_containers_search(search) |> list_containers_search(Keyword.get(opts, :search))
|> Repo.all() |> Repo.all()
end end
defp list_containers_search(query, nil), do: query @spec list_containers_search(Queryable.t(), search :: String.t() | nil) :: Queryable.t()
defp list_containers_search(query, ""), do: query 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) trimmed_search = String.trim(search)
query query

View File

@ -113,6 +113,6 @@ defmodule CanneryWeb.ContainerLive.Index do
end end
defp display_containers(%{assigns: %{search: search, current_user: current_user}} = socket) do 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
end end

View File

@ -23,7 +23,7 @@ msgstr ""
## Run "mix gettext.extract" to bring this file up to ## Run "mix gettext.extract" to bring this file up to
## date. Leave "msgstr"s empty as changing them here has no ## date. Leave "msgstr"s empty as changing them here has no
## effect: edit them in PO (.po) files instead. ## effect: edit them in PO (.po) files instead.
#: lib/cannery/containers.ex:220 #: lib/cannery/containers.ex:224
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Container must be empty before deleting" msgid "Container must be empty before deleting"
msgstr "Behälter muss vor dem Löschen leer sein" msgstr "Behälter muss vor dem Löschen leer sein"

View File

@ -10,7 +10,7 @@ msgid ""
msgstr "" msgstr ""
"Language: en\n" "Language: en\n"
#: lib/cannery/containers.ex:220 #: lib/cannery/containers.ex:224
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Container must be empty before deleting" msgid "Container must be empty before deleting"
msgstr "" msgstr ""

View File

@ -10,7 +10,7 @@
msgid "" msgid ""
msgstr "" msgstr ""
#: lib/cannery/containers.ex:220 #: lib/cannery/containers.ex:224
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Container must be empty before deleting" msgid "Container must be empty before deleting"
msgstr "" msgstr ""

View File

@ -23,7 +23,7 @@ msgstr ""
## Run "mix gettext.extract" to bring this file up to ## Run "mix gettext.extract" to bring this file up to
## date. Leave "msgstr"s empty as changing them here has no ## date. Leave "msgstr"s empty as changing them here has no
## effect: edit them in PO (.po) files instead. ## effect: edit them in PO (.po) files instead.
#: lib/cannery/containers.ex:220 #: lib/cannery/containers.ex:224
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Container must be empty before deleting" msgid "Container must be empty before deleting"
msgstr "El contenedor debe estar vacío antes de ser borrado" msgstr "El contenedor debe estar vacío antes de ser borrado"

View File

@ -23,7 +23,7 @@ msgstr ""
# # Run "mix gettext.extract" to bring this file up to # # Run "mix gettext.extract" to bring this file up to
# # date. Leave "msgstr"s empty as changing them here has no # # date. Leave "msgstr"s empty as changing them here has no
# # effect: edit them in PO (.po) files instead. # # effect: edit them in PO (.po) files instead.
#: lib/cannery/containers.ex:220 #: lib/cannery/containers.ex:224
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Container must be empty before deleting" msgid "Container must be empty before deleting"
msgstr "Le conteneur doit être vide pour être supprimé" msgstr "Le conteneur doit être vide pour être supprimé"

View File

@ -24,7 +24,7 @@ msgstr ""
## Run "mix gettext.extract" to bring this file up to ## Run "mix gettext.extract" to bring this file up to
## date. Leave "msgstr"s empty as changing them here has no ## date. Leave "msgstr"s empty as changing them here has no
## effect: edit them in PO (.po) files instead. ## effect: edit them in PO (.po) files instead.
#: lib/cannery/containers.ex:220 #: lib/cannery/containers.ex:224
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Container must be empty before deleting" msgid "Container must be empty before deleting"
msgstr "Caithfidh an coimeádán a bheidh follamh roimh scriosadh" msgstr "Caithfidh an coimeádán a bheidh follamh roimh scriosadh"

View File

@ -77,16 +77,16 @@ defmodule Cannery.ContainersTest do
_shouldnt_return = container_fixture(%{name: "another person's container"}, user_fixture()) _shouldnt_return = container_fixture(%{name: "another person's container"}, user_fixture())
# attributes # attributes
assert Containers.list_containers("cool", current_user) == [container_a] assert Containers.list_containers(current_user, search: "cool") == [container_a]
assert Containers.list_containers("fascinating", current_user) == [container_b] assert Containers.list_containers(current_user, search: "fascinating") == [container_b]
assert Containers.list_containers("secret", current_user) == [container_c] assert Containers.list_containers(current_user, search: "secret") == [container_c]
assert Containers.list_containers("box", current_user) == [container_d] assert Containers.list_containers(current_user, search: "box") == [container_d]
# tags # tags
assert Containers.list_containers("stupendous", current_user) == [container_c] assert Containers.list_containers(current_user, search: "stupendous") == [container_c]
assert Containers.list_containers("amazing", current_user) == [container_d] 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 end
test "get_container!/2 returns the container with given id", test "get_container!/2 returns the container with given id",