fix pack sql naming issues

This commit is contained in:
shibao 2023-05-12 21:48:04 -04:00
parent 9306d0f970
commit 8a58d53dc1
3 changed files with 96 additions and 96 deletions

View File

@ -29,12 +29,12 @@ defmodule Cannery.ActivityLog do
def list_shot_records(search \\ nil, type, %{id: user_id}) do def list_shot_records(search \\ nil, type, %{id: user_id}) do
from(sg in ShotRecord, from(sg in ShotRecord,
as: :sg, as: :sg,
left_join: ag in Pack, left_join: p in Pack,
as: :ag, as: :p,
on: sg.pack_id == ag.id, on: sg.pack_id == p.id,
left_join: at in Type, left_join: at in Type,
as: :at, as: :at,
on: ag.type_id == at.id, on: p.type_id == at.id,
where: sg.user_id == ^user_id, where: sg.user_id == ^user_id,
distinct: sg.id distinct: sg.id
) )
@ -52,7 +52,7 @@ defmodule Cannery.ActivityLog do
query query
|> where( |> where(
[sg: sg, ag: ag, at: at], [sg: sg, p: p, at: at],
fragment( fragment(
"? @@ websearch_to_tsquery('english', ?)", "? @@ websearch_to_tsquery('english', ?)",
sg.search, sg.search,
@ -60,7 +60,7 @@ defmodule Cannery.ActivityLog do
) or ) or
fragment( fragment(
"? @@ websearch_to_tsquery('english', ?)", "? @@ websearch_to_tsquery('english', ?)",
ag.search, p.search,
^trimmed_search ^trimmed_search
) or ) or
fragment( fragment(
@ -172,9 +172,9 @@ defmodule Cannery.ActivityLog do
fn _repo, %{create_shot_record: %{pack_id: pack_id, user_id: user_id}} -> fn _repo, %{create_shot_record: %{pack_id: pack_id, user_id: user_id}} ->
pack = pack =
Repo.one( Repo.one(
from ag in Pack, from p in Pack,
where: ag.id == ^pack_id, where: p.id == ^pack_id,
where: ag.user_id == ^user_id where: p.user_id == ^user_id
) )
{:ok, pack} {:ok, pack}
@ -221,7 +221,7 @@ defmodule Cannery.ActivityLog do
|> Multi.run( |> Multi.run(
:pack, :pack,
fn repo, %{update_shot_record: %{pack_id: pack_id, user_id: user_id}} -> fn repo, %{update_shot_record: %{pack_id: pack_id, user_id: user_id}} ->
{:ok, repo.one(from ag in Pack, where: ag.id == ^pack_id and ag.user_id == ^user_id)} {:ok, repo.one(from p in Pack, where: p.id == ^pack_id and p.user_id == ^user_id)}
end end
) )
|> Multi.update( |> Multi.update(
@ -266,7 +266,7 @@ defmodule Cannery.ActivityLog do
|> Multi.run( |> Multi.run(
:pack, :pack,
fn repo, %{delete_shot_record: %{pack_id: pack_id, user_id: user_id}} -> fn repo, %{delete_shot_record: %{pack_id: pack_id, user_id: user_id}} ->
{:ok, repo.one(from ag in Pack, where: ag.id == ^pack_id and ag.user_id == ^user_id)} {:ok, repo.one(from p in Pack, where: p.id == ^pack_id and p.user_id == ^user_id)}
end end
) )
|> Multi.update( |> Multi.update(
@ -384,13 +384,13 @@ defmodule Cannery.ActivityLog do
|> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end) |> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end)
Repo.all( Repo.all(
from ag in Pack, from p in Pack,
left_join: sg in ShotRecord, left_join: sg in ShotRecord,
on: ag.id == sg.pack_id, on: p.id == sg.pack_id,
where: ag.type_id in ^type_ids, where: p.type_id in ^type_ids,
where: not (sg.count |> is_nil()), where: not (sg.count |> is_nil()),
group_by: ag.type_id, group_by: p.type_id,
select: {ag.type_id, sum(sg.count)} select: {p.type_id, sum(sg.count)}
) )
|> Map.new() |> Map.new()
end end

View File

@ -173,14 +173,14 @@ defmodule Cannery.Ammo do
select: %{pack_id: sg.pack_id, total: sum(sg.count)} select: %{pack_id: sg.pack_id, total: sum(sg.count)}
Repo.all( Repo.all(
from ag in Pack, from p in Pack,
as: :pack, as: :pack,
left_join: sg_query in subquery(sg_total_query), left_join: sg_query in subquery(sg_total_query),
on: ag.id == sg_query.pack_id, on: p.id == sg_query.pack_id,
where: ag.type_id in ^type_ids, where: p.type_id in ^type_ids,
group_by: ag.type_id, group_by: p.type_id,
where: not (ag.price_paid |> is_nil()), where: not (p.price_paid |> is_nil()),
select: {ag.type_id, sum(ag.price_paid) / sum(ag.count + coalesce(sg_query.total, 0))} select: {p.type_id, sum(p.price_paid) / sum(p.count + coalesce(sg_query.total, 0))}
) )
|> Map.new() |> Map.new()
end end
@ -224,11 +224,11 @@ defmodule Cannery.Ammo do
|> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end) |> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end)
Repo.all( Repo.all(
from ag in Pack, from p in Pack,
where: ag.type_id in ^type_ids, where: p.type_id in ^type_ids,
where: ag.user_id == ^user_id, where: p.user_id == ^user_id,
group_by: ag.type_id, group_by: p.type_id,
select: {ag.type_id, sum(ag.count)} select: {p.type_id, sum(p.count)}
) )
|> Map.new() |> Map.new()
end end
@ -402,10 +402,10 @@ defmodule Cannery.Ammo do
%User{id: user_id}, %User{id: user_id},
show_used show_used
) do ) do
from(ag in Pack, from(p in Pack,
as: :ag, as: :p,
where: ag.type_id == ^type_id, where: p.type_id == ^type_id,
where: ag.user_id == ^user_id, where: p.user_id == ^user_id,
preload: ^@pack_preloads preload: ^@pack_preloads
) )
|> list_packs_for_type_show_used(show_used) |> list_packs_for_type_show_used(show_used)
@ -415,7 +415,7 @@ defmodule Cannery.Ammo do
@spec list_packs_for_type_show_used(Queryable.t(), show_used :: boolean()) :: @spec list_packs_for_type_show_used(Queryable.t(), show_used :: boolean()) ::
Queryable.t() Queryable.t()
def list_packs_for_type_show_used(query, false), def list_packs_for_type_show_used(query, false),
do: query |> where([ag: ag], ag.count > 0) do: query |> where([p: p], p.count > 0)
def list_packs_for_type_show_used(query, _true), do: query def list_packs_for_type_show_used(query, _true), do: query
@ -449,13 +449,13 @@ defmodule Cannery.Ammo do
type, type,
%User{id: user_id} %User{id: user_id}
) do ) do
from(ag in Pack, from(p in Pack,
as: :ag, as: :p,
join: at in assoc(ag, :type), join: at in assoc(p, :type),
as: :at, as: :at,
where: ag.container_id == ^container_id, where: p.container_id == ^container_id,
where: ag.user_id == ^user_id, where: p.user_id == ^user_id,
where: ag.count > 0, where: p.count > 0,
preload: ^@pack_preloads preload: ^@pack_preloads
) )
|> list_packs_for_container_filter_type(type) |> list_packs_for_container_filter_type(type)
@ -490,10 +490,10 @@ defmodule Cannery.Ammo do
@spec get_packs_count!(User.t()) :: integer() @spec get_packs_count!(User.t()) :: integer()
@spec get_packs_count!(User.t(), show_used :: boolean()) :: integer() @spec get_packs_count!(User.t(), show_used :: boolean()) :: integer()
def get_packs_count!(%User{id: user_id}, show_used \\ false) do def get_packs_count!(%User{id: user_id}, show_used \\ false) do
from(ag in Pack, from(p in Pack,
as: :ag, as: :p,
where: ag.user_id == ^user_id, where: p.user_id == ^user_id,
select: count(ag.id), select: count(p.id),
distinct: true distinct: true
) )
|> get_packs_count_show_used(show_used) |> get_packs_count_show_used(show_used)
@ -502,7 +502,7 @@ defmodule Cannery.Ammo do
@spec get_packs_count_show_used(Queryable.t(), show_used :: boolean()) :: Queryable.t() @spec get_packs_count_show_used(Queryable.t(), show_used :: boolean()) :: Queryable.t()
defp get_packs_count_show_used(query, false), defp get_packs_count_show_used(query, false),
do: query |> where([ag: ag], ag.count > 0) do: query |> where([p: p], p.count > 0)
defp get_packs_count_show_used(query, _true), do: query defp get_packs_count_show_used(query, _true), do: query
@ -566,12 +566,12 @@ defmodule Cannery.Ammo do
types types
|> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end) |> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end)
from(ag in Pack, from(p in Pack,
as: :ag, as: :p,
where: ag.user_id == ^user_id, where: p.user_id == ^user_id,
where: ag.type_id in ^type_ids, where: p.type_id in ^type_ids,
group_by: ag.type_id, group_by: p.type_id,
select: {ag.type_id, count(ag.id)} select: {p.type_id, count(p.id)}
) )
|> get_packs_count_for_types_maybe_show_used(show_used) |> get_packs_count_for_types_maybe_show_used(show_used)
|> Repo.all() |> Repo.all()
@ -583,7 +583,7 @@ defmodule Cannery.Ammo do
defp get_packs_count_for_types_maybe_show_used(query, true), do: query defp get_packs_count_for_types_maybe_show_used(query, true), do: query
defp get_packs_count_for_types_maybe_show_used(query, _false) do defp get_packs_count_for_types_maybe_show_used(query, _false) do
query |> where([ag: ag], not (ag.count == 0)) query |> where([p: p], not (p.count == 0))
end end
@doc """ @doc """
@ -625,12 +625,12 @@ defmodule Cannery.Ammo do
|> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end) |> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end)
Repo.all( Repo.all(
from ag in Pack, from p in Pack,
where: ag.user_id == ^user_id, where: p.user_id == ^user_id,
where: ag.type_id in ^type_ids, where: p.type_id in ^type_ids,
where: ag.count == 0, where: p.count == 0,
group_by: ag.type_id, group_by: p.type_id,
select: {ag.type_id, count(ag.id)} select: {p.type_id, count(p.id)}
) )
|> Map.new() |> Map.new()
end end
@ -678,11 +678,11 @@ defmodule Cannery.Ammo do
|> Enum.map(fn %Container{id: container_id, user_id: ^user_id} -> container_id end) |> Enum.map(fn %Container{id: container_id, user_id: ^user_id} -> container_id end)
Repo.all( Repo.all(
from ag in Pack, from p in Pack,
where: ag.container_id in ^container_ids, where: p.container_id in ^container_ids,
where: ag.count > 0, where: p.count > 0,
group_by: ag.container_id, group_by: p.container_id,
select: {ag.container_id, count(ag.id)} select: {p.container_id, count(p.id)}
) )
|> Map.new() |> Map.new()
end end
@ -726,10 +726,10 @@ defmodule Cannery.Ammo do
|> Enum.map(fn %Container{id: container_id, user_id: ^user_id} -> container_id end) |> Enum.map(fn %Container{id: container_id, user_id: ^user_id} -> container_id end)
Repo.all( Repo.all(
from ag in Pack, from p in Pack,
where: ag.container_id in ^container_ids, where: p.container_id in ^container_ids,
group_by: ag.container_id, group_by: p.container_id,
select: {ag.container_id, sum(ag.count)} select: {p.container_id, sum(p.count)}
) )
|> Map.new() |> Map.new()
end end
@ -754,14 +754,14 @@ defmodule Cannery.Ammo do
User.t(), User.t(),
show_used :: boolean() show_used :: boolean()
) :: [Pack.t()] ) :: [Pack.t()]
def list_packs(search, type, %{id: user_id}, show_used \\ false) do def list_packs(search, class, %{id: user_id}, show_used \\ false) do
from(ag in Pack, from(p in Pack,
as: :ag, as: :p,
join: at in assoc(ag, :type), join: at in assoc(p, :type),
as: :at, as: :at,
join: c in Container, join: c in Container,
on: ag.container_id == c.id, on: p.container_id == c.id,
on: ag.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,
@ -769,33 +769,33 @@ defmodule Cannery.Ammo do
on: ct.tag_id == t.id, on: ct.tag_id == t.id,
on: c.user_id == t.user_id, on: c.user_id == t.user_id,
as: :t, as: :t,
where: ag.user_id == ^user_id, where: p.user_id == ^user_id,
distinct: ag.id, distinct: p.id,
preload: ^@pack_preloads preload: ^@pack_preloads
) )
|> list_packs_filter_on_type(type) |> list_packs_class(class)
|> list_packs_show_used(show_used) |> list_packs_show_used(show_used)
|> list_packs_search(search) |> list_packs_search(search)
|> Repo.all() |> Repo.all()
end end
@spec list_packs_filter_on_type(Queryable.t(), Type.class() | :all) :: Queryable.t() @spec list_packs_class(Queryable.t(), Type.class() | :all) :: Queryable.t()
defp list_packs_filter_on_type(query, :rifle), defp list_packs_class(query, :rifle),
do: query |> where([at: at], at.class == :rifle) do: query |> where([at: at], at.class == :rifle)
defp list_packs_filter_on_type(query, :pistol), defp list_packs_class(query, :pistol),
do: query |> where([at: at], at.class == :pistol) do: query |> where([at: at], at.class == :pistol)
defp list_packs_filter_on_type(query, :shotgun), defp list_packs_class(query, :shotgun),
do: query |> where([at: at], at.class == :shotgun) do: query |> where([at: at], at.class == :shotgun)
defp list_packs_filter_on_type(query, _all), do: query defp list_packs_class(query, _all), do: query
@spec list_packs_show_used(Queryable.t(), show_used :: boolean()) :: Queryable.t() @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, true), do: query
defp list_packs_show_used(query, _false) do defp list_packs_show_used(query, _false) do
query |> where([ag: ag], not (ag.count == 0)) query |> where([p: p], not (p.count == 0))
end end
@spec list_packs_show_used(Queryable.t(), search :: String.t() | nil) :: Queryable.t() @spec list_packs_show_used(Queryable.t(), search :: String.t() | nil) :: Queryable.t()
@ -807,10 +807,10 @@ defmodule Cannery.Ammo do
query query
|> where( |> where(
[ag: ag, at: at, c: c, t: t], [p: p, at: at, c: c, t: t],
fragment( fragment(
"? @@ websearch_to_tsquery('english', ?)", "? @@ websearch_to_tsquery('english', ?)",
ag.search, p.search,
^trimmed_search ^trimmed_search
) or ) or
fragment( fragment(
@ -830,11 +830,11 @@ defmodule Cannery.Ammo do
) )
) )
|> order_by( |> order_by(
[ag: ag], [p: p],
desc: desc:
fragment( fragment(
"ts_rank_cd(?, websearch_to_tsquery('english', ?), 4)", "ts_rank_cd(?, websearch_to_tsquery('english', ?), 4)",
ag.search, p.search,
^trimmed_search ^trimmed_search
) )
) )
@ -852,9 +852,9 @@ defmodule Cannery.Ammo do
@spec list_staged_packs(User.t()) :: [Pack.t()] @spec list_staged_packs(User.t()) :: [Pack.t()]
def list_staged_packs(%User{id: user_id}) do def list_staged_packs(%User{id: user_id}) do
Repo.all( Repo.all(
from ag in Pack, from p in Pack,
where: ag.user_id == ^user_id, where: p.user_id == ^user_id,
where: ag.staged == true, where: p.staged == true,
preload: ^@pack_preloads preload: ^@pack_preloads
) )
end end
@ -891,11 +891,11 @@ defmodule Cannery.Ammo do
%{optional(Pack.id()) => Pack.t()} %{optional(Pack.id()) => Pack.t()}
def get_packs(ids, %User{id: user_id}) do def get_packs(ids, %User{id: user_id}) do
Repo.all( Repo.all(
from ag in Pack, from p in Pack,
where: ag.id in ^ids, where: p.id in ^ids,
where: ag.user_id == ^user_id, where: p.user_id == ^user_id,
preload: ^@pack_preloads, preload: ^@pack_preloads,
select: {ag.id, ag} select: {p.id, p}
) )
|> Map.new() |> Map.new()
end end

