fix ammo type sql naming issues

This commit is contained in:
shibao 2023-05-12 22:22:46 -04:00
parent 2179bd5d86
commit 2e488fa26c

View File

@ -33,9 +33,9 @@ defmodule Cannery.Ammo do
def list_types(search \\ nil, user, type) def list_types(search \\ nil, user, type)
def list_types(search, %User{id: user_id}, type) do def list_types(search, %User{id: user_id}, type) do
from(at in Type, from(t in Type,
as: :at, as: :t,
where: at.user_id == ^user_id, where: t.user_id == ^user_id,
preload: ^@type_preloads preload: ^@type_preloads
) )
|> list_types_filter_type(type) |> list_types_filter_type(type)
@ -45,27 +45,27 @@ defmodule Cannery.Ammo do
@spec list_types_filter_search(Queryable.t(), search :: String.t() | nil) :: Queryable.t() @spec list_types_filter_search(Queryable.t(), search :: String.t() | nil) :: Queryable.t()
defp list_types_filter_search(query, search) when search in ["", nil], defp list_types_filter_search(query, search) when search in ["", nil],
do: query |> order_by([at: at], at.name) do: query |> order_by([t: t], t.name)
defp list_types_filter_search(query, search) when search |> is_binary() do defp list_types_filter_search(query, search) when search |> is_binary() do
trimmed_search = String.trim(search) trimmed_search = String.trim(search)
query query
|> where( |> where(
[at: at], [t: t],
fragment( fragment(
"? @@ websearch_to_tsquery('english', ?)", "? @@ websearch_to_tsquery('english', ?)",
at.search, t.search,
^trimmed_search ^trimmed_search
) )
) )
|> order_by( |> order_by(
[at: at], [t: t],
{ {
:desc, :desc,
fragment( fragment(
"ts_rank_cd(?, websearch_to_tsquery('english', ?), 4)", "ts_rank_cd(?, websearch_to_tsquery('english', ?), 4)",
at.search, t.search,
^trimmed_search ^trimmed_search
) )
} }
@ -74,13 +74,13 @@ defmodule Cannery.Ammo do
@spec list_types_filter_type(Queryable.t(), Type.class() | :all) :: Queryable.t() @spec list_types_filter_type(Queryable.t(), Type.class() | :all) :: Queryable.t()
defp list_types_filter_type(query, :rifle), defp list_types_filter_type(query, :rifle),
do: query |> where([at: at], at.class == :rifle) do: query |> where([t: t], t.class == :rifle)
defp list_types_filter_type(query, :pistol), defp list_types_filter_type(query, :pistol),
do: query |> where([at: at], at.class == :pistol) do: query |> where([t: t], t.class == :pistol)
defp list_types_filter_type(query, :shotgun), defp list_types_filter_type(query, :shotgun),
do: query |> where([at: at], at.class == :shotgun) do: query |> where([t: t], t.class == :shotgun)
defp list_types_filter_type(query, _all), do: query defp list_types_filter_type(query, _all), do: query
@ -96,9 +96,9 @@ defmodule Cannery.Ammo do
@spec get_types_count!(User.t()) :: integer() @spec get_types_count!(User.t()) :: integer()
def get_types_count!(%User{id: user_id}) do def get_types_count!(%User{id: user_id}) do
Repo.one( Repo.one(
from at in Type, from t in Type,
where: at.user_id == ^user_id, where: t.user_id == ^user_id,
select: count(at.id), select: count(t.id),
distinct: true distinct: true
) || 0 ) || 0
end end
@ -120,9 +120,9 @@ defmodule Cannery.Ammo do
@spec get_type!(Type.id(), User.t()) :: Type.t() @spec get_type!(Type.id(), User.t()) :: Type.t()
def get_type!(id, %User{id: user_id}) do def get_type!(id, %User{id: user_id}) do
Repo.one!( Repo.one!(
from at in Type, from t in Type,
where: at.id == ^id, where: t.id == ^id,
where: at.user_id == ^user_id, where: t.user_id == ^user_id,
preload: ^@type_preloads preload: ^@type_preloads
) )
end end
@ -451,8 +451,8 @@ defmodule Cannery.Ammo do
) do ) do
from(p in Pack, from(p in Pack,
as: :p, as: :p,
join: at in assoc(p, :type), join: t in assoc(p, :type),
as: :at, as: :t,
where: p.container_id == ^container_id, where: p.container_id == ^container_id,
where: p.user_id == ^user_id, where: p.user_id == ^user_id,
where: p.count > 0, where: p.count > 0,
@ -465,13 +465,13 @@ defmodule Cannery.Ammo do
@spec list_packs_for_container_filter_type(Queryable.t(), Type.class() | :all) :: @spec list_packs_for_container_filter_type(Queryable.t(), Type.class() | :all) ::
Queryable.t() Queryable.t()
defp list_packs_for_container_filter_type(query, :rifle), defp list_packs_for_container_filter_type(query, :rifle),
do: query |> where([at: at], at.class == :rifle) do: query |> where([t: t], t.class == :rifle)
defp list_packs_for_container_filter_type(query, :pistol), defp list_packs_for_container_filter_type(query, :pistol),
do: query |> where([at: at], at.class == :pistol) do: query |> where([t: t], t.class == :pistol)
defp list_packs_for_container_filter_type(query, :shotgun), defp list_packs_for_container_filter_type(query, :shotgun),
do: query |> where([at: at], at.class == :shotgun) do: query |> where([t: t], t.class == :shotgun)
defp list_packs_for_container_filter_type(query, _all), do: query defp list_packs_for_container_filter_type(query, _all), do: query
@ -757,18 +757,18 @@ defmodule Cannery.Ammo do
def list_packs(search, class, %User{id: user_id}, show_used \\ false) do def list_packs(search, class, %User{id: user_id}, show_used \\ false) do
from(p in Pack, from(p in Pack,
as: :p, as: :p,
join: at in assoc(p, :type), join: t in assoc(p, :type),
as: :at, as: :t,
join: c in Container, join: c in Container,
on: p.container_id == c.id, on: p.container_id == c.id,
on: p.user_id == c.user_id, on: p.user_id == c.user_id,
as: :c, as: :c,
left_join: ct in ContainerTag, left_join: ct in ContainerTag,
on: c.id == ct.container_id, on: c.id == ct.container_id,
left_join: t in Tag, left_join: tag in Tag,
on: ct.tag_id == t.id, on: ct.tag_id == tag.id,
on: c.user_id == t.user_id, on: c.user_id == tag.user_id,
as: :t, as: :tag,
where: p.user_id == ^user_id, where: p.user_id == ^user_id,
distinct: p.id, distinct: p.id,
preload: ^@pack_preloads preload: ^@pack_preloads
@ -781,13 +781,13 @@ defmodule Cannery.Ammo do
@spec list_packs_class(Queryable.t(), Type.class() | :all) :: Queryable.t() @spec list_packs_class(Queryable.t(), Type.class() | :all) :: Queryable.t()
defp list_packs_class(query, :rifle), defp list_packs_class(query, :rifle),
do: query |> where([at: at], at.class == :rifle) do: query |> where([t: t], t.class == :rifle)
defp list_packs_class(query, :pistol), defp list_packs_class(query, :pistol),
do: query |> where([at: at], at.class == :pistol) do: query |> where([t: t], t.class == :pistol)
defp list_packs_class(query, :shotgun), defp list_packs_class(query, :shotgun),
do: query |> where([at: at], at.class == :shotgun) do: query |> where([t: t], t.class == :shotgun)
defp list_packs_class(query, _all), do: query defp list_packs_class(query, _all), do: query
@ -807,7 +807,7 @@ defmodule Cannery.Ammo do
query query
|> where( |> where(
[p: p, at: at, c: c, t: t], [p: p, t: t, c: c, tag: tag],
fragment( fragment(
"? @@ websearch_to_tsquery('english', ?)", "? @@ websearch_to_tsquery('english', ?)",
p.search, p.search,
@ -815,7 +815,7 @@ defmodule Cannery.Ammo do
) or ) or
fragment( fragment(
"? @@ websearch_to_tsquery('english', ?)", "? @@ websearch_to_tsquery('english', ?)",
at.search, t.search,
^trimmed_search ^trimmed_search
) or ) or
fragment( fragment(
@ -825,7 +825,7 @@ defmodule Cannery.Ammo do
) or ) or
fragment( fragment(
"? @@ websearch_to_tsquery('english', ?)", "? @@ websearch_to_tsquery('english', ?)",
t.search, tag.search,
^trimmed_search ^trimmed_search
) )
) )