From 7110a2db21192611b43ca17c01f897f230ac4c3b Mon Sep 17 00:00:00 2001 From: shibao Date: Tue, 15 Feb 2022 19:55:48 -0500 Subject: [PATCH] order in db --- lib/cannery/accounts.ex | 4 ++-- lib/cannery/ammo.ex | 19 ++++++++++++++----- lib/cannery/containers.ex | 2 +- lib/cannery/invites.ex | 2 +- lib/cannery/tags.ex | 3 ++- lib/cannery_web/live/invite_live/index.ex | 5 ++--- priv/gettext/errors.pot | 2 +- 7 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/cannery/accounts.ex b/lib/cannery/accounts.ex index 29bf4973..d25d53f2 100644 --- a/lib/cannery/accounts.ex +++ b/lib/cannery/accounts.ex @@ -74,7 +74,7 @@ defmodule Cannery.Accounts do """ @spec list_all_users_by_role(User.t()) :: %{String.t() => [User.t()]} def list_all_users_by_role(%User{role: :admin}) do - Repo.all(User) |> Enum.group_by(fn user -> user.role end) + Repo.all(from u in User, order_by: u.email) |> Enum.group_by(fn user -> user.role end) end @doc """ @@ -89,7 +89,7 @@ defmodule Cannery.Accounts do @spec list_users_by_role(:admin | :user) :: [User.t()] def list_users_by_role(role) do role = role |> to_string() - Repo.all(from u in User, where: u.role == ^role) + Repo.all(from u in User, where: u.role == ^role, order_by: u.email) end ## User registration diff --git a/lib/cannery/ammo.ex b/lib/cannery/ammo.ex index f64b76b7..26b03508 100644 --- a/lib/cannery/ammo.ex +++ b/lib/cannery/ammo.ex @@ -19,7 +19,7 @@ defmodule Cannery.Ammo do """ @spec list_ammo_types(User.t()) :: [AmmoType.t()] def list_ammo_types(%User{id: user_id}), - do: Repo.all(from at in AmmoType, where: at.user_id == ^user_id) + do: Repo.all(from at in AmmoType, where: at.user_id == ^user_id, order_by: at.name) @doc """ Gets a single ammo_type. @@ -163,7 +163,8 @@ defmodule Cannery.Ammo do Repo.all( from am in AmmoGroup, where: am.ammo_type_id == ^ammo_type_id, - where: am.user_id == ^user_id + where: am.user_id == ^user_id, + order_by: am.id ) end @@ -180,9 +181,12 @@ defmodule Cannery.Ammo do @spec list_ammo_groups(User.t(), include_empty :: boolean()) :: [AmmoGroup.t()] def list_ammo_groups(%User{id: user_id}, include_empty \\ false) do if include_empty do - from am in AmmoGroup, where: am.user_id == ^user_id + from am in AmmoGroup, where: am.user_id == ^user_id, order_by: am.id else - from am in AmmoGroup, where: am.user_id == ^user_id, where: not (am.count == 0) + from am in AmmoGroup, + where: am.user_id == ^user_id, + where: not (am.count == 0), + order_by: am.id end |> Repo.all() end @@ -198,7 +202,12 @@ defmodule Cannery.Ammo do """ @spec list_staged_ammo_groups(User.t()) :: [AmmoGroup.t()] def list_staged_ammo_groups(%User{id: user_id}) do - Repo.all(from am in AmmoGroup, where: am.user_id == ^user_id, where: am.staged == true) + Repo.all( + from am in AmmoGroup, + where: am.user_id == ^user_id, + where: am.staged == true, + order_by: am.id + ) end @doc """ diff --git a/lib/cannery/containers.ex b/lib/cannery/containers.ex index 08827e31..2d2be35f 100644 --- a/lib/cannery/containers.ex +++ b/lib/cannery/containers.ex @@ -20,7 +20,7 @@ defmodule Cannery.Containers do """ @spec list_containers(User.t()) :: [Container.t()] def list_containers(%User{id: user_id}), - do: Repo.all(from c in Container, where: c.user_id == ^user_id) + do: Repo.all(from c in Container, where: c.user_id == ^user_id, order_by: c.name) @doc """ Gets a single container. diff --git a/lib/cannery/invites.ex b/lib/cannery/invites.ex index e535c4dd..f8788ad1 100644 --- a/lib/cannery/invites.ex +++ b/lib/cannery/invites.ex @@ -20,7 +20,7 @@ defmodule Cannery.Invites do """ @spec list_invites(User.t()) :: [Invite.t()] def list_invites(%User{role: :admin}) do - Repo.all(Invite) + Repo.all(from i in Invite, order_by: i.name) end @doc """ diff --git a/lib/cannery/tags.ex b/lib/cannery/tags.ex index 4b6a9c0d..8b3e9605 100644 --- a/lib/cannery/tags.ex +++ b/lib/cannery/tags.ex @@ -18,7 +18,8 @@ defmodule Cannery.Tags do """ @spec list_tags(User.t()) :: [Tag.t()] - def list_tags(%{id: user_id}), do: Repo.all(from t in Tag, where: t.user_id == ^user_id) + def list_tags(%{id: user_id}), + do: Repo.all(from t in Tag, where: t.user_id == ^user_id, order_by: t.name) @doc """ Gets a single tag. diff --git a/lib/cannery_web/live/invite_live/index.ex b/lib/cannery_web/live/invite_live/index.ex index d48f7fb8..3f4e0add 100644 --- a/lib/cannery_web/live/invite_live/index.ex +++ b/lib/cannery_web/live/invite_live/index.ex @@ -138,16 +138,15 @@ defmodule CanneryWeb.InviteLive.Index do end defp display_invites(%{assigns: %{current_user: current_user}} = socket) do - invites = Invites.list_invites(current_user) |> Enum.sort_by(fn %{name: name} -> name end) + invites = Invites.list_invites(current_user) all_users = Accounts.list_all_users_by_role(current_user) admins = all_users |> Map.get(:admin, []) |> Enum.reject(fn %{id: user_id} -> user_id == current_user.id end) - |> Enum.sort_by(fn %{email: email} -> email end) - users = all_users |> Map.get(:user, []) |> Enum.sort_by(fn %{email: email} -> email end) + users = all_users |> Map.get(:user, []) socket |> assign(invites: invites, admins: admins, users: users) end end diff --git a/priv/gettext/errors.pot b/priv/gettext/errors.pot index 36979560..73a52292 100644 --- a/priv/gettext/errors.pot +++ b/priv/gettext/errors.pot @@ -132,7 +132,7 @@ msgid "must have the @ sign and no spaces" msgstr "" #, elixir-format, ex-autogen -#: lib/cannery/tags.ex:39 +#: lib/cannery/tags.ex:40 msgid "Tag not found" msgstr ""