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 all commits
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
21 changes: 10 additions & 11 deletions lib/skate/detours/detours.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ 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
(detour in Skate.Detours.Db.Detour)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woah! I didn't think the macro would allow you to do this :o

|> from(
preload: [:author],
order_by: [desc: detour.updated_at]
)
|> Repo.all()
|> Repo.preload(:author)
end

@doc """
Expand All @@ -43,11 +46,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 +139,9 @@ 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)
(detour in Skate.Detours.Db.Detour)
|> from(where: detour.id == ^id, preload: [:author])
|> Repo.one!()

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