Skip to content

Commit

Permalink
tweak: Preload author and retrieve detours in one query (#2804)
Browse files Browse the repository at this point in the history
* tweak: Fix detours query to do the author preload and detour retrieval in one query

---------

Co-authored-by: Kayla Firestack <firestack@users.noreply.github.com>
  • Loading branch information
hannahpurcell and firestack authored Sep 23, 2024
1 parent 89cd36c commit 0d06ca3
Showing 1 changed file with 10 additions and 11 deletions.
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)
|> 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

0 comments on commit 0d06ca3

Please sign in to comment.