Skip to content

Commit

Permalink
feat(ex/detours): add add_author?/2 function and replace with_author
Browse files Browse the repository at this point in the history
  • Loading branch information
firestack committed Jan 31, 2025
1 parent db4e3ca commit 3304ae5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
17 changes: 12 additions & 5 deletions lib/skate/detours/db/detour.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ defmodule Skate.Detours.Db.Detour do

## Detour virtual fields
# -------------------------------------------------------
field :status, Ecto.Enum, values: [:draft, :active, :past], virtual: true
field(:status, Ecto.Enum, values: [:draft, :active, :past], virtual: true) ::
simple_status() | nil

# Route properties
field :route_id, :string, virtual: true
Expand Down Expand Up @@ -69,6 +70,7 @@ defmodule Skate.Detours.Db.Detour do
%{virtual_fields: wanted_virtual_fields, fields: wanted_fields} = split_fields(fields)

query
|> add_author?(wanted_fields)
|> select_merge(^wanted_fields)
|> select_virtual_fields(wanted_virtual_fields)
end
Expand Down Expand Up @@ -106,6 +108,14 @@ defmodule Skate.Detours.Db.Detour do
order_by(query, desc: :updated_at)
end

defp add_author?(query, fields) do
if Keyword.has_key?(fields, :author) do
with_author(query)
else
query
end
end

@doc """
Joins the `Skate.Settings.Db.User` struct into the `Skate.Detours.Db.Detour`
via Ecto preload.
Expand All @@ -126,9 +136,6 @@ defmodule Skate.Detours.Db.Detour do

def select_detour_list_info(query \\ base()) do
query
|> preload([
:author
])
|> select_fields([
# Table Columns
:id,
Expand All @@ -148,7 +155,7 @@ defmodule Skate.Detours.Db.Detour do
:state_value,

# Nested Fields
author: [:email]
author: [:email, :id]
])
|> sorted_by_last_updated()
end
Expand Down
1 change: 0 additions & 1 deletion lib/skate/detours/detours.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ defmodule Skate.Detours.Detours do

def list_detours(fields) do
Skate.Detours.Db.Detour.Queries.select_fields(fields)
|> Skate.Detours.Db.Detour.Queries.with_author()
|> Skate.Detours.Db.Detour.Queries.sorted_by_last_updated()
|> Repo.all()
end
Expand Down

0 comments on commit 3304ae5

Please sign in to comment.