Add location delete support
This commit is contained in:
parent
861af87e93
commit
21a95a5d0e
3 changed files with 70 additions and 3 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -216,6 +216,9 @@ class AdminRoutes {
|
|||
'as' => 'admin.locations',
|
||||
'uses' => 'Admin\LocationsController@getIndex'
|
||||
]);
|
||||
$router->delete('/{id}', [
|
||||
'uses' => 'Admin\LocationsController@deleteLocation'
|
||||
]);
|
||||
});
|
||||
|
||||
// API Routes
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue