feat: add updated and brand sort options to /api/stations endpoint
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.3) (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled

Extends NearbyStationsRequest validation to accept `sort=updated` and `sort=brand`. Updates StationController to use match expression with dedicated closures: 'updated' sorts by price_effective_at descending (newest first), 'brand' sorts alphabetically by brand_name, with existing price and distance options.
This commit is contained in:
Ovidiu U
2026-04-05 19:52:20 +01:00
parent 6249ed8fe2
commit 6176359d1f
2 changed files with 7 additions and 2 deletions

View File

@@ -54,7 +54,12 @@ class StationController extends Controller
$stations = $all
->filter(fn ($s) => (float) $s->distance_km <= $radius)
->sortBy($sort === 'price' ? 'price_pence' : 'distance_km')
->sortBy(match ($sort) {
'price' => fn ($s) => (int) $s->price_pence,
'updated' => fn ($s) => $s->price_effective_at ? -strtotime($s->price_effective_at) : PHP_INT_MAX,
'brand' => fn ($s) => strtolower((string) $s->brand_name),
default => fn ($s) => (float) $s->distance_km,
})
->values();
$prices = $stations->pluck('price_pence');