Skip to content

Commit 21a95a5

Browse files
committed
Add location delete support
1 parent 861af87 commit 21a95a5

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

app/Http/Controllers/Admin/LocationsController.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,28 @@ public function getIndex(Request $request)
2727
]);
2828
}
2929

30-
public function postView(Request $request)
30+
public function deleteLocation(Request $request, $id)
3131
{
32-
$location = Models\Location::findOrFail($request->input('location_id'));
32+
$model = Models\Location::select(
33+
'locations.id',
34+
DB::raw('(SELECT COUNT(*) FROM nodes WHERE nodes.location = locations.id) as a_nodeCount'),
35+
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node IN (SELECT nodes.id FROM nodes WHERE nodes.location = locations.id)) as a_serverCount')
36+
)->where('id', $id)->first();
37+
38+
if (!$model) {
39+
return response()->json([
40+
'error' => 'No location with that ID exists on the system.'
41+
], 404);
42+
}
43+
44+
if ($model->a_nodeCount > 0 || $model->a_serverCount > 0) {
45+
return response()->json([
46+
'error' => 'You cannot remove a location that is currently assigned to a node or server.'
47+
], 422);
48+
}
49+
50+
$model->delete();
51+
return response('', 204);
3352
}
3453

3554
}

app/Http/Routes/AdminRoutes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ public function map(Router $router) {
216216
'as' => 'admin.locations',
217217
'uses' => 'Admin\LocationsController@getIndex'
218218
]);
219+
$router->delete('/{id}', [
220+
'uses' => 'Admin\LocationsController@deleteLocation'
221+
]);
219222
});
220223

221224
// API Routes

resources/views/admin/locations/index.blade.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,71 @@
1818
<th>Description</th>
1919
<th class="text-center">Nodes</th>
2020
<th class="text-center">Servers</th>
21+
<th class="text-center"></th>
22+
<th class="text-center"></th>
2123
</tr>
2224
</thead>
2325
<tbody>
2426
@foreach ($locations as $location)
2527
<tr>
26-
<td><a href="#/edit/{{ $location->id }}" data-action="edit" data-location="{{ $location->id }}"><code>{{ $location->short }}</code></td>
28+
<td><code>{{ $location->short }}</code></td>
2729
<td>{{ $location->long }}</td>
2830
<td class="text-center">{{ $location->a_nodeCount }}</td>
2931
<td class="text-center">{{ $location->a_serverCount }}</td>
32+
<td class="text-center"><a href="#edit"><i class="fa fa-wrench" data-action="edit" data-id="{{ $location->id }}" data-short="{{ $location->short }}" data-long="{{ $location->long }}"></i></a></td>
33+
<td class="text-center"><a href="#delete" class="text-danger" data-action="delete" data-id="{{ $location->id }}"><i class="fa fa-trash-o"></i></a></td>
3034
</tr>
3135
@endforeach
3236
</tbody>
3337
</table>
3438
<div class="row">
3539
<div class="col-md-12 text-center">{!! $locations->render() !!}</div>
3640
</div>
41+
<div class="well">
42+
<div class="row">
43+
<div class="col-md-12">
44+
<button class="btn btn-sm btn-success" id="addNewLocation">Add New Location</button>
45+
</div>
46+
</div>
47+
</div>
3748
</div>
3849
<script>
3950
$(document).ready(function () {
4051
$('#sidebar_links').find("a[href='/admin/locations']").addClass('active');
52+
$('[data-action="delete"]').click(function (event) {
53+
event.preventDefault();
54+
var self = $(this);
55+
swal({
56+
type: 'warning',
57+
title: '',
58+
text: 'Do you really want to delete this location?',
59+
showCancelButton: true,
60+
closeOnConfirm: false,
61+
showLoaderOnConfirm: true
62+
}, function () {
63+
$.ajax({
64+
method: 'DELETE',
65+
url: '{{ route('admin.locations') }}/' + self.data('id'),
66+
headers: {
67+
'X-CSRF-TOKEN': '{{ csrf_token() }}'
68+
}
69+
}).done(function () {
70+
swal({
71+
type: 'success',
72+
title: '',
73+
text: 'Location was successfully deleted.'
74+
});
75+
self.parent().parent().slideUp();
76+
}).fail(function (jqXHR) {
77+
console.error(jqXHR);
78+
swal({
79+
type: 'error',
80+
title: 'Whoops!',
81+
text: (typeof jqXHR.responseJSON !== 'undefined') ? jqXHR.responseJSON.error : 'An error occured while processing this request.'
82+
});
83+
});
84+
});
85+
});
4186
});
4287
</script>
4388
@endsection

0 commit comments

Comments
 (0)