From 485c6e86bb8cd4555fce54285a90d5a58ce18a72 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Tue, 20 Mar 2018 13:06:06 -0500 Subject: [PATCH] add separate flight_fields table and move over to flight_field_values --- ...2017_06_17_214650_create_flight_tables.php | 18 ++++++-- .../2017_06_28_195426_create_pirep_tables.php | 10 ++--- .../2017_12_12_174519_create_bids_table.php | 2 +- .../2017_12_20_005147_create_acars_tables.php | 2 +- app/Database/seeds/sample.yml | 6 +++ .../Controllers/Admin/FlightController.php | 36 +++++++++++++--- .../Controllers/Admin/PirepController.php | 6 +-- app/Interfaces/Model.php | 2 +- app/Models/Flight.php | 18 ++++---- app/Models/FlightField.php | 41 +++++++++++++++++++ ...{FlightFields.php => FlightFieldValue.php} | 6 +-- app/Models/Pirep.php | 2 - app/Models/Traits/HashIdTrait.php | 8 ++-- app/Routes/admin.php | 2 +- app/Services/FlightService.php | 23 ++++++++++- .../admin/flights/flight_fields.blade.php | 8 ++-- .../views/admin/flights/scripts.blade.php | 2 + 17 files changed, 147 insertions(+), 45 deletions(-) create mode 100644 app/Models/FlightField.php rename app/Models/{FlightFields.php => FlightFieldValue.php} (76%) 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 a7766987..15ca3f24 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 @@ -14,7 +14,7 @@ class CreateFlightTables extends Migration public function up() { Schema::create('flights', function (Blueprint $table) { - $table->string('id', \App\Models\Flight::ID_MAX_LENGTH); + $table->string('id', \App\Interfaces\Model::ID_MAX_LENGTH); $table->unsignedInteger('airline_id'); $table->string('flight_number', 10); $table->string('route_code', 5)->nullable(); @@ -42,7 +42,7 @@ class CreateFlightTables extends Migration }); Schema::create('flight_fare', function (Blueprint $table) { - $table->string('flight_id', \App\Models\Flight::ID_MAX_LENGTH); + $table->string('flight_id', \App\Interfaces\Model::ID_MAX_LENGTH); $table->unsignedInteger('fare_id'); $table->string('price', 10)->nullable(); $table->string('cost', 10)->nullable(); @@ -52,9 +52,21 @@ class CreateFlightTables extends Migration $table->primary(['flight_id', 'fare_id']); }); + /** + * Hold a master list of fields + */ Schema::create('flight_fields', function (Blueprint $table) { $table->increments('id'); - $table->string('flight_id', \App\Models\Flight::ID_MAX_LENGTH); + $table->string('name', 50); + $table->string('slug', 50)->nullable(); + }); + + /** + * The values for the actual fields + */ + Schema::create('flight_field_values', function (Blueprint $table) { + $table->bigIncrements('id'); + $table->string('flight_id', \App\Interfaces\Model::ID_MAX_LENGTH); $table->string('name', 50); $table->text('value'); $table->timestamps(); 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 2fb620ed..c9d766ac 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 @@ -16,7 +16,7 @@ class CreatePirepTables extends Migration public function up() { Schema::create('pireps', function (Blueprint $table) { - $table->string('id', \App\Models\Pirep::ID_MAX_LENGTH); + $table->string('id', \App\Interfaces\Model::ID_MAX_LENGTH); $table->unsignedInteger('user_id'); $table->unsignedInteger('airline_id'); $table->unsignedInteger('aircraft_id')->nullable(); @@ -52,7 +52,7 @@ class CreatePirepTables extends Migration Schema::create('pirep_comments', function (Blueprint $table) { $table->bigIncrements('id'); - $table->string('pirep_id', \App\Models\Pirep::ID_MAX_LENGTH); + $table->string('pirep_id', \App\Interfaces\Model::ID_MAX_LENGTH); $table->unsignedInteger('user_id'); $table->text('comment'); $table->timestamps(); @@ -60,7 +60,7 @@ class CreatePirepTables extends Migration Schema::create('pirep_fares', function (Blueprint $table) { $table->bigIncrements('id'); - $table->string('pirep_id', \App\Models\Pirep::ID_MAX_LENGTH); + $table->string('pirep_id', \App\Interfaces\Model::ID_MAX_LENGTH); $table->unsignedInteger('fare_id'); $table->unsignedInteger('count')->nullable()->default(0); @@ -68,7 +68,7 @@ class CreatePirepTables extends Migration }); Schema::create('pirep_fields', function (Blueprint $table) { - $table->bigIncrements('id'); + $table->increments('id'); $table->string('name', 50); $table->string('slug', 50)->nullable(); $table->boolean('required')->nullable()->default(false); @@ -76,7 +76,7 @@ class CreatePirepTables extends Migration Schema::create('pirep_field_values', function (Blueprint $table) { $table->bigIncrements('id'); - $table->string('pirep_id', \App\Models\Pirep::ID_MAX_LENGTH); + $table->string('pirep_id', \App\Interfaces\Model::ID_MAX_LENGTH); $table->string('name', 50); $table->string('slug', 50)->nullable(); $table->string('value')->nullable(); diff --git a/app/Database/migrations/2017_12_12_174519_create_bids_table.php b/app/Database/migrations/2017_12_12_174519_create_bids_table.php index 78c622d5..5b6c83ae 100644 --- a/app/Database/migrations/2017_12_12_174519_create_bids_table.php +++ b/app/Database/migrations/2017_12_12_174519_create_bids_table.php @@ -16,7 +16,7 @@ class CreateBidsTable extends Migration Schema::create('bids', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('user_id'); - $table->string('flight_id', \App\Models\Flight::ID_MAX_LENGTH); + $table->string('flight_id', \App\Interfaces\Model::ID_MAX_LENGTH); $table->timestamps(); $table->index('user_id'); diff --git a/app/Database/migrations/2017_12_20_005147_create_acars_tables.php b/app/Database/migrations/2017_12_20_005147_create_acars_tables.php index c285410a..3ac8fc5f 100644 --- a/app/Database/migrations/2017_12_20_005147_create_acars_tables.php +++ b/app/Database/migrations/2017_12_20_005147_create_acars_tables.php @@ -15,7 +15,7 @@ class CreateAcarsTables extends Migration { Schema::create('acars', function (Blueprint $table) { $table->string('id', 12); - $table->string('pirep_id', \App\Models\Pirep::ID_MAX_LENGTH); + $table->string('pirep_id', \App\Interfaces\Model::ID_MAX_LENGTH); $table->unsignedTinyInteger('type'); $table->unsignedInteger('nav_type')->nullable(); $table->unsignedInteger('order')->default(0); diff --git a/app/Database/seeds/sample.yml b/app/Database/seeds/sample.yml index 8683cf29..b82bdb85 100644 --- a/app/Database/seeds/sample.yml +++ b/app/Database/seeds/sample.yml @@ -346,6 +346,12 @@ flights: updated_at: NOW flight_fields: + - name: Departure Gate + slug: departure_gate + - name: Arrival Gate + slug: arrival_gate + +flight_field_values: - id: 1 flight_id: flightid_1 name: cost index diff --git a/app/Http/Controllers/Admin/FlightController.php b/app/Http/Controllers/Admin/FlightController.php index 911a2fd5..d7155fe2 100644 --- a/app/Http/Controllers/Admin/FlightController.php +++ b/app/Http/Controllers/Admin/FlightController.php @@ -7,7 +7,8 @@ use App\Http\Requests\UpdateFlightRequest; use App\Interfaces\Controller; use App\Models\Enums\FlightType; use App\Models\Flight; -use App\Models\FlightFields; +use App\Models\FlightField; +use App\Models\FlightFieldValue; use App\Repositories\AirlineRepository; use App\Repositories\AirportRepository; use App\Repositories\FareRepository; @@ -18,6 +19,7 @@ use App\Services\FlightService; use App\Support\Units\Time; use Flash; use Illuminate\Http\Request; +use Log; use Response; /** @@ -62,6 +64,30 @@ class FlightController extends Controller $this->subfleetRepo = $subfleetRepo; } + /** + * Save any custom fields found + * @param Flight $flight + * @param Request $request + */ + protected function saveCustomFields(Flight $flight, Request $request): void + { + $custom_fields = []; + $flight_fields = FlightField::all(); + foreach ($flight_fields as $field) { + if (!$request->filled($field->slug)) { + continue; + } + + $custom_fields[] = [ + 'name' => $field->name, + 'value' => $request->input($field->slug) + ]; + } + + Log::info('PIREP Custom Fields', $custom_fields); + $this->flightSvc->updateCustomFields($flight->id, $custom_fields); + } + /** * @param $flight * @return array @@ -295,7 +321,7 @@ class FlightController extends Controller * @param Request $request * @return mixed */ - public function fields(Request $request) + public function field_values(Request $request) { $id = $request->id; @@ -308,19 +334,19 @@ class FlightController extends Controller // add custom field to flight if ($request->isMethod('post')) { - $field = new FlightFields; + $field = new FlightFieldValue; $field->flight_id = $id; $field->name = $request->name; $field->value = $request->value; $field->save(); } elseif ($request->isMethod('put')) { - $field = FlightFields::where('id', $request->field_id)->first(); + $field = FlightFieldValue::where('id', $request->field_id)->first(); $field->value = $request->value; $field->save(); // update the field value } // remove custom field from flight elseif ($request->isMethod('delete')) { - FlightFields::destroy($request->field_id); + FlightFieldValue::destroy($request->field_id); } return $this->return_fields_view($flight); diff --git a/app/Http/Controllers/Admin/PirepController.php b/app/Http/Controllers/Admin/PirepController.php index bfc28a62..710e8c77 100644 --- a/app/Http/Controllers/Admin/PirepController.php +++ b/app/Http/Controllers/Admin/PirepController.php @@ -114,7 +114,7 @@ class PirepController extends Controller * @param Pirep $pirep * @param Request $request */ - protected function saveCustomFields(Pirep $pirep, Request $request) + protected function saveCustomFields(Pirep $pirep, Request $request): void { $custom_fields = []; $pirep_fields = $this->pirepFieldRepo->all(); @@ -265,7 +265,6 @@ class PirepController extends Controller if (empty($pirep)) { Flash::error('Pirep not found'); - return redirect(route('admin.pireps.index')); } @@ -358,7 +357,6 @@ class PirepController extends Controller $this->saveFares($pirep, $request); Flash::success('Pirep updated successfully.'); - return redirect(route('admin.pireps.index')); } @@ -373,14 +371,12 @@ class PirepController extends Controller if (empty($pirep)) { Flash::error('Pirep not found'); - return redirect(route('admin.pireps.index')); } $this->pirepRepo->delete($id); Flash::success('Pirep deleted successfully.'); - return redirect(route('admin.pireps.index')); } diff --git a/app/Interfaces/Model.php b/app/Interfaces/Model.php index 5d9e0945..51192d87 100644 --- a/app/Interfaces/Model.php +++ b/app/Interfaces/Model.php @@ -8,5 +8,5 @@ namespace App\Interfaces; */ abstract class Model extends \Illuminate\Database\Eloquent\Model { - + public const ID_MAX_LENGTH = 12; } diff --git a/app/Models/Flight.php b/app/Models/Flight.php index bff915c4..af83ec8e 100644 --- a/app/Models/Flight.php +++ b/app/Models/Flight.php @@ -10,17 +10,17 @@ use PhpUnitsOfMeasure\Exception\NonNumericValue; use PhpUnitsOfMeasure\Exception\NonStringUnitName; /** - * @property Airline airline - * @property mixed flight_number - * @property mixed route_code - * @property mixed route_leg - * @property Collection fields + * @property string id + * @property Airline airline + * @property mixed flight_number + * @property mixed route_code + * @property mixed route_leg + * @property Collection field_values */ class Flight extends Model { use HashIdTrait; - public const ID_MAX_LENGTH = 12; public $table = 'flights'; public $incrementing = false; @@ -130,7 +130,7 @@ class Flight extends Model */ public function field($field_name): string { - $field = $this->fields->where('name', $field_name)->first(); + $field = $this->field_values->where('name', $field_name)->first(); if($field) { return $field['value']; } @@ -168,9 +168,9 @@ class Flight extends Model ->withPivot('price', 'cost', 'capacity'); } - public function fields() + public function field_values() { - return $this->hasMany(FlightFields::class, 'flight_id'); + return $this->hasMany(FlightFieldValue::class, 'flight_id'); } public function subfleets() diff --git a/app/Models/FlightField.php b/app/Models/FlightField.php new file mode 100644 index 00000000..32ffe00c --- /dev/null +++ b/app/Models/FlightField.php @@ -0,0 +1,41 @@ + 'boolean', + ]; + + public static $rules = [ + 'name' => 'required', + ]; + + /** + * When setting the name attribute, also set the slug + * @param $name + */ + public function setNameAttribute($name): void + { + $this->attributes['name'] = $name; + $this->attributes['slug'] = str_slug($name); + } +} diff --git a/app/Models/FlightFields.php b/app/Models/FlightFieldValue.php similarity index 76% rename from app/Models/FlightFields.php rename to app/Models/FlightFieldValue.php index ca69a4ed..64d3a48d 100644 --- a/app/Models/FlightFields.php +++ b/app/Models/FlightFieldValue.php @@ -5,12 +5,12 @@ namespace App\Models; use App\Interfaces\Model; /** - * Class FlightFields + * Class FlightFieldValue * @package App\Models */ -class FlightFields extends Model +class FlightFieldValue extends Model { - public $table = 'flight_fields'; + public $table = 'flight_field_values'; public $fillable = [ 'flight_id', diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index 86c002db..e42aadd4 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -34,8 +34,6 @@ class Pirep extends Model { use HashIdTrait; - public const ID_MAX_LENGTH = 12; - public $table = 'pireps'; public $incrementing = false; diff --git a/app/Models/Traits/HashIdTrait.php b/app/Models/Traits/HashIdTrait.php index 9776eabc..5e898289 100644 --- a/app/Models/Traits/HashIdTrait.php +++ b/app/Models/Traits/HashIdTrait.php @@ -2,6 +2,7 @@ namespace App\Models\Traits; +use App\Interfaces\Model; use Hashids\Hashids; trait HashIdTrait @@ -10,11 +11,10 @@ trait HashIdTrait * @return string * @throws \Hashids\HashidsException */ - protected static function createNewHashId(): string + final protected static function createNewHashId(): string { - $hashids = new Hashids('', 12); + $hashids = new Hashids('', Model::ID_MAX_LENGTH); $mt = str_replace('.', '', microtime(true)); - return $hashids->encode($mt); } @@ -22,7 +22,7 @@ trait HashIdTrait * Register callbacks * @throws \Hashids\HashidsException */ - protected static function bootHashIdTrait() + final protected static function bootHashIdTrait(): void { static::creating(function ($model) { if (empty($model->id)) { diff --git a/app/Routes/admin.php b/app/Routes/admin.php index 21b8e23b..1fcb2968 100644 --- a/app/Routes/admin.php +++ b/app/Routes/admin.php @@ -32,7 +32,7 @@ Route::group([ # flights and aircraft associations Route::resource('flights', 'FlightController'); Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/fares', 'FlightController@fares'); - Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/fields', 'FlightController@fields'); + Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/fields', 'FlightController@field_values'); Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/subfleets', 'FlightController@subfleets'); # pirep related routes diff --git a/app/Services/FlightService.php b/app/Services/FlightService.php index 6cc5e22a..dbf9431a 100644 --- a/app/Services/FlightService.php +++ b/app/Services/FlightService.php @@ -6,6 +6,7 @@ use App\Exceptions\BidExists; use App\Interfaces\Service; use App\Models\Bid; use App\Models\Flight; +use App\Models\FlightFieldValue; use App\Models\User; use App\Repositories\FlightRepository; use App\Repositories\NavdataRepository; @@ -104,13 +105,33 @@ class FlightService extends Service * @param Flight $flight * @throws \Exception */ - public function deleteFlight(Flight $flight) + public function deleteFlight(Flight $flight): void { $where = ['flight_id' => $flight->id]; Bid::where($where)->delete(); $flight->delete(); } + /** + * Update any custom PIREP fields + * @param Flight $flight_id + * @param array $field_values + */ + public function updateCustomFields(Flight $flight_id, array $field_values): void + { + foreach ($field_values as $fv) { + FlightFieldValue::updateOrCreate( + [ + 'flight_id' => $flight_id, + 'name' => $fv['name'], + ], + [ + 'value' => $fv['value'] + ] + ); + } + } + /** * Return all of the navaid points as a collection * @param Flight $flight diff --git a/resources/views/admin/flights/flight_fields.blade.php b/resources/views/admin/flights/flight_fields.blade.php index f0dcb73a..e59c98ce 100644 --- a/resources/views/admin/flights/flight_fields.blade.php +++ b/resources/views/admin/flights/flight_fields.blade.php @@ -1,15 +1,15 @@

custom fields


- @if(count($flight->fields)) + @if(count($flight->field_values)) - - + + @endif - @foreach($flight->fields as $field) + @foreach($flight->field_values as $field)
NameValueActionsValue
{{ $field->name }} diff --git a/resources/views/admin/flights/scripts.blade.php b/resources/views/admin/flights/scripts.blade.php index 36a8b2ed..4cef09e6 100644 --- a/resources/views/admin/flights/scripts.blade.php +++ b/resources/views/admin/flights/scripts.blade.php @@ -62,11 +62,13 @@ $(document).ready(function () { $(document).on('submit', 'form.pjax_flight_fields', function (event) { event.preventDefault(); $.pjax.submit(event, '#flight_fields_wrapper', {push: false}); + setEditable(); }); $(document).on('submit', 'form.pjax_subfleet_form', function (event) { event.preventDefault(); $.pjax.submit(event, '#subfleet_flight_wrapper', {push: false}); + setEditable(); }); $(document).on('submit', 'form.pjax_fares_form', function (event) {