diff --git a/app/Http/Controllers/Admin/LocationsController.php b/app/Http/Controllers/Admin/LocationsController.php index 190050948..dbf108f2d 100644 --- a/app/Http/Controllers/Admin/LocationsController.php +++ b/app/Http/Controllers/Admin/LocationsController.php @@ -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); } } diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index 5f45a25a1..cc32e31d8 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -216,6 +216,9 @@ class AdminRoutes { 'as' => 'admin.locations', 'uses' => 'Admin\LocationsController@getIndex' ]); + $router->delete('/{id}', [ + 'uses' => 'Admin\LocationsController@deleteLocation' + ]); }); // API Routes diff --git a/resources/views/admin/locations/index.blade.php b/resources/views/admin/locations/index.blade.php index c84dd238e..643c55be1 100644 --- a/resources/views/admin/locations/index.blade.php +++ b/resources/views/admin/locations/index.blade.php @@ -18,15 +18,19 @@ Description Nodes Servers + + @foreach ($locations as $location) - {{ $location->short }} + {{ $location->short }} {{ $location->long }} {{ $location->a_nodeCount }} {{ $location->a_serverCount }} + + @endforeach @@ -34,10 +38,51 @@
{!! $locations->render() !!}
+
+
+
+ +
+
+
@endsection