use sr for shot record in sql

This commit is contained in:
shibao 2023-06-05 18:35:19 -04:00
parent f0536f3030
commit a8fa321040
2 changed files with 37 additions and 37 deletions

View File

@ -27,16 +27,16 @@ defmodule Cannery.ActivityLog do
@spec list_shot_records(search :: nil | String.t(), Type.class() | :all, User.t()) :: @spec list_shot_records(search :: nil | String.t(), Type.class() | :all, User.t()) ::
[ShotRecord.t()] [ShotRecord.t()]
def list_shot_records(search \\ nil, type, %User{id: user_id}) do def list_shot_records(search \\ nil, type, %User{id: user_id}) do
from(sg in ShotRecord, from(sr in ShotRecord,
as: :sg, as: :sr,
left_join: p in Pack, left_join: p in Pack,
as: :p, as: :p,
on: sg.pack_id == p.id, on: sr.pack_id == p.id,
left_join: at in Type, left_join: at in Type,
as: :at, as: :at,
on: p.type_id == at.id, on: p.type_id == at.id,
where: sg.user_id == ^user_id, where: sr.user_id == ^user_id,
distinct: sg.id distinct: sr.id
) )
|> list_shot_records_search(search) |> list_shot_records_search(search)
|> list_shot_records_filter_type(type) |> list_shot_records_filter_type(type)
@ -52,10 +52,10 @@ defmodule Cannery.ActivityLog do
query query
|> where( |> where(
[sg: sg, p: p, at: at], [sr: sr, p: p, at: at],
fragment( fragment(
"? @@ websearch_to_tsquery('english', ?)", "? @@ websearch_to_tsquery('english', ?)",
sg.search, sr.search,
^trimmed_search ^trimmed_search
) or ) or
fragment( fragment(
@ -69,11 +69,11 @@ defmodule Cannery.ActivityLog do
^trimmed_search ^trimmed_search
) )
) )
|> order_by([sg: sg], { |> order_by([sr: sr], {
:desc, :desc,
fragment( fragment(
"ts_rank_cd(?, websearch_to_tsquery('english', ?), 4)", "ts_rank_cd(?, websearch_to_tsquery('english', ?), 4)",
sg.search, sr.search,
^trimmed_search ^trimmed_search
) )
}) })
@ -104,9 +104,9 @@ defmodule Cannery.ActivityLog do
@spec get_shot_record_count!(User.t()) :: integer() @spec get_shot_record_count!(User.t()) :: integer()
def get_shot_record_count!(%User{id: user_id}) do def get_shot_record_count!(%User{id: user_id}) do
Repo.one( Repo.one(
from sg in ShotRecord, from sr in ShotRecord,
where: sg.user_id == ^user_id, where: sr.user_id == ^user_id,
select: count(sg.id), select: count(sr.id),
distinct: true distinct: true
) || 0 ) || 0
end end
@ -117,9 +117,9 @@ defmodule Cannery.ActivityLog do
%User{id: user_id} %User{id: user_id}
) do ) do
Repo.all( Repo.all(
from sg in ShotRecord, from sr in ShotRecord,
where: sg.pack_id == ^pack_id, where: sr.pack_id == ^pack_id,
where: sg.user_id == ^user_id where: sr.user_id == ^user_id
) )
end end
@ -140,10 +140,10 @@ defmodule Cannery.ActivityLog do
@spec get_shot_record!(ShotRecord.id(), User.t()) :: ShotRecord.t() @spec get_shot_record!(ShotRecord.id(), User.t()) :: ShotRecord.t()
def get_shot_record!(id, %User{id: user_id}) do def get_shot_record!(id, %User{id: user_id}) do
Repo.one!( Repo.one!(
from sg in ShotRecord, from sr in ShotRecord,
where: sg.id == ^id, where: sr.id == ^id,
where: sg.user_id == ^user_id, where: sr.user_id == ^user_id,
order_by: sg.date order_by: sr.date
) )
end end
@ -308,11 +308,11 @@ defmodule Cannery.ActivityLog do
|> Enum.map(fn %{id: pack_id} -> pack_id end) |> Enum.map(fn %{id: pack_id} -> pack_id end)
Repo.all( Repo.all(
from sg in ShotRecord, from sr in ShotRecord,
where: sg.pack_id in ^pack_ids, where: sr.pack_id in ^pack_ids,
where: sg.user_id == ^user_id, where: sr.user_id == ^user_id,
group_by: sg.pack_id, group_by: sr.pack_id,
select: {sg.pack_id, sum(sg.count)} select: {sr.pack_id, sum(sr.count)}
) )
|> Map.new() |> Map.new()
end end
@ -337,11 +337,11 @@ defmodule Cannery.ActivityLog do
|> Enum.map(fn %Pack{id: pack_id, user_id: ^user_id} -> pack_id end) |> Enum.map(fn %Pack{id: pack_id, user_id: ^user_id} -> pack_id end)
Repo.all( Repo.all(
from sg in ShotRecord, from sr in ShotRecord,
where: sg.pack_id in ^pack_ids, where: sr.pack_id in ^pack_ids,
where: sg.user_id == ^user_id, where: sr.user_id == ^user_id,
group_by: sg.pack_id, group_by: sr.pack_id,
select: {sg.pack_id, max(sg.date)} select: {sr.pack_id, max(sr.date)}
) )
|> Map.new() |> Map.new()
end end
@ -385,12 +385,12 @@ defmodule Cannery.ActivityLog do
Repo.all( Repo.all(
from p in Pack, from p in Pack,
left_join: sg in ShotRecord, left_join: sr in ShotRecord,
on: p.id == sg.pack_id, on: p.id == sr.pack_id,
where: p.type_id in ^type_ids, where: p.type_id in ^type_ids,
where: not (sg.count |> is_nil()), where: not (sr.count |> is_nil()),
group_by: p.type_id, group_by: p.type_id,
select: {p.type_id, sum(sg.count)} select: {p.type_id, sum(sr.count)}
) )
|> Map.new() |> Map.new()
end end

View File

@ -167,10 +167,10 @@ 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)
sg_total_query = sg_total_query =
from sg in ShotRecord, from sr in ShotRecord,
where: not (sg.count |> is_nil()), where: not (sr.count |> is_nil()),
group_by: sg.pack_id, group_by: sr.pack_id,
select: %{pack_id: sg.pack_id, total: sum(sg.count)} select: %{pack_id: sr.pack_id, total: sum(sr.count)}
Repo.all( Repo.all(
from p in Pack, from p in Pack,