diff --git a/app/Http/Controllers/Admin/RankController.php b/app/Http/Controllers/Admin/RankController.php index 6a6f008b..be798914 100644 --- a/app/Http/Controllers/Admin/RankController.php +++ b/app/Http/Controllers/Admin/RankController.php @@ -7,6 +7,7 @@ use App\Http\Requests\CreateRankRequest; use App\Http\Requests\UpdateRankRequest; use App\Repositories\RankRepository; use App\Repositories\SubfleetRepository; +use App\Repositories\UserRepository; use App\Services\FleetService; use Cache; use Illuminate\Http\Request; @@ -18,6 +19,7 @@ class RankController extends Controller private FleetService $fleetSvc; private RankRepository $rankRepository; private SubfleetRepository $subfleetRepo; + private UserRepository $userRepo; /** * RankController constructor. @@ -25,15 +27,18 @@ class RankController extends Controller * @param FleetService $fleetSvc * @param RankRepository $rankingRepo * @param SubfleetRepository $subfleetRepo + * @param UserRepository $userRepo */ public function __construct( FleetService $fleetSvc, RankRepository $rankingRepo, - SubfleetRepository $subfleetRepo + SubfleetRepository $subfleetRepo, + UserRepository $userRepo ) { $this->fleetSvc = $fleetSvc; $this->rankRepository = $rankingRepo; $this->subfleetRepo = $subfleetRepo; + $this->userRepo = $userRepo; } /** @@ -190,6 +195,19 @@ class RankController extends Controller */ public function destroy($id) { + $rank_in_use = $this->userRepo->findWhere(['rank_id' => $id])->count(); + if ($rank_in_use > 0) { + Flash::error('Rank cannot be deleted since it\'s already assigned to one or more pilots!'); + + return redirect(route('admin.ranks.index')); + } + + if ($this->rankRepository->count() === 1) { + Flash::error('Rank cannot be deleted since it\'s the only remaining rank in your database!'); + + return redirect(route('admin.ranks.index')); + } + $rank = $this->rankRepository->findWithoutFail($id); if (empty($rank)) {