feat: add Station, StationPrice, StationPriceCurrent, StationPriceArchive models and factories
This commit is contained in:
56
app/Models/Station.php
Normal file
56
app/Models/Station.php
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enums\FuelType;
|
||||||
|
use Database\Factories\StationFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
|
||||||
|
#[Fillable([
|
||||||
|
'node_id', 'trading_name', 'brand_name', 'is_same_trading_and_brand',
|
||||||
|
'is_supermarket', 'is_motorway_service_station', 'is_supermarket_service_station',
|
||||||
|
'temporary_closure', 'permanent_closure', 'permanent_closure_date',
|
||||||
|
'public_phone_number', 'address_line_1', 'address_line_2', 'city',
|
||||||
|
'county', 'country', 'postcode', 'lat', 'lng',
|
||||||
|
'amenities', 'opening_times', 'fuel_types', 'last_seen_at',
|
||||||
|
])]
|
||||||
|
class Station extends Model
|
||||||
|
{
|
||||||
|
/** @use HasFactory<StationFactory> */
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
public $timestamps = false;
|
||||||
|
protected $primaryKey = 'node_id';
|
||||||
|
public $incrementing = false;
|
||||||
|
protected $keyType = 'string';
|
||||||
|
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'is_same_trading_and_brand' => 'boolean',
|
||||||
|
'is_supermarket' => 'boolean',
|
||||||
|
'is_motorway_service_station' => 'boolean',
|
||||||
|
'is_supermarket_service_station' => 'boolean',
|
||||||
|
'temporary_closure' => 'boolean',
|
||||||
|
'permanent_closure' => 'boolean',
|
||||||
|
'permanent_closure_date' => 'date',
|
||||||
|
'amenities' => 'array',
|
||||||
|
'opening_times' => 'array',
|
||||||
|
'fuel_types' => 'array',
|
||||||
|
'last_seen_at' => 'datetime',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function currentPrices(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(StationPriceCurrent::class, 'station_id', 'node_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function prices(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(StationPrice::class, 'station_id', 'node_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
34
app/Models/StationPrice.php
Normal file
34
app/Models/StationPrice.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enums\FuelType;
|
||||||
|
use Database\Factories\StationPriceFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
#[Fillable(['station_id', 'fuel_type', 'price_pence', 'price_effective_at', 'price_reported_at', 'recorded_at'])]
|
||||||
|
class StationPrice extends Model
|
||||||
|
{
|
||||||
|
/** @use HasFactory<StationPriceFactory> */
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'fuel_type' => FuelType::class,
|
||||||
|
'price_effective_at' => 'datetime',
|
||||||
|
'price_reported_at' => 'datetime',
|
||||||
|
'recorded_at' => 'datetime',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function station(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Station::class, 'station_id', 'node_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
29
app/Models/StationPriceArchive.php
Normal file
29
app/Models/StationPriceArchive.php
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enums\FuelType;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
#[Fillable(['station_id', 'fuel_type', 'price_pence', 'price_effective_at', 'price_reported_at', 'recorded_at'])]
|
||||||
|
class StationPriceArchive extends Model
|
||||||
|
{
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'fuel_type' => FuelType::class,
|
||||||
|
'price_effective_at' => 'datetime',
|
||||||
|
'price_reported_at' => 'datetime',
|
||||||
|
'recorded_at' => 'datetime',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function station(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Station::class, 'station_id', 'node_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
41
app/Models/StationPriceCurrent.php
Normal file
41
app/Models/StationPriceCurrent.php
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enums\FuelType;
|
||||||
|
use Database\Factories\StationPriceCurrentFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
#[Fillable(['station_id', 'fuel_type', 'price_pence', 'price_effective_at', 'price_reported_at', 'recorded_at'])]
|
||||||
|
class StationPriceCurrent extends Model
|
||||||
|
{
|
||||||
|
/** @use HasFactory<StationPriceCurrentFactory> */
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
public $timestamps = false;
|
||||||
|
protected $primaryKey = null;
|
||||||
|
public $incrementing = false;
|
||||||
|
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'fuel_type' => FuelType::class,
|
||||||
|
'price_effective_at' => 'datetime',
|
||||||
|
'price_reported_at' => 'datetime',
|
||||||
|
'recorded_at' => 'datetime',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function station(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Station::class, 'station_id', 'node_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function priceInPence(): float
|
||||||
|
{
|
||||||
|
return $this->price_pence / 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
50
database/factories/StationFactory.php
Normal file
50
database/factories/StationFactory.php
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Models\Station;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/** @extends Factory<Station> */
|
||||||
|
class StationFactory extends Factory
|
||||||
|
{
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
$trading = $this->faker->company();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'node_id' => hash('sha256', $this->faker->unique()->uuid()),
|
||||||
|
'trading_name' => $trading,
|
||||||
|
'brand_name' => $trading,
|
||||||
|
'is_same_trading_and_brand' => true,
|
||||||
|
'is_supermarket' => false,
|
||||||
|
'is_motorway_service_station' => false,
|
||||||
|
'is_supermarket_service_station' => false,
|
||||||
|
'temporary_closure' => false,
|
||||||
|
'permanent_closure' => false,
|
||||||
|
'permanent_closure_date' => null,
|
||||||
|
'public_phone_number' => null,
|
||||||
|
'address_line_1' => $this->faker->streetAddress(),
|
||||||
|
'address_line_2' => null,
|
||||||
|
'city' => $this->faker->city(),
|
||||||
|
'county' => null,
|
||||||
|
'country' => 'England',
|
||||||
|
'postcode' => strtoupper($this->faker->postcode()),
|
||||||
|
'lat' => $this->faker->latitude(49.9, 60.9),
|
||||||
|
'lng' => $this->faker->longitude(-8.2, 1.8),
|
||||||
|
'amenities' => [],
|
||||||
|
'opening_times' => null,
|
||||||
|
'fuel_types' => ['E10', 'E5'],
|
||||||
|
'last_seen_at' => now(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supermarket(): static
|
||||||
|
{
|
||||||
|
return $this->state([
|
||||||
|
'trading_name' => 'Tesco',
|
||||||
|
'brand_name' => 'Tesco',
|
||||||
|
'is_supermarket' => true,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
24
database/factories/StationPriceCurrentFactory.php
Normal file
24
database/factories/StationPriceCurrentFactory.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Enums\FuelType;
|
||||||
|
use App\Models\Station;
|
||||||
|
use App\Models\StationPriceCurrent;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/** @extends Factory<StationPriceCurrent> */
|
||||||
|
class StationPriceCurrentFactory extends Factory
|
||||||
|
{
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'station_id' => Station::factory(),
|
||||||
|
'fuel_type' => FuelType::E10,
|
||||||
|
'price_pence' => $this->faker->numberBetween(12000, 18000),
|
||||||
|
'price_effective_at' => now()->subHour(),
|
||||||
|
'price_reported_at' => now()->subMinutes(30),
|
||||||
|
'recorded_at' => now(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
24
database/factories/StationPriceFactory.php
Normal file
24
database/factories/StationPriceFactory.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Enums\FuelType;
|
||||||
|
use App\Models\Station;
|
||||||
|
use App\Models\StationPrice;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/** @extends Factory<StationPrice> */
|
||||||
|
class StationPriceFactory extends Factory
|
||||||
|
{
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'station_id' => Station::factory(),
|
||||||
|
'fuel_type' => FuelType::E10,
|
||||||
|
'price_pence' => $this->faker->numberBetween(12000, 18000),
|
||||||
|
'price_effective_at' => now()->subDays($this->faker->numberBetween(1, 30)),
|
||||||
|
'price_reported_at' => now()->subDays($this->faker->numberBetween(1, 30)),
|
||||||
|
'recorded_at' => now(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user