add support for deleting service option

This commit is contained in:
Dane Everitt 2016-02-20 16:55:05 -05:00
parent 1e9bf1c220
commit dcfdb89e3c
4 changed files with 56 additions and 0 deletions

View file

@ -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') {

View file

@ -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'

View file

@ -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);

View file

@ -185,6 +185,18 @@
@endforeach
</tbody>
</table>
<form action="{{ route('admin.services.option', $option->id) }}" method="POST">
<div class="row">
<div class="col-md-12">
<div class="alert alert-danger">
Deleting an option is an irreversible action. An option can <em>only</em> be deleted if no servers are associated with it.
</div>
{!! csrf_field() !!}
{!! method_field('DELETE') !!}
<input type="submit" class="btn btn-sm btn-danger pull-right" value="Delete Option" />
</div>
</div>
</form>
</div>
<script>
$(document).ready(function () {