View File

@ -203,9 +203,9 @@ defmodule Cannery.Containers do
{:ok, Container.t()} | {:error, Container.changeset()} {:ok, Container.t()} | {:error, Container.changeset()}
def delete_container(%Container{user_id: user_id} = container, %User{id: user_id}) do def delete_container(%Container{user_id: user_id} = container, %User{id: user_id}) do
Repo.one( Repo.one(
from ag in Pack, from p in Pack,
where: ag.container_id == ^container.id, where: p.container_id == ^container.id,
select: count(ag.id) select: count(p.id)
) )
|> case do |> case do
0 -> 0 ->
@ -305,10 +305,10 @@ defmodule Cannery.Containers do
@spec list_tags(search :: nil | String.t(), User.t()) :: [Tag.t()] @spec list_tags(search :: nil | String.t(), User.t()) :: [Tag.t()]
def list_tags(search \\ nil, user) def list_tags(search \\ nil, user)
def list_tags(search, %{id: user_id}) when search |> is_nil() or search == "", def list_tags(search, %User{id: user_id}) when search |> is_nil() or search == "",
do: Repo.all(from t in Tag, where: t.user_id == ^user_id, order_by: t.name) do: Repo.all(from t in Tag, where: t.user_id == ^user_id, order_by: t.name)
def list_tags(search, %{id: user_id}) when search |> is_binary() do def list_tags(search, %User{id: user_id}) when search |> is_binary() do
trimmed_search = String.trim(search) trimmed_search = String.trim(search)
Repo.all( Repo.all(