From dcfdb89e3ccd047629d0b8a95b2c65a13f1c2154 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 20 Feb 2016 16:55:05 -0500 Subject: [PATCH] add support for deleting service option --- .../Controllers/Admin/ServiceController.php | 18 +++++++++++++++ app/Http/Routes/AdminRoutes.php | 4 ++++ app/Repositories/ServiceRepository/Option.php | 22 +++++++++++++++++++ .../admin/services/options/view.blade.php | 12 ++++++++++ 4 files changed, 56 insertions(+) diff --git a/app/Http/Controllers/Admin/ServiceController.php b/app/Http/Controllers/Admin/ServiceController.php index 3f0670dae..5ab9e96df 100644 --- a/app/Http/Controllers/Admin/ServiceController.php +++ b/app/Http/Controllers/Admin/ServiceController.php @@ -156,6 +156,24 @@ class ServiceController extends Controller return redirect()->route('admin.services.option', $option)->withInput(); } + public function deleteOption(Request $request, $option) + { + try { + $service = Models\ServiceOptions::select('parent_service')->where('id', $option)->first(); + $repo = new ServiceRepository\Option; + $repo->delete($option); + + Alert::success('Successfully deleted that option.')->flash(); + return redirect()->route('admin.services.service', $service->parent_service); + } catch (DisplayException $ex) { + Alert::danger($ex->getMessage())->flash(); + } catch (\Exception $ex) { + Log::error($ex); + Alert::danger('An error was encountered while attempting to delete this option.')->flash(); + } + return redirect()->route('admin.services.option', $option); + } + public function postOptionVariable(Request $request, $option, $variable) { if ($variable === 'new') { diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index 356df760c..d597d342b 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -388,6 +388,10 @@ class AdminRoutes { 'uses' => 'Admin\ServiceController@postOption' ]); + $router->delete('/option/{id}', [ + 'uses' => 'Admin\ServiceController@deleteOption' + ]); + $router->post('/option/{option}/{variable}', [ 'as' => 'admin.services.option.variable', 'uses' => 'Admin\ServiceController@postOptionVariable' diff --git a/app/Repositories/ServiceRepository/Option.php b/app/Repositories/ServiceRepository/Option.php index 2622c3f6d..60476b50e 100644 --- a/app/Repositories/ServiceRepository/Option.php +++ b/app/Repositories/ServiceRepository/Option.php @@ -73,6 +73,28 @@ class Option return $option->id; } + public function delete($id) + { + $option = Models\ServiceOptions::findOrFail($id); + $servers = Models\Server::where('option', $option->id)->get(); + + if (count($servers) !== 0) { + throw new DisplayException('You cannot delete an option that has servers attached to it currently.'); + } + + DB::beginTransaction(); + + try { + Models\ServiceVariables::where('option_id', $option->id)->delete(); + $option->delete(); + + DB::commit(); + } catch (\Exception $ex) { + DB::rollBack(); + throw $ex; + } + } + public function update($id, array $data) { $option = Models\ServiceOptions::findOrFail($id); diff --git a/resources/views/admin/services/options/view.blade.php b/resources/views/admin/services/options/view.blade.php index 31c55ffec..cea388786 100644 --- a/resources/views/admin/services/options/view.blade.php +++ b/resources/views/admin/services/options/view.blade.php @@ -185,6 +185,18 @@ @endforeach +
+
+
+
+ Deleting an option is an irreversible action. An option can only be deleted if no servers are associated with it. +
+ {!! csrf_field() !!} + {!! method_field('DELETE') !!} + +
+
+