Add support for adding new service option
This commit is contained in:
parent
177bd4ec9d
commit
1e9bf1c220
8 changed files with 186 additions and 4 deletions
|
@ -47,7 +47,10 @@ class ServiceController extends Controller
|
|||
public function getIndex(Request $request)
|
||||
{
|
||||
return view('admin.services.index', [
|
||||
'services' => Models\Service::all()
|
||||
'services' => Models\Service::select(
|
||||
'services.*',
|
||||
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.service = services.id) as c_servers')
|
||||
)->get()
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -185,4 +188,29 @@ class ServiceController extends Controller
|
|||
}
|
||||
}
|
||||
|
||||
public function newOption(Request $request, $service)
|
||||
{
|
||||
return view('admin.services.options.new', [
|
||||
'service' => Models\Service::findOrFail($service),
|
||||
]);
|
||||
}
|
||||
|
||||
public function postNewOption(Request $request, $service)
|
||||
{
|
||||
try {
|
||||
$repo = new ServiceRepository\Option;
|
||||
$id = $repo->create($service, $request->except([
|
||||
'_token'
|
||||
]));
|
||||
Alert::success('Successfully created new service option.')->flash();
|
||||
return redirect()->route('admin.services.option', $id);
|
||||
} catch (DisplayValidationException $ex) {
|
||||
return redirect()->route('admin.services.option.new', $service)->withErrors(json_decode($ex->getMessage()))->withInput();
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
Alert::danger('An error occured while attempting to add this service option.')->flash();
|
||||
}
|
||||
return redirect()->route('admin.services.option.new', $service)->withInput();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -370,6 +370,15 @@ class AdminRoutes {
|
|||
'uses' => 'Admin\ServiceController@deleteService'
|
||||
]);
|
||||
|
||||
$router->get('/option/new/{service}', [
|
||||
'as' => 'admin.services.option.new',
|
||||
'uses' => 'Admin\ServiceController@newOption'
|
||||
]);
|
||||
|
||||
$router->post('/option/new/{service}', [
|
||||
'uses' => 'Admin\ServiceController@postNewOption'
|
||||
]);
|
||||
|
||||
$router->get('/option/{id}', [
|
||||
'as' => 'admin.services.option',
|
||||
'uses' => 'Admin\ServiceController@getOption'
|
||||
|
|
|
@ -40,6 +40,39 @@ class Option
|
|||
//
|
||||
}
|
||||
|
||||
public function create($service, array $data)
|
||||
{
|
||||
$service = Models\Service::findOrFail($service);
|
||||
|
||||
$validator = Validator::make($data, [
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'required|string|min:1',
|
||||
'tag' => 'required|string|max:255',
|
||||
'executable' => 'sometimes|string|max:255',
|
||||
'docker_image' => 'required|string|max:255',
|
||||
'startup' => 'sometimes|string'
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
throw new DisplayValidationException($validator->errors());
|
||||
}
|
||||
|
||||
if (isset($data['executable']) && empty($data['executable'])) {
|
||||
$data['executable'] = null;
|
||||
}
|
||||
|
||||
if (isset($data['startup']) && empty($data['startup'])) {
|
||||
$data['startup'] = null;
|
||||
}
|
||||
|
||||
$option = new Models\ServiceOptions;
|
||||
$option->parent_service = $service->id;
|
||||
$option->fill($data);
|
||||
$option->save();
|
||||
|
||||
return $option->id;
|
||||
}
|
||||
|
||||
public function update($id, array $data)
|
||||
{
|
||||
$option = Models\ServiceOptions::findOrFail($id);
|
||||
|
|
Reference in a new issue