Add historic airports to database
This commit is contained in:
@@ -17,7 +17,10 @@ class PopulateAirportTimezones extends Command
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
Airport::whereNull('timezone')->chunkById(100, function ($airports) {
|
||||
Airport::where(function ($query) {
|
||||
$query->whereNull('timezone')
|
||||
->orWhere('timezone', 'Undefined');
|
||||
})->chunkById(100, function ($airports) {
|
||||
foreach ($airports as $airport) {
|
||||
$zoneName = null;
|
||||
$attempts = 0;
|
||||
|
||||
@@ -58,28 +58,38 @@ class SearchController extends Controller
|
||||
{
|
||||
$q = request('q', '');
|
||||
$len = strlen($q);
|
||||
$includeInactive = request()->boolean('include_inactive');
|
||||
|
||||
if ($len < 3) return [];
|
||||
|
||||
return Airport::with('region.country')
|
||||
->when(!$includeInactive, fn($query) => $query->where('active', true))
|
||||
->when($len === 3, fn($query) => $query->where('iata_code', 'ilike', $q))
|
||||
->when($len >= 4, fn($query) => $query->where(function ($sub) use ($q, $len) {
|
||||
$sub->when($len === 4, fn($s) => $s->where('icao_code', 'ilike', $q))
|
||||
->orWhere('name', 'ilike', "%{$q}%")
|
||||
->orWhere('municipality', 'ilike', "%{$q}%");
|
||||
})->orderByRaw("
|
||||
CASE
|
||||
WHEN icao_code = ? THEN 0
|
||||
WHEN iata_code = ? THEN 1
|
||||
ELSE 2
|
||||
END
|
||||
", [$q, $q]))
|
||||
}))
|
||||
->orderByRaw("
|
||||
CASE
|
||||
WHEN active THEN 0
|
||||
ELSE 1
|
||||
END
|
||||
")
|
||||
->orderByRaw("
|
||||
CASE
|
||||
WHEN icao_code = ? THEN 0
|
||||
WHEN iata_code = ? THEN 1
|
||||
ELSE 2
|
||||
END
|
||||
", [$q, $q])
|
||||
->limit(15)
|
||||
->get(['id', 'name', 'municipality', 'iata_code', 'icao_code', 'region_id'])
|
||||
->map(fn($airport) => [
|
||||
->get(['id', 'name', 'municipality', 'iata_code', 'icao_code', 'region_id', 'active'])
|
||||
->map(fn(Airport $airport) => [
|
||||
'value' => $airport->id,
|
||||
'title' => $airport->display_name,
|
||||
'country_code' => strtolower($airport->region->country->code),
|
||||
'active' => $airport->active,
|
||||
])
|
||||
->values();
|
||||
}
|
||||
|
||||
@@ -20,12 +20,14 @@ class Airport extends Model
|
||||
'icao_code',
|
||||
'iata_code',
|
||||
'local_code',
|
||||
'active',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'latitude_deg' => 'float',
|
||||
'longitude_deg' => 'float',
|
||||
'elevation_ft' => 'integer',
|
||||
'active' => 'boolean',
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
@@ -37,6 +39,8 @@ class Airport extends Model
|
||||
'LHR', 'LGW', 'STN', 'LTN', 'LCY', 'SEN',
|
||||
];
|
||||
|
||||
|
||||
|
||||
protected function displayName() : Attribute{
|
||||
return Attribute::make(
|
||||
get: function () {
|
||||
@@ -53,6 +57,11 @@ class Airport extends Model
|
||||
);
|
||||
}
|
||||
|
||||
public function scopeActive($query)
|
||||
{
|
||||
return $query->where('active', true);
|
||||
}
|
||||
|
||||
public function region(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Region::class);
|
||||
|
||||
Reference in New Issue
Block a user