Add location delete support

This commit is contained in:
Dane Everitt 2016-01-16 22:29:35 -05:00
parent 861af87e93
commit 21a95a5d0e
3 changed files with 70 additions and 3 deletions

View file

@ -27,9 +27,28 @@ class LocationsController extends Controller
]);
}
public function postView(Request $request)
public function deleteLocation(Request $request, $id)
{
$location = Models\Location::findOrFail($request->input('location_id'));
$model = Models\Location::select(
'locations.id',
DB::raw('(SELECT COUNT(*) FROM nodes WHERE nodes.location = locations.id) as a_nodeCount'),
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node IN (SELECT nodes.id FROM nodes WHERE nodes.location = locations.id)) as a_serverCount')
)->where('id', $id)->first();
if (!$model) {
return response()->json([
'error' => 'No location with that ID exists on the system.'
], 404);
}
if ($model->a_nodeCount > 0 || $model->a_serverCount > 0) {
return response()->json([
'error' => 'You cannot remove a location that is currently assigned to a node or server.'
], 422);
}
$model->delete();
return response('', 204);
}
}

View file

@ -216,6 +216,9 @@ class AdminRoutes {
'as' => 'admin.locations',
'uses' => 'Admin\LocationsController@getIndex'
]);
$router->delete('/{id}', [
'uses' => 'Admin\LocationsController@deleteLocation'
]);
});
// API Routes

View file

@ -18,15 +18,19 @@
<th>Description</th>
<th class="text-center">Nodes</th>
<th class="text-center">Servers</th>
<th class="text-center"></th>
<th class="text-center"></th>
</tr>
</thead>
<tbody>
@foreach ($locations as $location)
<tr>
<td><a href="#/edit/{{ $location->id }}" data-action="edit" data-location="{{ $location->id }}"><code>{{ $location->short }}</code></td>
<td><code>{{ $location->short }}</code></td>
<td>{{ $location->long }}</td>
<td class="text-center">{{ $location->a_nodeCount }}</td>
<td class="text-center">{{ $location->a_serverCount }}</td>
<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>
<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>
</tr>
@endforeach
</tbody>
@ -34,10 +38,51 @@
<div class="row">
<div class="col-md-12 text-center">{!! $locations->render() !!}</div>
</div>
<div class="well">
<div class="row">
<div class="col-md-12">
<button class="btn btn-sm btn-success" id="addNewLocation">Add New Location</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
$('#sidebar_links').find("a[href='/admin/locations']").addClass('active');
$('[data-action="delete"]').click(function (event) {
event.preventDefault();
var self = $(this);
swal({
type: 'warning',
title: '',
text: 'Do you really want to delete this location?',
showCancelButton: true,
closeOnConfirm: false,
showLoaderOnConfirm: true
}, function () {
$.ajax({
method: 'DELETE',
url: '{{ route('admin.locations') }}/' + self.data('id'),
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
}).done(function () {
swal({
type: 'success',
title: '',
text: 'Location was successfully deleted.'
});
self.parent().parent().slideUp();
}).fail(function (jqXHR) {
console.error(jqXHR);
swal({
type: 'error',
title: 'Whoops!',
text: (typeof jqXHR.responseJSON !== 'undefined') ? jqXHR.responseJSON.error : 'An error occured while processing this request.'
});
});
});
});
});
</script>
@endsection