add support for variable creation and deletion

This commit is contained in:
Dane Everitt 2016-02-21 00:38:03 -05:00
parent dcf2f6fa0a
commit 48b9bc0c52
5 changed files with 198 additions and 4 deletions

View file

@ -202,6 +202,34 @@ class ServiceController extends Controller
return redirect()->route('admin.services.option', [$service, $option])->withInput();
}
public function getNewVariable(Request $request, $service, $option)
{
return view('admin.services.options.variable', [
'service' => Models\Service::findOrFail($service),
'option' => Models\ServiceOptions::where('parent_service', $service)->where('id', $option)->firstOrFail()
]);
}
public function postNewVariable(Request $request, $service, $option)
{
try {
$repo = new ServiceRepository\Variable;
$repo->create($option, $request->except([
'_token'
]));
Alert::success('Successfully added new variable to this option.')->flash();
return redirect()->route('admin.services.option', [$service, $option])->withInput();
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.services.option.variable.new', [$service, $option])->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch (\Exception $ex) {
Log::error($ex);
Alert::danger('An error occurred while attempting to add this variable.')->flash();
}
return redirect()->route('admin.services.option.variable.new', [$service, $option])->withInput();
}
public function newOption(Request $request, $service)
{
return view('admin.services.options.new', [
@ -227,4 +255,19 @@ class ServiceController extends Controller
return redirect()->route('admin.services.option.new', $service)->withInput();
}
public function deleteVariable(Request $request, $service, $option, $variable)
{
try {
$repo = new ServiceRepository\Variable;
$repo->delete($variable);
Alert::success('Deleted variable.')->flash();
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch (\Exception $ex) {
Log::error($ex);
Alert::danger('An error occured while attempting to delete that variable.')->flash();
}
return redirect()->route('admin.services.option', [$service, $option]);
}
}

View file

@ -392,10 +392,24 @@ class AdminRoutes {
'uses' => 'Admin\ServiceController@deleteOption'
]);
$router->get('/service/{service}/option/{option}/variable/new', [
'as' => 'admin.services.option.variable.new',
'uses' => 'Admin\ServiceController@getNewVariable'
]);
$router->post('/service/{service}/option/{option}/variable/new', [
'uses' => 'Admin\ServiceController@postNewVariable'
]);
$router->post('/service/{service}/option/{option}/variable/{variable}', [
'as' => 'admin.services.option.variable',
'uses' => 'Admin\ServiceController@postOptionVariable'
]);
$router->get('/service/{service}/option/{option}/variable/{variable}/delete', [
'as' => 'admin.services.option.variable.delete',
'uses' => 'Admin\ServiceController@deleteVariable'
]);
});
}

View file

@ -40,6 +40,58 @@ class Variable
//
}
public function create($id, array $data)
{
$option = Models\ServiceOptions::findOrFail($id);
$validator = Validator::make($data, [
'name' => 'required|string|min:1|max:255',
'description' => 'required|string',
'env_variable' => 'required|regex:/^[\w]{1,255}$/',
'default_value' => 'required|string|max:255',
'user_viewable' => 'sometimes|required|numeric|size:1',
'user_editable' => 'sometimes|required|numeric|size:1',
'required' => 'sometimes|required|numeric|size:1',
'regex' => 'required|string|min:1'
]);
if ($validator->fails()) {
throw new DisplayValidationException($validator->errors());
}
if (!preg_match($data['regex'], $data['default_value'])) {
throw new DisplayException('The default value you entered cannot violate the regex requirements.');
}
if (Models\ServiceVariables::where('env_variable', $data['env_variable'])->where('option_id', $option->id)->first()) {
throw new DisplayException('An environment variable with that name already exists for this option.');
}
$data['user_viewable'] = (isset($data['user_viewable']) && in_array((int) $data['user_viewable'], [0, 1])) ? $data['user_viewable'] : 0;
$data['user_editable'] = (isset($data['user_editable']) && in_array((int) $data['user_editable'], [0, 1])) ? $data['user_editable'] : 0;
$data['required'] = (isset($data['required']) && in_array((int) $data['required'], [0, 1])) ? $data['required'] : 0;
$variable = new Models\ServiceVariables;
$variable->option_id = $option->id;
$variable->fill($data);
$variable->save();
}
public function delete($id) {
$variable = Models\ServiceVariables::findOrFail($id);
DB::beginTransaction();
try {
Models\ServerVariables::where('variable_id', $variable->id)->delete();
$variable->delete();
DB::commit();
} catch (\Exception $ex) {
DB::rollBack();
throw $ex;
}
}
public function update($id, array $data)
{
$variable = Models\ServiceVariables::findOrFail($id);
@ -48,7 +100,7 @@ class Variable
'name' => 'sometimes|required|string|min:1|max:255',
'description' => 'sometimes|required|string',
'env_variable' => 'sometimes|required|regex:/^[\w]{1,255}$/',
'default_value' => 'sometimes|required|string',
'default_value' => 'sometimes|required|string|max:255',
'user_viewable' => 'sometimes|required|numeric|size:1',
'user_editable' => 'sometimes|required|numeric|size:1',
'required' => 'sometimes|required|numeric|size:1',
@ -66,6 +118,10 @@ class Variable
throw new DisplayException('The default value you entered cannot violate the regex requirements.');
}
if (Models\ServiceVariables::where('id', '!=', $variable->id)->where('env_variable', $data['env_variable'])->where('option_id', $variable->option_id)->first()) {
throw new DisplayException('An environment variable with that name already exists for this option.');
}
$data['user_viewable'] = (isset($data['user_viewable']) && in_array((int) $data['user_viewable'], [0, 1])) ? $data['user_viewable'] : $variable->user_viewable;
$data['user_editable'] = (isset($data['user_editable']) && in_array((int) $data['user_editable'], [0, 1])) ? $data['user_editable'] : $variable->user_editable;
$data['required'] = (isset($data['required']) && in_array((int) $data['required'], [0, 1])) ? $data['required'] : $variable->required;