diff --git a/app/Http/Controllers/Admin/ServiceController.php b/app/Http/Controllers/Admin/ServiceController.php index 02d8c7353..3f0670dae 100644 --- a/app/Http/Controllers/Admin/ServiceController.php +++ b/app/Http/Controllers/Admin/ServiceController.php @@ -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(); + } + } diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index e59103d5a..356df760c 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -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' diff --git a/app/Repositories/ServiceRepository/Option.php b/app/Repositories/ServiceRepository/Option.php index 92c4dd369..2622c3f6d 100644 --- a/app/Repositories/ServiceRepository/Option.php +++ b/app/Repositories/ServiceRepository/Option.php @@ -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); diff --git a/resources/views/admin/services/index.blade.php b/resources/views/admin/services/index.blade.php index 66b9b4e81..e55fce78e 100644 --- a/resources/views/admin/services/index.blade.php +++ b/resources/views/admin/services/index.blade.php @@ -35,6 +35,7 @@