feat: add price classification enum and reliable sort option
This commit is contained in:
49
app/Enums/PriceClassification.php
Normal file
49
app/Enums/PriceClassification.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
enum PriceClassification: string
|
||||
{
|
||||
case Current = 'current';
|
||||
case Recent = 'recent';
|
||||
case Stale = 'stale';
|
||||
case Outdated = 'outdated';
|
||||
|
||||
public static function fromUpdatedAt(?Carbon $updatedAt): self
|
||||
{
|
||||
if ($updatedAt === null) {
|
||||
return self::Outdated;
|
||||
}
|
||||
|
||||
$hours = $updatedAt->diffInHours(now());
|
||||
|
||||
return match (true) {
|
||||
$hours < 24 => self::Current,
|
||||
$hours < 48 => self::Recent,
|
||||
$hours < 120 => self::Stale,
|
||||
default => self::Outdated,
|
||||
};
|
||||
}
|
||||
|
||||
public function weight(): int
|
||||
{
|
||||
return match ($this) {
|
||||
self::Current => 0,
|
||||
self::Recent => 1,
|
||||
self::Stale => 2,
|
||||
self::Outdated => 3,
|
||||
};
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::Current => 'Current',
|
||||
self::Recent => 'Recent',
|
||||
self::Stale => 'Stale',
|
||||
self::Outdated => 'Outdated',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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,updated,brand'],
|
||||
'sort' => ['nullable', 'string', 'in:price,distance,updated,brand,reliable'],
|
||||
'pricing_mode' => ['nullable', 'string', 'in:pump'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Resources\Api;
|
||||
|
||||
use App\Enums\PriceClassification;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Carbon;
|
||||
@@ -26,6 +27,12 @@ class StationResource extends JsonResource
|
||||
'price_updated_at' => $this->price_effective_at
|
||||
? Carbon::parse($this->price_effective_at)->toISOString()
|
||||
: null,
|
||||
'price_classification' => PriceClassification::fromUpdatedAt(
|
||||
$this->price_effective_at ? Carbon::parse($this->price_effective_at) : null
|
||||
)->value,
|
||||
'price_classification_label' => PriceClassification::fromUpdatedAt(
|
||||
$this->price_effective_at ? Carbon::parse($this->price_effective_at) : null
|
||||
)->label(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ class StationSearch extends Component
|
||||
#[Validate('required|integer|min:1|max:20')]
|
||||
public int $radius = 5;
|
||||
|
||||
#[Validate('nullable|string|in:price,distance,updated,brand')]
|
||||
public string $sort = 'price';
|
||||
#[Validate('nullable|string|in:price,distance,updated,brand,reliable')]
|
||||
public string $sort = 'reliable';
|
||||
|
||||
public array $results = [];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user