From 3aa4b8a410ae0552335dc6ff1dc07c47944a2be8 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Tue, 27 Feb 2018 14:12:03 -0600 Subject: [PATCH] Cleanup and move subfleet/rank settings to FleetService class --- app/Http/Controllers/Admin/RankController.php | 19 +++++++--- .../Controllers/Admin/SubfleetController.php | 25 ++++++++++--- app/Services/AircraftService.php | 8 ---- app/Services/FleetService.php | 37 +++++++++++++++++++ tests/FinanceTest.php | 9 +++++ 5 files changed, 79 insertions(+), 19 deletions(-) delete mode 100644 app/Services/AircraftService.php create mode 100644 app/Services/FleetService.php diff --git a/app/Http/Controllers/Admin/RankController.php b/app/Http/Controllers/Admin/RankController.php index e89deb92..2bca2ade 100644 --- a/app/Http/Controllers/Admin/RankController.php +++ b/app/Http/Controllers/Admin/RankController.php @@ -6,6 +6,7 @@ use App\Http\Requests\CreateRankRequest; use App\Http\Requests\UpdateRankRequest; use App\Repositories\RankRepository; use App\Repositories\SubfleetRepository; +use App\Services\FleetService; use Cache; use Flash; use Illuminate\Http\Request; @@ -15,17 +16,22 @@ use Response; class RankController extends BaseController { /** @var RankRepository */ - private $rankRepository, $subfleetRepo; + private $fleetSvc, + $rankRepository, + $subfleetRepo; /** * RankController constructor. + * @param FleetService $fleetSvc * @param RankRepository $rankingRepo * @param SubfleetRepository $subfleetRepo */ public function __construct( + FleetService $fleetSvc, RankRepository $rankingRepo, SubfleetRepository $subfleetRepo ) { + $this->fleetSvc = $fleetSvc; $this->rankRepository = $rankingRepo; $this->subfleetRepo = $subfleetRepo; } @@ -204,19 +210,22 @@ class RankController extends BaseController // add aircraft to flight 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')) { $override = []; - $subfleet = $this->subfleetRepo->find($request->input('subfleet_id')); $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 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); diff --git a/app/Http/Controllers/Admin/SubfleetController.php b/app/Http/Controllers/Admin/SubfleetController.php index 43f684f3..c7f048e5 100644 --- a/app/Http/Controllers/Admin/SubfleetController.php +++ b/app/Http/Controllers/Admin/SubfleetController.php @@ -12,6 +12,7 @@ use App\Repositories\FareRepository; use App\Repositories\RankRepository; use App\Repositories\SubfleetRepository; use App\Services\FareService; +use App\Services\FleetService; use Flash; use Illuminate\Http\Request; use Prettus\Repository\Criteria\RequestCriteria; @@ -20,27 +21,36 @@ use Response; class SubfleetController extends BaseController { /** @var SubfleetRepository */ - private $aircraftRepo, $rankRepo, $subfleetRepo, $fareRepo, $fareSvc; + private $aircraftRepo, + $fareRepo, + $fareSvc, + $fleetSvc, + $rankRepo, + $subfleetRepo; /** * SubfleetController constructor. * @param AircraftRepository $aircraftRepo + * @param FleetService $fleetSvc + * @param RankRepository $rankRepo * @param SubfleetRepository $subfleetRepo * @param FareRepository $fareRepo * @param FareService $fareSvc */ public function __construct( AircraftRepository $aircraftRepo, + FleetService $fleetSvc, RankRepository $rankRepo, SubfleetRepository $subfleetRepo, FareRepository $fareRepo, FareService $fareSvc ) { $this->aircraftRepo = $aircraftRepo; - $this->rankRepo = $rankRepo; - $this->subfleetRepo = $subfleetRepo; $this->fareRepo = $fareRepo; $this->fareSvc = $fareSvc; + $this->fleetSvc = $fleetSvc; + $this->rankRepo = $rankRepo; + $this->subfleetRepo = $subfleetRepo; } /** @@ -271,19 +281,22 @@ class SubfleetController extends BaseController * update specific rank data */ 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')) { $override = []; $rank = $this->fareRepo->find($request->input('rank_id')); $override[$request->name] = $request->value; - $subfleet->ranks()->updateExistingPivot($rank->id, $override); + + $this->fleetSvc->addSubfleetToRank($subfleet, $rank, $override); } // dissassociate fare from teh aircraft 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(); diff --git a/app/Services/AircraftService.php b/app/Services/AircraftService.php deleted file mode 100644 index 4b6f94a8..00000000 --- a/app/Services/AircraftService.php +++ /dev/null @@ -1,8 +0,0 @@ -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; + } +} diff --git a/tests/FinanceTest.php b/tests/FinanceTest.php index a0ce4bea..6422950c 100644 --- a/tests/FinanceTest.php +++ b/tests/FinanceTest.php @@ -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() + { + + } }