diff --git a/app/Http/Controllers/Admin/ServiceController.php b/app/Http/Controllers/Admin/ServiceController.php index e0496d1b0..02d8c7353 100644 --- a/app/Http/Controllers/Admin/ServiceController.php +++ b/app/Http/Controllers/Admin/ServiceController.php @@ -106,6 +106,22 @@ class ServiceController extends Controller return redirect()->route('admin.services.service', $service)->withInput(); } + public function deleteService(Request $request, $service) + { + try { + $repo = new ServiceRepository\Service; + $repo->delete($service); + Alert::success('Successfully deleted that service.')->flash(); + return redirect()->route('admin.services'); + } catch (DisplayException $ex) { + Alert::danger($ex->getMessage())->flash(); + } catch (\Exception $ex) { + Log::error($ex); + Alert::danger('An error was encountered while attempting to delete that service.')->flash(); + } + return redirect()->route('admin.services.service', $service); + } + public function getOption(Request $request, $option) { $opt = Models\ServiceOptions::findOrFail($option); diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index c9a20441b..e59103d5a 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -366,6 +366,10 @@ class AdminRoutes { 'uses' => 'Admin\ServiceController@postService' ]); + $router->delete('/service/{id}', [ + 'uses' => 'Admin\ServiceController@deleteService' + ]); + $router->get('/option/{id}', [ 'as' => 'admin.services.option', 'uses' => 'Admin\ServiceController@getOption' diff --git a/app/Repositories/ServiceRepository/Service.php b/app/Repositories/ServiceRepository/Service.php index 2595c47ea..84e01f3e4 100644 --- a/app/Repositories/ServiceRepository/Service.php +++ b/app/Repositories/ServiceRepository/Service.php @@ -85,4 +85,26 @@ class Service $service->save(); } + public function delete($id) + { + $service = Models\Service::findOrFail($id); + $servers = Models\Server::where('service', $service->id)->get(); + $options = Models\ServiceOptions::select('id')->where('parent_service', $service->id); + + if (count($servers) !== 0) { + throw new DisplayException('You cannot delete a service that has servers associated with it.'); + } + + DB::beginTransaction(); + try { + Models\ServiceVariables::whereIn('option_id', $options->get()->toArray())->delete(); + $options->delete(); + $service->delete(); + DB::commit(); + } catch (\Exception $ex) { + DB::rollBack(); + throw $ex; + } + } + } diff --git a/resources/views/admin/services/view.blade.php b/resources/views/admin/services/view.blade.php index 9526ed5ff..8ede24b62 100644 --- a/resources/views/admin/services/view.blade.php +++ b/resources/views/admin/services/view.blade.php @@ -106,6 +106,18 @@ +