feat: include search lat/lng in station API meta response

This commit is contained in:
Ovidiu U
2026-04-06 09:37:39 +01:00
parent 0955873221
commit cef21a4f0f
2 changed files with 29 additions and 5 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Enums\PriceClassification;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\Api\NearbyStationsRequest; use App\Http\Requests\Api\NearbyStationsRequest;
use App\Http\Resources\Api\StationResource; use App\Http\Resources\Api\StationResource;
@@ -10,6 +11,7 @@ use App\Models\Station;
use App\Services\PostcodeService; use App\Services\PostcodeService;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Support\Carbon;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
class StationController extends Controller class StationController extends Controller
@@ -52,15 +54,21 @@ class StationController extends Controller
->where('stations.permanent_closure', false) ->where('stations.permanent_closure', false)
->get(); ->get();
$stations = $all $filtered = $all->filter(fn ($s) => (float) $s->distance_km <= $radius);
->filter(fn ($s) => (float) $s->distance_km <= $radius)
->sortBy(match ($sort) { $stations = $sort === 'reliable'
? $filtered->sortBy([
fn ($s) => PriceClassification::fromUpdatedAt(
$s->price_effective_at ? Carbon::parse($s->price_effective_at) : null
)->weight(),
fn ($s) => (int) $s->price_pence,
])->values()
: $filtered->sortBy(match ($sort) {
'price' => fn ($s) => (int) $s->price_pence, 'price' => fn ($s) => (int) $s->price_pence,
'updated' => fn ($s) => $s->price_effective_at ? -strtotime($s->price_effective_at) : PHP_INT_MAX, 'updated' => fn ($s) => $s->price_effective_at ? -strtotime($s->price_effective_at) : PHP_INT_MAX,
'brand' => fn ($s) => strtolower((string) $s->brand_name), 'brand' => fn ($s) => strtolower((string) $s->brand_name),
default => fn ($s) => (float) $s->distance_km, default => fn ($s) => (float) $s->distance_km,
}) })->values();
->values();
$prices = $stations->pluck('price_pence'); $prices = $stations->pluck('price_pence');
@@ -82,6 +90,8 @@ class StationController extends Controller
'count' => $stations->count(), 'count' => $stations->count(),
'fuel_type' => $fuelType->value, 'fuel_type' => $fuelType->value,
'radius_km' => $radius, 'radius_km' => $radius,
'lat' => $lat,
'lng' => $lng,
'lowest_pence' => $prices->min(), 'lowest_pence' => $prices->min(),
'highest_pence' => $prices->max(), 'highest_pence' => $prices->max(),
'cheapest_price_pence' => $prices->min(), 'cheapest_price_pence' => $prices->min(),

View File

@@ -157,3 +157,17 @@ it('returns 422 when postcode cannot be resolved', function () {
->assertUnprocessable() ->assertUnprocessable()
->assertJsonValidationErrors(['postcode']); ->assertJsonValidationErrors(['postcode']);
}); });
it('includes resolved lat and lng in meta', function () {
$station = Station::factory()->create(['lat' => 52.555064, 'lng' => -0.256119]);
StationPriceCurrent::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::B7Standard,
'price_pence' => 14500,
]);
$this->getJson('/api/stations?lat=52.555064&lng=-0.256119&fuel_type=diesel&radius=10')
->assertOk()
->assertJsonPath('meta.lat', 52.555064)
->assertJsonPath('meta.lng', -0.256119);
});