Skip to content

Commit dcfdb89

Browse files
committed
add support for deleting service option
1 parent 1e9bf1c commit dcfdb89

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

app/Http/Controllers/Admin/ServiceController.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,24 @@ public function postOption(Request $request, $option)
156156
return redirect()->route('admin.services.option', $option)->withInput();
157157
}
158158

159+
public function deleteOption(Request $request, $option)
160+
{
161+
try {
162+
$service = Models\ServiceOptions::select('parent_service')->where('id', $option)->first();
163+
$repo = new ServiceRepository\Option;
164+
$repo->delete($option);
165+
166+
Alert::success('Successfully deleted that option.')->flash();
167+
return redirect()->route('admin.services.service', $service->parent_service);
168+
} catch (DisplayException $ex) {
169+
Alert::danger($ex->getMessage())->flash();
170+
} catch (\Exception $ex) {
171+
Log::error($ex);
172+
Alert::danger('An error was encountered while attempting to delete this option.')->flash();
173+
}
174+
return redirect()->route('admin.services.option', $option);
175+
}
176+
159177
public function postOptionVariable(Request $request, $option, $variable)
160178
{
161179
if ($variable === 'new') {

app/Http/Routes/AdminRoutes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ public function map(Router $router) {
388388
'uses' => 'Admin\ServiceController@postOption'
389389
]);
390390

391+
$router->delete('/option/{id}', [
392+
'uses' => 'Admin\ServiceController@deleteOption'
393+
]);
394+
391395
$router->post('/option/{option}/{variable}', [
392396
'as' => 'admin.services.option.variable',
393397
'uses' => 'Admin\ServiceController@postOptionVariable'

app/Repositories/ServiceRepository/Option.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,28 @@ public function create($service, array $data)
7373
return $option->id;
7474
}
7575

76+
public function delete($id)
77+
{
78+
$option = Models\ServiceOptions::findOrFail($id);
79+
$servers = Models\Server::where('option', $option->id)->get();
80+
81+
if (count($servers) !== 0) {
82+
throw new DisplayException('You cannot delete an option that has servers attached to it currently.');
83+
}
84+
85+
DB::beginTransaction();
86+
87+
try {
88+
Models\ServiceVariables::where('option_id', $option->id)->delete();
89+
$option->delete();
90+
91+
DB::commit();
92+
} catch (\Exception $ex) {
93+
DB::rollBack();
94+
throw $ex;
95+
}
96+
}
97+
7698
public function update($id, array $data)
7799
{
78100
$option = Models\ServiceOptions::findOrFail($id);

resources/views/admin/services/options/view.blade.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@
185185
@endforeach
186186
</tbody>
187187
</table>
188+
<form action="{{ route('admin.services.option', $option->id) }}" method="POST">
189+
<div class="row">
190+
<div class="col-md-12">
191+
<div class="alert alert-danger">
192+
Deleting an option is an irreversible action. An option can <em>only</em> be deleted if no servers are associated with it.
193+
</div>
194+
{!! csrf_field() !!}
195+
{!! method_field('DELETE') !!}
196+
<input type="submit" class="btn btn-sm btn-danger pull-right" value="Delete Option" />
197+
</div>
198+
</div>
199+
</form>
188200
</div>
189201
<script>
190202
$(document).ready(function () {

0 commit comments

Comments
 (0)