Rebased and fixed issue #1275

This commit is contained in:
Yash 2022-10-26 12:31:09 +05:30
parent b4311b861f
commit be0cbb1756

View File

@ -7,6 +7,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\Repositories\UserRepository;
use App\Services\FleetService; use App\Services\FleetService;
use Cache; use Cache;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -18,6 +19,7 @@ class RankController extends Controller
private FleetService $fleetSvc; private FleetService $fleetSvc;
private RankRepository $rankRepository; private RankRepository $rankRepository;
private SubfleetRepository $subfleetRepo; private SubfleetRepository $subfleetRepo;
private UserRepository $userRepo;
/** /**
* RankController constructor. * RankController constructor.
@ -25,15 +27,18 @@ class RankController extends Controller
* @param FleetService $fleetSvc * @param FleetService $fleetSvc
* @param RankRepository $rankingRepo * @param RankRepository $rankingRepo
* @param SubfleetRepository $subfleetRepo * @param SubfleetRepository $subfleetRepo
* @param UserRepository $userRepo
*/ */
public function __construct( public function __construct(
FleetService $fleetSvc, FleetService $fleetSvc,
RankRepository $rankingRepo, RankRepository $rankingRepo,
SubfleetRepository $subfleetRepo SubfleetRepository $subfleetRepo,
UserRepository $userRepo
) { ) {
$this->fleetSvc = $fleetSvc; $this->fleetSvc = $fleetSvc;
$this->rankRepository = $rankingRepo; $this->rankRepository = $rankingRepo;
$this->subfleetRepo = $subfleetRepo; $this->subfleetRepo = $subfleetRepo;
$this->userRepo = $userRepo;
} }
/** /**
@ -190,6 +195,19 @@ class RankController extends Controller
*/ */
public function destroy($id) 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); $rank = $this->rankRepository->findWithoutFail($id);
if (empty($rank)) { if (empty($rank)) {