Skip to content

Commit 46b3d2f

Browse files
committed
feat: Add ability to delete unused airports
This adds the ability to delete unused airports. Only works if not coupled to a Event, Booking and AirportLink Fix #399
1 parent 90b265b commit 46b3d2f

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

app/Http/Controllers/Airport/AirportAdminController.php

+14
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,18 @@ public function import(): RedirectResponse
8585
flashMessage('success', __('Done'), __('Airports have been added'));
8686
return redirect(route('admin.airports.index'));
8787
}
88+
89+
public function destroyUnused()
90+
{
91+
$this->authorize('destroy', Airport::class);
92+
Airport::whereDoesntHave('flightsDep')
93+
->whereDoesntHave('flightsArr')
94+
->whereDoesntHave('eventDep')
95+
->whereDoesntHave('eventArr')
96+
->delete();
97+
98+
flashMessage('success', __('Done'), __('Unused airport have been deleted!'));
99+
100+
return redirect(route('admin.airports.index'));
101+
}
88102
}

resources/views/airport/admin/overview.blade.php

+28-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@include('layouts.alert')
77
@push('scripts')
88
<script>
9-
$('.delete-airport').on('click', function (e) {
9+
$('.delete-airport').on('click', function(e) {
1010
e.preventDefault();
1111
Swal.fire({
1212
title: 'Are you sure',
@@ -21,23 +21,40 @@
2121
}
2222
});
2323
});
24+
$('.delete-unused-airports').on('click', function(e) {
25+
e.preventDefault();
26+
Swal.fire({
27+
title: 'Are you sure',
28+
text: 'Are you sure you want to delete all unused airports?',
29+
icon: 'warning',
30+
showCancelButton: true,
31+
}).then((result) => {
32+
if (result.value) {
33+
Swal.fire('Deleting unused airports...');
34+
Swal.showLoading();
35+
$(`#${$(this).attr('form')}`).submit();
36+
}
37+
});
38+
});
2439
</script>
2540
@endpush
2641
<p>
2742
<a href="{{ route('admin.airports.create') }}" class="btn btn-primary"><i class="fa fa-plus"></i> Add new
2843
Airport</a>&nbsp;
29-
<a href=" {{ route('admin.airportLinks.create') }}" class="btn btn-primary"><i class="fa fa-plus"></i> Add new
44+
<a href="{{ route('admin.airportLinks.create') }}" class="btn btn-primary"><i class="fa fa-plus"></i> Add new
3045
Airport
3146
Link</a>
47+
<button class="btn btn-danger delete-unused-airports" form="delete-unused-airports"><i class="fa fa-trash"></i>
48+
Delete unused airports</button>
3249
</p>
3350
<table class="table table-hover">
3451
<thead>
35-
<tr>
36-
<th scope="row">ICAO</th>
37-
<th scope="row">IATA</th>
38-
<th scope="row">Name</th>
39-
<th scope="row" colspan="2">Actions</th>
40-
</tr>
52+
<tr>
53+
<th scope="row">ICAO</th>
54+
<th scope="row">IATA</th>
55+
<th scope="row">Name</th>
56+
<th scope="row" colspan="2">Actions</th>
57+
</tr>
4158
</thead>
4259
@forelse($airports as $airport)
4360
<tr>
@@ -52,7 +69,7 @@
5269
</a>
5370
</td>
5471
<td>
55-
@if($airport->flightsDep->isEmpty() && $airport->flightsArr->isEmpty() && $airport->eventDep->isEmpty() && $airport->eventArr->isEmpty())
72+
@if ($airport->flightsDep->isEmpty() && $airport->flightsArr->isEmpty() && $airport->eventDep->isEmpty() && $airport->eventArr->isEmpty())
5673
<form action="{{ route('admin.airports.destroy', $airport) }}" method="post">
5774
@method('DELETE')
5875
<button class="btn btn-danger delete-airport"><i class="fa fa-trash"></i> Remove Airport
@@ -74,4 +91,6 @@
7491
{{ $airports->links() }}
7592
</table>
7693
{{ $airports->links() }}
94+
<x-form :action="route('admin.airports.destroyUnused')" id="delete-unused-airports" method="POST"
95+
style="display: none;"></x-form>
7796
@endsection

routes/web.php

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
Route::group(['as' => 'admin.', 'prefix' => 'admin', 'middleware' => 'auth.isAdmin'], function () {
3434
// Airports
3535
Route::get('airports/import', [AirportAdminController::class, 'import'])->name('airports.import');
36+
Route::post('airports/destroy-unused', [AirportAdminController::class, 'destroyUnused'])->name('airports.destroyUnused');
3637
Route::resource('airports', AirportAdminController::class);
3738

3839
// AirportLinks

0 commit comments

Comments
 (0)