From c3e417429ab0e746c9b63ee2e15917a4359cc2b3 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Fri, 23 Jun 2017 17:33:18 -0500 Subject: [PATCH] #32 assign aircraft to subfleets, and aircraft to subfleets --- .../Controllers/Admin/AircraftController.php | 7 ++-- .../Controllers/Admin/FlightController.php | 39 +++++++++---------- .../Requests/CreateAircraftClassRequest.php | 30 -------------- app/Http/Requests/CreateAirportRequest.php | 4 +- app/Http/Requests/CreateFlightRequest.php | 4 +- app/Http/Requests/CreateRankRequest.php | 4 +- app/Http/Requests/CreateSubfleetRequest.php | 4 +- .../Requests/UpdateAircraftClassRequest.php | 30 -------------- app/Http/Requests/UpdateAirportRequest.php | 4 +- app/Http/Requests/UpdateFlightRequest.php | 4 +- app/Http/Requests/UpdateRankRequest.php | 4 +- app/Http/Requests/UpdateSubfleetRequest.php | 4 +- app/Models/Aircraft.php | 2 +- app/Models/Flight.php | 4 +- app/Models/Subfleet.php | 5 +++ app/Services/AircraftService.php | 2 +- app/Services/PIREPService.php | 4 +- ...2017_06_17_214650_create_flights_table.php | 9 ----- ...17_06_23_011011_create_subfleets_table.php | 13 ++++++- database/seeds/dev.yml | 18 +++++++-- .../views/admin/aircraft/fields.blade.php | 5 +++ .../admin/aircraft/show_fields.blade.php | 5 ++- .../views/admin/aircraft/table.blade.php | 8 ++++ resources/views/admin/flights/show.blade.php | 8 ++-- ...aircraft.blade.php => subfleets.blade.php} | 24 ++++++------ routes/admin.php | 2 +- tests/AircraftTest.php | 24 +++--------- 27 files changed, 113 insertions(+), 158 deletions(-) delete mode 100644 app/Http/Requests/CreateAircraftClassRequest.php delete mode 100644 app/Http/Requests/UpdateAircraftClassRequest.php rename resources/views/admin/flights/{aircraft.blade.php => subfleets.blade.php} (70%) diff --git a/app/Http/Controllers/Admin/AircraftController.php b/app/Http/Controllers/Admin/AircraftController.php index 5752f7b0..9d55492e 100644 --- a/app/Http/Controllers/Admin/AircraftController.php +++ b/app/Http/Controllers/Admin/AircraftController.php @@ -5,7 +5,7 @@ namespace App\Http\Controllers\Admin; use App\Models\Subfleet; use App\Http\Requests\CreateAircraftRequest; use App\Http\Requests\UpdateAircraftRequest; -use App\Repositories\AircraftRepository; +use App\Repositories\SubfleetRepository; use App\Repositories\FareRepository; use Illuminate\Http\Request; use Flash; @@ -14,7 +14,7 @@ use Response; class AircraftController extends BaseController { - /** @var AircraftRepository */ + /** @var SubfleetRepository */ private $aircraftRepository, $fareRepository; protected function getAvailFares($aircraft) @@ -32,7 +32,7 @@ class AircraftController extends BaseController return $retval; } - public function __construct(AircraftRepository $aircraftRepo, FareRepository $fareRepo) + public function __construct(SubfleetRepository $aircraftRepo, FareRepository $fareRepo) { $this->fareRepository = $fareRepo; $this->aircraftRepository = $aircraftRepo; @@ -126,7 +126,6 @@ class AircraftController extends BaseController $aircraft = $this->aircraftRepository->update($request->all(), $id); Flash::success('Aircraft updated successfully.'); - return redirect(route('admin.aircraft.index')); } diff --git a/app/Http/Controllers/Admin/FlightController.php b/app/Http/Controllers/Admin/FlightController.php index 3eb4322c..d9b52404 100644 --- a/app/Http/Controllers/Admin/FlightController.php +++ b/app/Http/Controllers/Admin/FlightController.php @@ -5,7 +5,7 @@ namespace App\Http\Controllers\Admin; use App\Http\Requests\CreateFlightRequest; use App\Http\Requests\UpdateFlightRequest; use App\Repositories\FlightRepository; -use App\Repositories\AircraftRepository; +use App\Repositories\SubfleetRepository; use Illuminate\Http\Request; use Flash; use Prettus\Repository\Criteria\RequestCriteria; @@ -14,27 +14,27 @@ use Response; class FlightController extends BaseController { /** @var FlightRepository */ - private $flightRepository, $aircraftRepository; + private $flightRepository, $subfleetRepo; public function __construct( FlightRepository $flightRepo, - AircraftRepository $aircraftRepository + SubfleetRepository $subfleetRepo ) { $this->flightRepository = $flightRepo; - $this->aircraftRepository = $aircraftRepository; + $this->subfleetRepo = $subfleetRepo; } - protected function getAvailAircraft($flight) + protected function getAvailSubfleets($flight) { $retval = []; $flight->refresh(); - $all_aircraft = $this->aircraftRepository->all(); - $avail_aircraft = $all_aircraft->except($flight->aircraft->modelKeys()); + $all_aircraft = $this->subfleetRepo->all(); + $avail_fleets = $all_aircraft->except($flight->subfleets->modelKeys()); - foreach ($avail_aircraft as $ac) { - $retval[$ac->id] = $ac->icao.' - '.$ac->registration; + foreach ($avail_fleets as $ac) { + $retval[$ac->id] = $ac->type.' - '.$ac->name; } return $retval; @@ -98,10 +98,10 @@ class FlightController extends BaseController return redirect(route('admin.flights.index')); } - $avail_aircraft = $this->getAvailAircraft($flight); + $avail_subfleets = $this->getAvailSubfleets($flight); return view('admin.flights.show') ->with('flight', $flight) - ->with('avail_aircraft', $avail_aircraft); + ->with('avail_subfleets', $avail_subfleets); } /** @@ -143,7 +143,6 @@ class FlightController extends BaseController $flight = $this->flightRepository->update($request->all(), $id); Flash::success('Flight updated successfully.'); - return redirect(route('admin.flights.index')); } @@ -169,15 +168,15 @@ class FlightController extends BaseController return redirect(route('admin.flights.index')); } - protected function return_aircraft_view($flight) + protected function return_subfleet_view($flight) { - $avail_aircraft = $this->getAvailAircraft($flight); - return view('admin.flights.aircraft') + $avail_subfleets = $this->getAvailSubfleets($flight); + return view('admin.flights.subfleets') ->with('flight', $flight) - ->with('avail_aircraft', $avail_aircraft); + ->with('avail_subfleets', $avail_subfleets); } - public function aircraft(Request $request) + public function subfleets(Request $request) { $id = $request->id; @@ -189,14 +188,14 @@ class FlightController extends BaseController // add aircraft to flight if ($request->isMethod('post')) { - $flight->aircraft()->syncWithoutDetaching([$request->aircraft_id]); + $flight->subfleets()->syncWithoutDetaching([$request->subfleet_id]); } // remove aircraft from flight elseif ($request->isMethod('delete')) { - $flight->aircraft()->detach($request->aircraft_id); + $flight->subfleets()->detach($request->subfleet_id); } - return $this->return_aircraft_view($flight); + return $this->return_subfleet_view($flight); } } diff --git a/app/Http/Requests/CreateAircraftClassRequest.php b/app/Http/Requests/CreateAircraftClassRequest.php deleted file mode 100644 index 4ca0a397..00000000 --- a/app/Http/Requests/CreateAircraftClassRequest.php +++ /dev/null @@ -1,30 +0,0 @@ -belongsTo('App\Models\Airport', 'alt_airport_id'); } - public function aircraft() + public function subfleets() { - return $this->belongsToMany('App\Models\Aircraft', 'flight_aircraft'); + return $this->belongsToMany('App\Models\Subfleet', 'subfleet_flight'); } } diff --git a/app/Models/Subfleet.php b/app/Models/Subfleet.php index e291ce3d..fa59fcf1 100644 --- a/app/Models/Subfleet.php +++ b/app/Models/Subfleet.php @@ -50,6 +50,11 @@ class Subfleet extends Model return $this->belongsTo('App\Models\Airline', 'airline_id'); } + public function flights() + { + return $this->belongsToMany('App\Models\Flight', 'subfleet_flight'); + } + public function ranks() { return $this->belongsToMany( diff --git a/app/Services/AircraftService.php b/app/Services/AircraftService.php index 312c5d02..0a03510c 100644 --- a/app/Services/AircraftService.php +++ b/app/Services/AircraftService.php @@ -13,7 +13,7 @@ class AircraftService extends BaseService AircraftClass $class = null ) { - $repo = app('App\Repositories\AircraftRepository'); + $repo = app('App\Repositories\SubfleetRepository'); try { $model = $repo->create($attributes); } catch (Exception $e) { diff --git a/app/Services/PIREPService.php b/app/Services/PIREPService.php index 540e2a11..e6bd38d9 100644 --- a/app/Services/PIREPService.php +++ b/app/Services/PIREPService.php @@ -2,7 +2,7 @@ namespace App\Services; -use App\Repositories\AircraftRepository; +use App\Repositories\SubfleetRepository; class PIREPService extends BaseService { @@ -13,7 +13,7 @@ class PIREPService extends BaseService { * return a PIREP model */ public function __construct( - AircraftRepository $aircraft + SubfleetRepository $aircraft ) { $this->aircraft = $aircraft; } diff --git a/database/migrations/2017_06_17_214650_create_flights_table.php b/database/migrations/2017_06_17_214650_create_flights_table.php index 0b708531..59ff10f6 100644 --- a/database/migrations/2017_06_17_214650_create_flights_table.php +++ b/database/migrations/2017_06_17_214650_create_flights_table.php @@ -37,14 +37,6 @@ class CreateFlightsTable extends Migration $table->index('dpt_airport_id'); $table->index('arr_airport_id'); }); - - Schema::create('flight_aircraft', function ($table) { - $table->uuid('flight_id'); - $table->integer('aircraft_id')->unsigned(); - - $table->primary(['flight_id', 'aircraft_id']); - $table->index(['aircraft_id', 'flight_id']); - }); } /** @@ -55,6 +47,5 @@ class CreateFlightsTable extends Migration public function down() { Schema::drop('flights'); - Schema::drop('flight_aircraft'); } } diff --git a/database/migrations/2017_06_23_011011_create_subfleets_table.php b/database/migrations/2017_06_23_011011_create_subfleets_table.php index 1b5a39fb..7ddf1f29 100644 --- a/database/migrations/2017_06_23_011011_create_subfleets_table.php +++ b/database/migrations/2017_06_23_011011_create_subfleets_table.php @@ -23,14 +23,22 @@ class CreateSubfleetsTable extends Migration }); Schema::create('subfleet_rank', function(Blueprint $table) { - $table->integer('rank_id')->unsigned()->nullable(); - $table->integer('subfleet_id')->unsigned()->nullable(); + $table->integer('rank_id')->unsigned(); + $table->integer('subfleet_id')->unsigned(); $table->double('acars_pay', 19, 2)->unsigned()->nullable(); $table->double('manual_pay', 19, 2)->unsigned()->nullable(); $table->primary(['rank_id', 'subfleet_id']); $table->index(['subfleet_id', 'rank_id']); }); + + Schema::create('subfleet_flight', function(Blueprint $table) { + $table->integer('subfleet_id')->unsigned(); + $table->integer('flight_id')->unsigned(); + + $table->primary(['subfleet_id', 'flight_id']); + $table->index(['flight_id', 'subfleet_id']); + }); } /** @@ -42,5 +50,6 @@ class CreateSubfleetsTable extends Migration { Schema::drop('subfleets'); Schema::drop('subfleet_rank'); + Schema::drop('subfleet_flight'); } } diff --git a/database/seeds/dev.yml b/database/seeds/dev.yml index 14c92581..41714388 100644 --- a/database/seeds/dev.yml +++ b/database/seeds/dev.yml @@ -73,11 +73,13 @@ airports: # aircraft: - id: 1 + subfleet_id: 1 icao: B744 name: Boeing 747-400 registration: NC17 tail_number: 17 - id: 2 + subfleet_id: 2 icao: B772 name: Boeing 777-200 registration: NC20 @@ -139,6 +141,16 @@ flights: arr_airport_id: 2 route: KAUS KJFK -flight_aircraft: - - flight_id: 1 - aircraft_id: 1 +subfleets: + - id: 1 + airline_id: 1 + name: 747-400 Winglets + type: 744W + - id: 2 + airline_id: 1 + name: 777-200 LR + type: 772-LR + +subfleet_flight: + - subfleet_id: 1 + flight_id: 1 diff --git a/resources/views/admin/aircraft/fields.blade.php b/resources/views/admin/aircraft/fields.blade.php index 411dcebe..7759b48c 100644 --- a/resources/views/admin/aircraft/fields.blade.php +++ b/resources/views/admin/aircraft/fields.blade.php @@ -10,6 +10,11 @@ {!! Form::text('name', null, ['class' => 'form-control']) !!} +
+ {!! Form::label('subfleet_id', 'Subfleet:') !!} + {!! Form::select('subfleet_id', $subfleets, null, ['class' => 'form-control', 'placeholder' => 'Select Subfleet']) !!} +
+
{!! Form::label('registration', 'Registration:') !!} diff --git a/resources/views/admin/aircraft/show_fields.blade.php b/resources/views/admin/aircraft/show_fields.blade.php index eed4765f..579605c3 100644 --- a/resources/views/admin/aircraft/show_fields.blade.php +++ b/resources/views/admin/aircraft/show_fields.blade.php @@ -6,7 +6,10 @@

{!! Form::label('icao', 'ICAO') !!}

-

{!! $aircraft->icao !!}/{!! $aircraft->class->code !!} ({!! $aircraft->class->name !!})

+

{!! $aircraft->icao !!} + @if($aircraft->subfleet) + /{!! $aircraft->subfleet->name !!}

+ @endif
diff --git a/resources/views/admin/aircraft/table.blade.php b/resources/views/admin/aircraft/table.blade.php index 9a9a2796..e94fdbee 100644 --- a/resources/views/admin/aircraft/table.blade.php +++ b/resources/views/admin/aircraft/table.blade.php @@ -2,6 +2,7 @@ ICAO Name + Subfleet Registration Active Actions @@ -11,6 +12,13 @@ {!! $ac->icao !!} {!! $ac->name !!} + + @if($ac->subfleet) + {!! $ac->subfleet->name !!} + @else + - + @endif + {!! $ac->registration !!}