Cleanup and move subfleet/rank settings to FleetService class

This commit is contained in:
Nabeel Shahzad 2018-02-27 14:12:03 -06:00
parent f0977b48e5
commit 3aa4b8a410
5 changed files with 79 additions and 19 deletions

View File

@ -6,6 +6,7 @@ use App\Http\Requests\CreateRankRequest;
use App\Http\Requests\UpdateRankRequest; use App\Http\Requests\UpdateRankRequest;
use App\Repositories\RankRepository; use App\Repositories\RankRepository;
use App\Repositories\SubfleetRepository; use App\Repositories\SubfleetRepository;
use App\Services\FleetService;
use Cache; use Cache;
use Flash; use Flash;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -15,17 +16,22 @@ use Response;
class RankController extends BaseController class RankController extends BaseController
{ {
/** @var RankRepository */ /** @var RankRepository */
private $rankRepository, $subfleetRepo; private $fleetSvc,
$rankRepository,
$subfleetRepo;
/** /**
* RankController constructor. * RankController constructor.
* @param FleetService $fleetSvc
* @param RankRepository $rankingRepo * @param RankRepository $rankingRepo
* @param SubfleetRepository $subfleetRepo * @param SubfleetRepository $subfleetRepo
*/ */
public function __construct( public function __construct(
FleetService $fleetSvc,
RankRepository $rankingRepo, RankRepository $rankingRepo,
SubfleetRepository $subfleetRepo SubfleetRepository $subfleetRepo
) { ) {
$this->fleetSvc = $fleetSvc;
$this->rankRepository = $rankingRepo; $this->rankRepository = $rankingRepo;
$this->subfleetRepo = $subfleetRepo; $this->subfleetRepo = $subfleetRepo;
} }
@ -204,19 +210,22 @@ class RankController extends BaseController
// add aircraft to flight // add aircraft to flight
if ($request->isMethod('post')) { if ($request->isMethod('post')) {
$rank->subfleets()->syncWithoutDetaching([$request->subfleet_id]); $subfleet = $this->subfleetRepo->find($request->input('subfleet_id'));
$this->fleetSvc->addSubfleetToRank($subfleet, $rank);
} }
elseif($request->isMethod('put')) { elseif($request->isMethod('put')) {
$override = []; $override = [];
$subfleet = $this->subfleetRepo->find($request->input('subfleet_id'));
$override[$request->name] = $request->value; $override[$request->name] = $request->value;
$rank->subfleets()->updateExistingPivot($subfleet->id, $override); $subfleet = $this->subfleetRepo->find($request->input('subfleet_id'));
$this->fleetSvc->addSubfleetToRank($subfleet, $rank);
} }
// remove aircraft from flight // remove aircraft from flight
elseif ($request->isMethod('delete')) { elseif ($request->isMethod('delete')) {
$rank->subfleets()->detach($request->subfleet_id); $subfleet = $this->subfleetRepo->find($request->input('subfleet_id'));
$this->fleetSvc->removeSubfleetFromRank($subfleet, $rank);
} }
return $this->return_subfleet_view($rank); return $this->return_subfleet_view($rank);

View File

@ -12,6 +12,7 @@ use App\Repositories\FareRepository;
use App\Repositories\RankRepository; use App\Repositories\RankRepository;
use App\Repositories\SubfleetRepository; use App\Repositories\SubfleetRepository;
use App\Services\FareService; use App\Services\FareService;
use App\Services\FleetService;
use Flash; use Flash;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Prettus\Repository\Criteria\RequestCriteria; use Prettus\Repository\Criteria\RequestCriteria;
@ -20,27 +21,36 @@ use Response;
class SubfleetController extends BaseController class SubfleetController extends BaseController
{ {
/** @var SubfleetRepository */ /** @var SubfleetRepository */
private $aircraftRepo, $rankRepo, $subfleetRepo, $fareRepo, $fareSvc; private $aircraftRepo,
$fareRepo,
$fareSvc,
$fleetSvc,
$rankRepo,
$subfleetRepo;
/** /**
* SubfleetController constructor. * SubfleetController constructor.
* @param AircraftRepository $aircraftRepo * @param AircraftRepository $aircraftRepo
* @param FleetService $fleetSvc
* @param RankRepository $rankRepo
* @param SubfleetRepository $subfleetRepo * @param SubfleetRepository $subfleetRepo
* @param FareRepository $fareRepo * @param FareRepository $fareRepo
* @param FareService $fareSvc * @param FareService $fareSvc
*/ */
public function __construct( public function __construct(
AircraftRepository $aircraftRepo, AircraftRepository $aircraftRepo,
FleetService $fleetSvc,
RankRepository $rankRepo, RankRepository $rankRepo,
SubfleetRepository $subfleetRepo, SubfleetRepository $subfleetRepo,
FareRepository $fareRepo, FareRepository $fareRepo,
FareService $fareSvc FareService $fareSvc
) { ) {
$this->aircraftRepo = $aircraftRepo; $this->aircraftRepo = $aircraftRepo;
$this->rankRepo = $rankRepo;
$this->subfleetRepo = $subfleetRepo;
$this->fareRepo = $fareRepo; $this->fareRepo = $fareRepo;
$this->fareSvc = $fareSvc; $this->fareSvc = $fareSvc;
$this->fleetSvc = $fleetSvc;
$this->rankRepo = $rankRepo;
$this->subfleetRepo = $subfleetRepo;
} }
/** /**
@ -271,19 +281,22 @@ class SubfleetController extends BaseController
* update specific rank data * update specific rank data
*/ */
if ($request->isMethod('post')) { if ($request->isMethod('post')) {
$subfleet->ranks()->syncWithoutDetaching([$request->input('rank_id')]); $rank = $this->fareRepo->find($request->input('rank_id'));
$this->fleetSvc->addSubfleetToRank($subfleet, $rank);
} }
elseif ($request->isMethod('put')) { elseif ($request->isMethod('put')) {
$override = []; $override = [];
$rank = $this->fareRepo->find($request->input('rank_id')); $rank = $this->fareRepo->find($request->input('rank_id'));
$override[$request->name] = $request->value; $override[$request->name] = $request->value;
$subfleet->ranks()->updateExistingPivot($rank->id, $override);
$this->fleetSvc->addSubfleetToRank($subfleet, $rank, $override);
} }
// dissassociate fare from teh aircraft // dissassociate fare from teh aircraft
elseif ($request->isMethod('delete')) { elseif ($request->isMethod('delete')) {
$subfleet->ranks()->detach($request->input('rank_id')); $rank = $this->fareRepo->find($request->input('rank_id'));
$this->fleetSvc->removeSubfleetFromRank($subfleet, $rank);
} }
$subfleet->save(); $subfleet->save();

View File

@ -1,8 +0,0 @@
<?php
namespace App\Services;
class AircraftService extends BaseService
{
}

View File

@ -0,0 +1,37 @@
<?php
namespace App\Services;
use App\Models\Rank;
use App\Models\Subfleet;
class FleetService extends BaseService
{
/**
* @param Subfleet $subfleet
* @param Rank $rank
* @param array $overrides
* @return Subfleet
*/
public function addSubfleetToRank(Subfleet $subfleet, Rank $rank, array $overrides=[])
{
$subfleet->ranks()->syncWithoutDetaching([$rank->id]);
if($overrides) {
$subfleet->ranks()->updateExistingPivot($rank->id, $overrides);
}
$subfleet->save();
$subfleet->refresh();
return $subfleet;
}
public function removeSubfleetFromRank(Subfleet $subfleet, Rank $rank)
{
$subfleet->ranks()->detach($rank->id);
$subfleet->save();
$subfleet->refresh();
return $subfleet;
}
}

View File

@ -270,4 +270,13 @@ class FinanceTest extends TestCase
} }
} }
} }
/**
* Get the pilot pay, derived from the rank, and then if there
* are any overrides from a PIREP
*/
public function testGetPilotPay()
{
}
} }