Skip to content

Commit

Permalink
Rebased and fixed issue #1275 (#1489)
Browse files Browse the repository at this point in the history
  • Loading branch information
YashGovekar committed Aug 23, 2023
1 parent 7590565 commit bdc6d4b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion app/Http/Controllers/Admin/RankController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Rank;
use App\Repositories\RankRepository;
use App\Repositories\SubfleetRepository;
use App\Repositories\UserRepository;
use App\Services\FleetService;
use Cache;
use Illuminate\Http\RedirectResponse;
Expand All @@ -24,11 +25,13 @@ class RankController extends Controller
* @param FleetService $fleetSvc
* @param RankRepository $rankRepo
* @param SubfleetRepository $subfleetRepo
* @param UserRepository $userRepo
*/
public function __construct(
private readonly FleetService $fleetSvc,
private readonly RankRepository $rankRepo,
private readonly SubfleetRepository $subfleetRepo
private readonly SubfleetRepository $subfleetRepo,
private readonly UserRepository $userRepo
) {
}

Expand Down Expand Up @@ -186,6 +189,19 @@ public function update(int $id, UpdateRankRequest $request): RedirectResponse
*/
public function destroy(int $id): RedirectResponse
{
$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->rankRepo->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->rankRepo->findWithoutFail($id);

if (empty($rank)) {
Expand Down

0 comments on commit bdc6d4b

Please sign in to comment.