#32 assign aircraft to subfleets, and aircraft to subfleets

This commit is contained in:
Nabeel Shahzad 2017-06-23 17:33:18 -05:00
parent b9d49a4af9
commit c3e417429a
27 changed files with 113 additions and 158 deletions

View File

@ -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'));
}

View File

@ -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);
}
}

View File

@ -1,30 +0,0 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\Models\AircraftClass;
class CreateAircraftClassRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return AircraftClass::$rules;
}
}

View File

@ -2,10 +2,10 @@
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\Models\Airport;
use Illuminate\Foundation\Http\FormRequest;
class CreateAirportRequest extends Request
class CreateAirportRequest extends FormRequest
{
/**

View File

@ -2,10 +2,10 @@
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\Models\Flight;
use Illuminate\Foundation\Http\FormRequest;
class CreateFlightRequest extends Request
class CreateFlightRequest extends FormRequest
{
/**

View File

@ -2,10 +2,10 @@
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\Models\Rank;
use Illuminate\Foundation\Http\FormRequest;
class CreateRankRequest extends Request
class CreateRankRequest extends FormRequest
{
/**

View File

@ -2,10 +2,10 @@
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\Models\Subfleet;
use Illuminate\Foundation\Http\FormRequest;
class CreateSubfleetRequest extends Request
class CreateSubfleetRequest extends FormRequest
{
/**

View File

@ -1,30 +0,0 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\Models\AircraftClass;
class UpdateAircraftClassRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return AircraftClass::$rules;
}
}

View File

@ -2,10 +2,10 @@
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\Models\Airport;
use Illuminate\Foundation\Http\FormRequest;
class UpdateAirportRequest extends Request
class UpdateAirportRequest extends FormRequest
{
/**

View File

@ -2,10 +2,10 @@
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\Models\Flight;
use Illuminate\Foundation\Http\FormRequest;
class UpdateFlightRequest extends Request
class UpdateFlightRequest extends FormRequest
{
/**

View File

@ -2,10 +2,10 @@
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\Models\Rank;
use Illuminate\Foundation\Http\FormRequest;
class UpdateRankRequest extends Request
class UpdateRankRequest extends FormRequest
{
/**

View File

@ -2,10 +2,10 @@
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\Models\Subfleet;
use Illuminate\Foundation\Http\FormRequest;
class UpdateSubfleetRequest extends Request
class UpdateSubfleetRequest extends FormRequest
{
/**

View File

@ -18,7 +18,7 @@ class Aircraft extends Model
public $fillable
= [
'aircraft_class_id',
'subfleet_id',
'icao',
'name',
'registration',

View File

@ -81,8 +81,8 @@ class Flight extends Model
return $this->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');
}
}

View File

@ -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(

View File

@ -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) {

View File

@ -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;
}

View File

@ -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');
}
}

View File

@ -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');
}
}

View File

@ -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

View File

@ -10,6 +10,11 @@
{!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group col-sm-6">
{!! Form::label('subfleet_id', 'Subfleet:') !!}
{!! Form::select('subfleet_id', $subfleets, null, ['class' => 'form-control', 'placeholder' => 'Select Subfleet']) !!}
</div>
<!-- Registration Field -->
<div class="form-group col-sm-6">
{!! Form::label('registration', 'Registration:') !!}

View File

@ -6,7 +6,10 @@
<h3 class="box-title">{!! Form::label('icao', 'ICAO') !!}</h3>
</div>
<div class="box-body">
<p class="lead">{!! $aircraft->icao !!}/<strong>{!! $aircraft->class->code !!}</strong> ({!! $aircraft->class->name !!})</p>
<p class="lead">{!! $aircraft->icao !!}
@if($aircraft->subfleet)
/{!! $aircraft->subfleet->name !!}</p>
@endif
</div>
</div>
</div>

View File

@ -2,6 +2,7 @@
<thead>
<th>ICAO</th>
<th>Name</th>
<th>Subfleet</th>
<th>Registration</th>
<th style="text-align: center;">Active</th>
<th style="text-align: center;">Actions</th>
@ -11,6 +12,13 @@
<tr>
<td><a href="{!! route('admin.aircraft.show', [$ac->id]) !!}">{!! $ac->icao !!}</a></td>
<td>{!! $ac->name !!}</td>
<td>
@if($ac->subfleet)
{!! $ac->subfleet->name !!}
@else
-
@endif
</td>
<td>{!! $ac->registration !!}</td>
<td style="text-align: center;">
<i class="fa fa-{{$ac->active == 1?"check":""}}-square-o" aria-hidden="true"

View File

@ -17,9 +17,9 @@
<div class="box-body">
<div class="row">
<div class="col-xs-12">
<h3>assigned aircraft</h3>
<h3>assigned subfleets</h3>
<div class="box-body">
@include('admin.flights.aircraft')
@include('admin.flights.subfleets')
</div>
</div>
</div>
@ -32,9 +32,9 @@
$(document).ready(function () {
$(".ac-flight-dropdown").select2();
$(document).on('submit', 'form.flight_ac_frm', function (event) {
$(document).on('submit', 'form.flight_subfleet', function (event) {
event.preventDefault();
$.pjax.submit(event, '#flight_aircraft_wrapper', {push: false});
$.pjax.submit(event, '#subfleet_flight_wrapper', {push: false});
});
});
</script>

View File

@ -1,20 +1,18 @@
<div id="flight_aircraft_wrapper" >
<div id="subfleet_flight_wrapper">
<table class="table table-responsive" id="aircrafts-table">
<thead>
<th>ICAO</th>
<th>Type</th>
<th>Name</th>
<th>Registration</th>
<th style="text-align: center;">Actions</th>
</thead>
<tbody>
@foreach($flight->aircraft as $ac)
@foreach($flight->subfleets as $sf)
<tr>
<td>{!! $ac->icao !!}</td>
<td>{!! $ac->name !!}</td>
<td>{!! $ac->registration !!}</td>
<td>{!! $sf->type !!}</td>
<td>{!! $sf->name !!}</td>
<td style="width: 10%; text-align: center;" class="form-inline">
{!! Form::open(['url' => '/admin/flights/'.$flight->id.'/aircraft', 'method' => 'delete', 'class' => 'flight_ac_frm']) !!}
{!! Form::hidden('aircraft_id', $flight->id) !!}
{!! Form::open(['url' => '/admin/flights/'.$flight->id.'/subfleets', 'method' => 'delete', 'class' => 'flight_subfleet']) !!}
{!! Form::hidden('subfleet_id', $sf->id) !!}
<div class='btn-group'>
{!! Form::button('<i class="glyphicon glyphicon-trash"></i>',
['type' => 'submit',
@ -31,13 +29,13 @@
<div class="row">
<div class="col-xs-12">
<div class="input-group input-group-lg pull-right">
{!! Form::open(['url' => '/admin/flights/'.$flight->id.'/aircraft',
{!! Form::open(['url' => '/admin/flights/'.$flight->id.'/subfleets',
'method' => 'post',
'class' => 'flight_ac_frm form-inline'
'class' => 'flight_subfleet form-inline'
])
!!}
{!! Form::select('aircraft_id', $avail_aircraft, null, [
'placeholder' => 'Select Aircraft',
{!! Form::select('subfleet_id', $avail_subfleets, null, [
'placeholder' => 'Select Subfleet',
'class' => 'ac-flight-dropdown form-control input-lg',
])
!!}

View File

@ -21,7 +21,7 @@ Route::group([
# flights and aircraft associations
Route::resource('flights', 'FlightController');
Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/aircraft', 'FlightController@aircraft');
Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/subfleets', 'FlightController@subfleets');
# rankings
Route::resource('ranks', 'RankController');

View File

@ -20,7 +20,7 @@ class AircraftTest extends TestCase
protected function findByICAO($icao)
{
$ac_repo = app('App\Repositories\AircraftRepository');
$ac_repo = app('App\Repositories\SubfleetRepository');
return $ac_repo->findByICAO($icao);
}
@ -36,28 +36,14 @@ class AircraftTest extends TestCase
*/
protected function addAircraft()
{
$svc = app('App\Services\AircraftService');
$err = $svc->create([
'icao' => $this->ICAO,
'name' => 'Boeing 777',
], $this->getAircraftClass());
$this->assertNotFalse($err);
$mdl = new App\Models\Aircraft;
$mdl->icao = $this->ICAO;
$mdl->name = 'Boeing 777';
$mdl->save();
return $this->findByICAO($this->ICAO);
}
public function testAircraftClasses()
{
$aircraft = $this->addAircraft();
$this->assertEquals($this->ICAO, $aircraft->icao, 'ICAO matching');
$this->assertEquals(
$this->getAircraftClass(),
$aircraft->class,
'Check belongsTo relationship'
);
}
public function testAircraftFaresNoOverride()
{
$fare_svc = app('App\Services\FareService');