From 6176359d1fb4f1f6399cb3435df050b16bef5724 Mon Sep 17 00:00:00 2001 From: Ovidiu U Date: Sun, 5 Apr 2026 19:52:20 +0100 Subject: [PATCH] feat: add updated and brand sort options to /api/stations endpoint 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. --- app/Http/Controllers/Api/StationController.php | 7 ++++++- app/Http/Requests/Api/NearbyStationsRequest.php | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/StationController.php b/app/Http/Controllers/Api/StationController.php index 40f058b..5eba8e8 100644 --- a/app/Http/Controllers/Api/StationController.php +++ b/app/Http/Controllers/Api/StationController.php @@ -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'); diff --git a/app/Http/Requests/Api/NearbyStationsRequest.php b/app/Http/Requests/Api/NearbyStationsRequest.php index ca63822..0bd934f 100644 --- a/app/Http/Requests/Api/NearbyStationsRequest.php +++ b/app/Http/Requests/Api/NearbyStationsRequest.php @@ -20,7 +20,7 @@ class NearbyStationsRequest extends FormRequest 'lng' => ['required_without:postcode', 'nullable', 'numeric', 'between:-180,180'], 'fuel_type' => ['required', 'string'], 'radius' => ['nullable', 'numeric', 'between:0.1,50'], - 'sort' => ['nullable', 'string', 'in:price,distance'], + 'sort' => ['nullable', 'string', 'in:price,distance,updated,brand'], 'pricing_mode' => ['nullable', 'string', 'in:pump'], ]; }