From 831b72fb2b1f633de1c7504836a79862c80c56d3 Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Wed, 15 Jan 2020 13:00:58 -0500 Subject: [PATCH] Set a default model value for airports on PIREP (#500) * Set a default model value for airports on PIREP * Fix airport icao reference * Default airport models --- app/Models/Pirep.php | 22 ++++++++++++++++++-- app/Repositories/AirportRepository.php | 7 ++----- app/Services/GeoService.php | 22 ++++++-------------- app/Services/ImportExport/FlightImporter.php | 16 +++++++------- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index c0af9770..63ccd2ac 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -420,7 +420,16 @@ class Pirep extends Model public function arr_airport() { - return $this->belongsTo(Airport::class, 'arr_airport_id'); + return $this->belongsTo(Airport::class, 'arr_airport_id') + ->withDefault(function ($model) { + if (!empty($this->attributes['arr_airport_id'])) { + $model->id = $this->attributes['arr_airport_id']; + $model->icao = $this->attributes['arr_airport_id']; + $model->name = $this->attributes['arr_airport_id']; + } + + return $model; + }); } public function alt_airport() @@ -430,7 +439,16 @@ class Pirep extends Model public function dpt_airport() { - return $this->belongsTo(Airport::class, 'dpt_airport_id'); + return $this->belongsTo(Airport::class, 'dpt_airport_id') + ->withDefault(function ($model) { + if (!empty($this->attributes['dpt_airport_id'])) { + $model->id = $this->attributes['dpt_airport_id']; + $model->icao = $this->attributes['dpt_airport_id']; + $model->name = $this->attributes['dpt_airport_id']; + } + + return $model; + }); } public function comments() diff --git a/app/Repositories/AirportRepository.php b/app/Repositories/AirportRepository.php index 4f7b3c47..22d6a364 100644 --- a/app/Repositories/AirportRepository.php +++ b/app/Repositories/AirportRepository.php @@ -7,9 +7,6 @@ use App\Models\Airport; use Prettus\Repository\Contracts\CacheableInterface; use Prettus\Repository\Traits\CacheableRepository; -/** - * Class AirportRepository - */ class AirportRepository extends Repository implements CacheableInterface { use CacheableRepository; @@ -27,8 +24,8 @@ class AirportRepository extends Repository implements CacheableInterface /** * Return the list of airports formatted for a select box * - * @param mixed $add_blank - * @param mixed $only_hubs + * @param bool $add_blank + * @param bool $only_hubs * * @return array */ diff --git a/app/Services/GeoService.php b/app/Services/GeoService.php index 8c524b5a..01197287 100644 --- a/app/Services/GeoService.php +++ b/app/Services/GeoService.php @@ -231,23 +231,13 @@ class GeoService extends Service ]); }*/ - // If there is a position update from ACARS, show where it is - // Otherwise, just assume it's at the arrival airport currently - if ($pirep->position) { - $position = [ - 'lat' => $pirep->position->lat, - 'lon' => $pirep->position->lon, - ]; - } else { - // if arrived, show it being at the arrival airport - $position = [ - 'lat' => $pirep->arr_airport->lat, - 'lon' => $pirep->arr_airport->lon, - ]; - } - return [ - 'position' => $position, + // If there is a position update from ACARS, show where it is + // Otherwise, just assume it's at the arrival airport currently + 'position' => [ + 'lat' => optional($pirep->position)->lat ?? $pirep->arr_airport->lat, + 'lon' => optional($pirep->position)->lon ?? $pirep->arr_airport->lon, + ], 'line' => $route->getLine(), 'points' => $route->getPoints(), 'airports' => [ diff --git a/app/Services/ImportExport/FlightImporter.php b/app/Services/ImportExport/FlightImporter.php index 825904ff..10696776 100644 --- a/app/Services/ImportExport/FlightImporter.php +++ b/app/Services/ImportExport/FlightImporter.php @@ -108,14 +108,6 @@ class FlightImporter extends ImportExport // Any specific transformations - // Check/calculate the distance - if (empty($row['distance'])) { - $row['distance'] = $this->airportSvc->calculateDistance( - $row['dpt_airport'], - $row['arr_airport'] - ); - } - // Check for a valid value $flight_type = $row['flight_type']; if (!array_key_exists($flight_type, FlightType::labels())) { @@ -139,6 +131,14 @@ class FlightImporter extends ImportExport $this->processAirport($row['alt_airport']); } + // Check/calculate the distance + if (empty($row['distance'])) { + $row['distance'] = $this->airportSvc->calculateDistance( + $row['dpt_airport'], + $row['arr_airport'] + ); + } + $this->processSubfleets($flight, $row['subfleets']); $this->processFares($flight, $row['fares']); $this->processFields($flight, $row['fields']);