Skip to content

Commit

Permalink
#32 assign aircraft to subfleets, and aircraft to subfleets
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Jun 23, 2017
1 parent b9d49a4 commit c3e4174
Show file tree
Hide file tree
Showing 27 changed files with 113 additions and 158 deletions.
7 changes: 3 additions & 4 deletions app/Http/Controllers/Admin/AircraftController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand All @@ -14,7 +14,7 @@

class AircraftController extends BaseController
{
/** @var AircraftRepository */
/** @var SubfleetRepository */
private $aircraftRepository, $fareRepository;

protected function getAvailFares($aircraft)
Expand All @@ -32,7 +32,7 @@ protected function getAvailFares($aircraft)
return $retval;
}

public function __construct(AircraftRepository $aircraftRepo, FareRepository $fareRepo)
public function __construct(SubfleetRepository $aircraftRepo, FareRepository $fareRepo)
{
$this->fareRepository = $fareRepo;
$this->aircraftRepository = $aircraftRepo;
Expand Down Expand Up @@ -126,7 +126,6 @@ public function update($id, UpdateAircraftRequest $request)
$aircraft = $this->aircraftRepository->update($request->all(), $id);

Flash::success('Aircraft updated successfully.');

return redirect(route('admin.aircraft.index'));
}

Expand Down
39 changes: 19 additions & 20 deletions app/Http/Controllers/Admin/FlightController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand All @@ -14,27 +14,27 @@
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;
Expand Down Expand Up @@ -98,10 +98,10 @@ public function show($id)
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);
}

/**
Expand Down Expand Up @@ -143,7 +143,6 @@ public function update($id, UpdateFlightRequest $request)
$flight = $this->flightRepository->update($request->all(), $id);

Flash::success('Flight updated successfully.');

return redirect(route('admin.flights.index'));
}

Expand All @@ -169,15 +168,15 @@ public function destroy($id)
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;

Expand All @@ -189,14 +188,14 @@ public function aircraft(Request $request)

// 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);
}
}
30 changes: 0 additions & 30 deletions app/Http/Requests/CreateAircraftClassRequest.php

This file was deleted.

4 changes: 2 additions & 2 deletions app/Http/Requests/CreateAirportRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/CreateFlightRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/CreateRankRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/CreateSubfleetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand Down
30 changes: 0 additions & 30 deletions app/Http/Requests/UpdateAircraftClassRequest.php

This file was deleted.

4 changes: 2 additions & 2 deletions app/Http/Requests/UpdateAirportRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/UpdateFlightRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/UpdateRankRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/UpdateSubfleetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Aircraft.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Aircraft extends Model

public $fillable
= [
'aircraft_class_id',
'subfleet_id',
'icao',
'name',
'registration',
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Flight.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public function alt_airport()
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');
}
}
5 changes: 5 additions & 0 deletions app/Models/Subfleet.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function airline()
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(
Expand Down
2 changes: 1 addition & 1 deletion app/Services/AircraftService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function create(
AircraftClass $class = null
) {

$repo = app('App\Repositories\AircraftRepository');
$repo = app('App\Repositories\SubfleetRepository');
try {
$model = $repo->create($attributes);
} catch (Exception $e) {
Expand Down
4 changes: 2 additions & 2 deletions app/Services/PIREPService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Services;

use App\Repositories\AircraftRepository;
use App\Repositories\SubfleetRepository;


class PIREPService extends BaseService {
Expand All @@ -13,7 +13,7 @@ class PIREPService extends BaseService {
* return a PIREP model
*/
public function __construct(
AircraftRepository $aircraft
SubfleetRepository $aircraft
) {
$this->aircraft = $aircraft;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ public function up()
$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']);
});
}

/**
Expand All @@ -55,6 +47,5 @@ public function up()
public function down()
{
Schema::drop('flights');
Schema::drop('flight_aircraft');
}
}
Loading

0 comments on commit c3e4174

Please sign in to comment.