feat: include search lat/lng in station API meta response
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Enums\PriceClassification;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\NearbyStationsRequest;
|
||||
use App\Http\Resources\Api\StationResource;
|
||||
@@ -10,6 +11,7 @@ use App\Models\Station;
|
||||
use App\Services\PostcodeService;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class StationController extends Controller
|
||||
@@ -52,15 +54,21 @@ class StationController extends Controller
|
||||
->where('stations.permanent_closure', false)
|
||||
->get();
|
||||
|
||||
$stations = $all
|
||||
->filter(fn ($s) => (float) $s->distance_km <= $radius)
|
||||
->sortBy(match ($sort) {
|
||||
$filtered = $all->filter(fn ($s) => (float) $s->distance_km <= $radius);
|
||||
|
||||
$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,
|
||||
'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();
|
||||
})->values();
|
||||
|
||||
$prices = $stations->pluck('price_pence');
|
||||
|
||||
@@ -82,6 +90,8 @@ class StationController extends Controller
|
||||
'count' => $stations->count(),
|
||||
'fuel_type' => $fuelType->value,
|
||||
'radius_km' => $radius,
|
||||
'lat' => $lat,
|
||||
'lng' => $lng,
|
||||
'lowest_pence' => $prices->min(),
|
||||
'highest_pence' => $prices->max(),
|
||||
'cheapest_price_pence' => $prices->min(),
|
||||
|
||||
@@ -157,3 +157,17 @@ it('returns 422 when postcode cannot be resolved', function () {
|
||||
->assertUnprocessable()
|
||||
->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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user