Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tweak: Preload author and retrieve detours in one query #2804

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions lib/skate/detours/detours.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ defmodule Skate.Detours.Detours do
alias Skate.Settings.User

@doc """
Returns the list of detours.
Returns the list of detours with author, sorted by updated_at

## Examples

iex> list_detours()
[%Detour{}, ...]
"""
def list_detours do
Detour
from(detour in Skate.Detours.Db.Detour,
preload: [:author],
order_by: [desc: detour.updated_at]
)
|> Repo.all()
|> Repo.preload(:author)
end

@doc """
Expand All @@ -43,11 +45,7 @@ defmodule Skate.Detours.Detours do
}
def grouped_detours(user_id) do
detours =
Repo.all(
from(d in Detour,
order_by: [desc: d.updated_at]
)
)
list_detours()
|> Enum.map(fn detour -> db_detour_to_detour(detour, user_id) end)
|> Enum.filter(& &1)
|> Enum.group_by(fn detour -> detour.status end)
Expand Down Expand Up @@ -140,9 +138,8 @@ defmodule Skate.Detours.Detours do
@spec get_detour_with_state!(integer()) :: DetourWithState.t()
def get_detour_with_state!(id) do
detour =
id
|> get_detour!()
|> Repo.preload(:author)
from(detour in Skate.Detours.Db.Detour, where: detour.id == ^id, preload: [:author])
|> Repo.one!()

%DetourWithState{
state: detour.state,
Expand Down
Loading