From 61af5fe2266db0ee8fcd6e370b5bb30b905227d1 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Sun, 11 Feb 2018 20:38:56 -0600 Subject: [PATCH] Cleanup column types and assign Time class to fields #189 --- app/Database/factories/FlightFactory.php | 2 +- app/Database/factories/PirepFactory.php | 4 +- ...2017_06_17_214650_create_flight_tables.php | 2 +- .../2017_06_28_195426_create_pirep_tables.php | 4 +- app/Http/Resources/Pirep.php | 4 ++ app/Models/Flight.php | 27 ++++++++++ app/Models/Pirep.php | 49 +++++++++++++++++++ app/Support/Units/Time.php | 4 +- 8 files changed, 89 insertions(+), 7 deletions(-) diff --git a/app/Database/factories/FlightFactory.php b/app/Database/factories/FlightFactory.php index 6a47bd9b..d7eebbb9 100644 --- a/app/Database/factories/FlightFactory.php +++ b/app/Database/factories/FlightFactory.php @@ -28,7 +28,7 @@ $factory->define(App\Models\Flight::class, function (Faker $faker) use ($airline 'route' => $faker->randomElement(['', $faker->text(5)]), 'dpt_time' => $faker->time(), 'arr_time' => $faker->time(), - 'flight_time' => $faker->randomFloat(2), + 'flight_time' => $faker->numberBetween(60, 360), 'has_bid' => false, 'active' => true, 'created_at' => $faker->dateTimeBetween('-1 week', 'now'), diff --git a/app/Database/factories/PirepFactory.php b/app/Database/factories/PirepFactory.php index 3b89c1e5..323b8d93 100644 --- a/app/Database/factories/PirepFactory.php +++ b/app/Database/factories/PirepFactory.php @@ -37,8 +37,8 @@ $factory->define(App\Models\Pirep::class, function (Faker $faker) { 'level' => $faker->numberBetween(20, 400), 'distance' => $faker->randomFloat(2), 'planned_distance' => $faker->randomFloat(2), - 'flight_time' => $faker->randomFloat(2), - 'planned_flight_time' => $faker->randomFloat(2), + 'flight_time' => $faker->numberBetween(60, 360), + 'planned_flight_time' => $faker->numberBetween(60, 360), 'zfw' => $faker->randomFloat(2), 'block_fuel' => $faker->randomFloat(2), 'fuel_used' => $faker->randomFloat(2), diff --git a/app/Database/migrations/2017_06_17_214650_create_flight_tables.php b/app/Database/migrations/2017_06_17_214650_create_flight_tables.php index 3e515f41..5a1b7c31 100644 --- a/app/Database/migrations/2017_06_17_214650_create_flight_tables.php +++ b/app/Database/migrations/2017_06_17_214650_create_flight_tables.php @@ -28,7 +28,7 @@ class CreateFlightTables extends Migration $table->string('arr_time', 10)->nullable(); $table->unsignedInteger('level')->nullable()->default(0); $table->unsignedDecimal('distance', 19)->nullable()->default(0.0); - $table->unsignedDecimal('flight_time', 19)->nullable(); + $table->unsignedInteger('flight_time')->nullable(); $table->tinyInteger('flight_type')->default(FlightType::PASSENGER); $table->text('route')->nullable(); $table->text('notes')->nullable(); diff --git a/app/Database/migrations/2017_06_28_195426_create_pirep_tables.php b/app/Database/migrations/2017_06_28_195426_create_pirep_tables.php index f55983db..90bdc503 100644 --- a/app/Database/migrations/2017_06_28_195426_create_pirep_tables.php +++ b/app/Database/migrations/2017_06_28_195426_create_pirep_tables.php @@ -31,8 +31,8 @@ class CreatePirepTables extends Migration $table->unsignedInteger('level')->nullable(); $table->unsignedDecimal('distance')->nullable(); $table->unsignedDecimal('planned_distance')->nullable(); - $table->unsignedDecimal('flight_time', 19)->nullable(); - $table->unsignedDecimal('planned_flight_time', 19)->nullable(); + $table->unsignedInteger('flight_time')->nullable(); + $table->unsignedInteger('planned_flight_time')->nullable(); $table->unsignedDecimal('zfw', 19)->nullable(); $table->unsignedDecimal('block_fuel', 19)->nullable(); $table->unsignedDecimal('fuel_used', 19)->nullable(); diff --git a/app/Http/Resources/Pirep.php b/app/Http/Resources/Pirep.php index 3b6191b4..a5669b7c 100644 --- a/app/Http/Resources/Pirep.php +++ b/app/Http/Resources/Pirep.php @@ -25,6 +25,10 @@ class Pirep extends Resource $pirep['planned_distance'] = $this->planned_distance->toObject(); } + /* + * Relationship fields + */ + $pirep['airline'] = new Airline($this->airline); $pirep['dpt_airport'] = new Airport($this->dpt_airport); $pirep['arr_airport'] = new Airport($this->arr_airport); diff --git a/app/Models/Flight.php b/app/Models/Flight.php index 0831ffe7..9c4df030 100644 --- a/app/Models/Flight.php +++ b/app/Models/Flight.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Support\Units\Distance; +use App\Support\Units\Time; use PhpUnitsOfMeasure\Exception\NonNumericValue; use PhpUnitsOfMeasure\Exception\NonStringUnitName; @@ -28,6 +29,7 @@ class Flight extends BaseModel 'arr_time', 'level', 'distance', + 'flight_time', 'flight_type', 'route', 'notes', @@ -39,6 +41,7 @@ class Flight extends BaseModel 'flight_number' => 'integer', 'level' => 'integer', 'distance' => 'float', + 'flight_time' => 'integer', 'flight_type' => 'integer', 'has_bid' => 'boolean', 'active' => 'boolean', @@ -108,6 +111,30 @@ class Flight extends BaseModel } } + /** + * @return Time + */ + public function getFlightTimeAttribute() + { + if (!array_key_exists('flight_time', $this->attributes)) { + return null; + } + + return new Time($this->attributes['flight_time']); + } + + /** + * @param $value + */ + public function setFlightTimeAttribute($value) + { + if ($value instanceof Time) { + $this->attributes['flight_time'] = $value->getMinutes(); + } else { + $this->attributes['flight_time'] = $value; + } + } + /** * Relationship */ diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index 83bc1eb4..956a2c02 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -7,6 +7,7 @@ use App\Models\Enums\PirepState; use App\Models\Traits\HashId; use App\Support\Units\Distance; +use App\Support\Units\Time; use PhpUnitsOfMeasure\Exception\NonNumericValue; use PhpUnitsOfMeasure\Exception\NonStringUnitName; @@ -137,6 +138,30 @@ class Pirep extends BaseModel } } + /** + * @return Time + */ + public function getFlightTimeAttribute() + { + if (!array_key_exists('flight_time', $this->attributes)) { + return null; + } + + return new Time($this->attributes['flight_time']); + } + + /** + * @param $value + */ + public function setFlightTimeAttribute($value) + { + if($value instanceof Time) { + $this->attributes['flight_time'] = $value->getMinutes(); + } else { + $this->attributes['flight_time'] = $value; + } + } + /** * Return the planned_distance in a converter class * @return int|Distance @@ -172,6 +197,30 @@ class Pirep extends BaseModel } } + /** + * @return Time + */ + public function getPlannedFlightTimeAttribute() + { + if (!array_key_exists('planned_flight_time', $this->attributes)) { + return null; + } + + return new Time($this->attributes['planned_flight_time']); + } + + /** + * @param $value + */ + public function setPlannedFlightTimeAttribute($value) + { + if ($value instanceof Time) { + $this->attributes['planned_flight_time'] = $value->getMinutes(); + } else { + $this->attributes['planned_flight_time'] = $value; + } + } + /** * Do some cleanup on the route * @param $route diff --git a/app/Support/Units/Time.php b/app/Support/Units/Time.php index 66bf2b50..8745e66c 100644 --- a/app/Support/Units/Time.php +++ b/app/Support/Units/Time.php @@ -20,8 +20,10 @@ class Time implements Arrayable */ public function __construct($minutes, $hours=null) { + $minutes = (int) $minutes; + if(!empty($hours)) { - $this->hours = $hours; + $this->hours = (int) $hours; } else { $this->hours = floor($minutes / 60); }