From 1e9bf1c22033a920c12665ebdf4f87670ca51f89 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 20 Feb 2016 16:45:13 -0500 Subject: [PATCH] Add support for adding new service option --- .../Controllers/Admin/ServiceController.php | 30 +++++- app/Http/Routes/AdminRoutes.php | 9 ++ app/Repositories/ServiceRepository/Option.php | 33 +++++++ .../views/admin/services/index.blade.php | 7 ++ resources/views/admin/services/new.blade.php | 2 +- .../admin/services/options/new.blade.php | 98 +++++++++++++++++++ .../admin/services/options/view.blade.php | 2 +- resources/views/admin/services/view.blade.php | 9 +- 8 files changed, 186 insertions(+), 4 deletions(-) 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 @@ Service Type Description + Servers @@ -42,8 +43,14 @@ {{ $service->name }} {!! $service->description !!} + {{ $service->c_servers }} @endforeach + + + + + diff --git a/resources/views/admin/services/new.blade.php b/resources/views/admin/services/new.blade.php index 2961834a8..cd421ef9c 100644 --- a/resources/views/admin/services/new.blade.php +++ b/resources/views/admin/services/new.blade.php @@ -20,7 +20,7 @@ @extends('layouts.admin') @section('title') - Manage Services + New Service @endsection @section('content') diff --git a/resources/views/admin/services/options/new.blade.php b/resources/views/admin/services/options/new.blade.php index e69de29bb..7749682dc 100644 --- a/resources/views/admin/services/options/new.blade.php +++ b/resources/views/admin/services/options/new.blade.php @@ -0,0 +1,98 @@ +{{-- Copyright (c) 2015 - 2016 Dane Everitt --}} + +{{-- Permission is hereby granted, free of charge, to any person obtaining a copy --}} +{{-- of this software and associated documentation files (the "Software"), to deal --}} +{{-- in the Software without restriction, including without limitation the rights --}} +{{-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell --}} +{{-- copies of the Software, and to permit persons to whom the Software is --}} +{{-- furnished to do so, subject to the following conditions: --}} + +{{-- The above copyright notice and this permission notice shall be included in all --}} +{{-- copies or substantial portions of the Software. --}} + +{{-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR --}} +{{-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, --}} +{{-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE --}} +{{-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER --}} +{{-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, --}} +{{-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE --}} +{{-- SOFTWARE. --}} +@extends('layouts.admin') + +@section('title') + New Service Option for {{ $service->name }} +@endsection + +@section('content') +
+ +

Service Option Settings


+
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+ +
+ +

Leave blank to use parent executable.

+
+
+
+ +
+ +
+
+
+
+
+ +
+ +

To use the default startup of the parent service simply leave this field blank.

+
+
+
+
+
+
+ {!! csrf_field() !!} + +
+
+
+
+
+ +@endsection diff --git a/resources/views/admin/services/options/view.blade.php b/resources/views/admin/services/options/view.blade.php index aca4d83e1..31c55ffec 100644 --- a/resources/views/admin/services/options/view.blade.php +++ b/resources/views/admin/services/options/view.blade.php @@ -20,7 +20,7 @@ @extends('layouts.admin') @section('title') - Manage Services + Manage Service Option {{ $option->name }} @endsection @section('content') diff --git a/resources/views/admin/services/view.blade.php b/resources/views/admin/services/view.blade.php index 8ede24b62..28e560fc5 100644 --- a/resources/views/admin/services/view.blade.php +++ b/resources/views/admin/services/view.blade.php @@ -20,7 +20,7 @@ @extends('layouts.admin') @section('title') - Manage Services + Manage Service @endsection @section('content') @@ -51,6 +51,13 @@ {{ $option->c_servers }} @endforeach + + + + + + +