Skip to content

Commit 1e9bf1c

Browse files
committed
Add support for adding new service option
1 parent 177bd4e commit 1e9bf1c

File tree

8 files changed

+186
-4
lines changed

8 files changed

+186
-4
lines changed

app/Http/Controllers/Admin/ServiceController.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public function __construct()
4747
public function getIndex(Request $request)
4848
{
4949
return view('admin.services.index', [
50-
'services' => Models\Service::all()
50+
'services' => Models\Service::select(
51+
'services.*',
52+
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.service = services.id) as c_servers')
53+
)->get()
5154
]);
5255
}
5356

@@ -185,4 +188,29 @@ public function postOptionVariable(Request $request, $option, $variable)
185188
}
186189
}
187190

191+
public function newOption(Request $request, $service)
192+
{
193+
return view('admin.services.options.new', [
194+
'service' => Models\Service::findOrFail($service),
195+
]);
196+
}
197+
198+
public function postNewOption(Request $request, $service)
199+
{
200+
try {
201+
$repo = new ServiceRepository\Option;
202+
$id = $repo->create($service, $request->except([
203+
'_token'
204+
]));
205+
Alert::success('Successfully created new service option.')->flash();
206+
return redirect()->route('admin.services.option', $id);
207+
} catch (DisplayValidationException $ex) {
208+
return redirect()->route('admin.services.option.new', $service)->withErrors(json_decode($ex->getMessage()))->withInput();
209+
} catch (\Exception $ex) {
210+
Log::error($ex);
211+
Alert::danger('An error occured while attempting to add this service option.')->flash();
212+
}
213+
return redirect()->route('admin.services.option.new', $service)->withInput();
214+
}
215+
188216
}

app/Http/Routes/AdminRoutes.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,15 @@ public function map(Router $router) {
370370
'uses' => 'Admin\ServiceController@deleteService'
371371
]);
372372

373+
$router->get('/option/new/{service}', [
374+
'as' => 'admin.services.option.new',
375+
'uses' => 'Admin\ServiceController@newOption'
376+
]);
377+
378+
$router->post('/option/new/{service}', [
379+
'uses' => 'Admin\ServiceController@postNewOption'
380+
]);
381+
373382
$router->get('/option/{id}', [
374383
'as' => 'admin.services.option',
375384
'uses' => 'Admin\ServiceController@getOption'

app/Repositories/ServiceRepository/Option.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,39 @@ public function __construct()
4040
//
4141
}
4242

43+
public function create($service, array $data)
44+
{
45+
$service = Models\Service::findOrFail($service);
46+
47+
$validator = Validator::make($data, [
48+
'name' => 'required|string|max:255',
49+
'description' => 'required|string|min:1',
50+
'tag' => 'required|string|max:255',
51+
'executable' => 'sometimes|string|max:255',
52+
'docker_image' => 'required|string|max:255',
53+
'startup' => 'sometimes|string'
54+
]);
55+
56+
if ($validator->fails()) {
57+
throw new DisplayValidationException($validator->errors());
58+
}
59+
60+
if (isset($data['executable']) && empty($data['executable'])) {
61+
$data['executable'] = null;
62+
}
63+
64+
if (isset($data['startup']) && empty($data['startup'])) {
65+
$data['startup'] = null;
66+
}
67+
68+
$option = new Models\ServiceOptions;
69+
$option->parent_service = $service->id;
70+
$option->fill($data);
71+
$option->save();
72+
73+
return $option->id;
74+
}
75+
4376
public function update($id, array $data)
4477
{
4578
$option = Models\ServiceOptions::findOrFail($id);

resources/views/admin/services/index.blade.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,22 @@
3535
<tr>
3636
<th>Service Type</th>
3737
<th>Description</th>
38+
<th class="text-center">Servers</th>
3839
</tr>
3940
</thead>
4041
<tbody>
4142
@foreach ($services as $service)
4243
<tr>
4344
<td><a href="{{ route('admin.services.service', $service->id) }}">{{ $service->name }}</a></td>
4445
<td>{!! $service->description !!}</td>
46+
<td class="text-center">{{ $service->c_servers }}</td>
4547
</tr>
4648
@endforeach
49+
<tr>
50+
<td></td>
51+
<td></td>
52+
<td class="text-center"><a href="{{ route('admin.services.new') }}"><i class="fa fa-plus"></i></a></td>
53+
</tr>
4754
</tbody>
4855
</table>
4956
</div>

resources/views/admin/services/new.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
@extends('layouts.admin')
2121

2222
@section('title')
23-
Manage Services
23+
New Service
2424
@endsection
2525

2626
@section('content')
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{{-- Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com> --}}
2+
3+
{{-- Permission is hereby granted, free of charge, to any person obtaining a copy --}}
4+
{{-- of this software and associated documentation files (the "Software"), to deal --}}
5+
{{-- in the Software without restriction, including without limitation the rights --}}
6+
{{-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell --}}
7+
{{-- copies of the Software, and to permit persons to whom the Software is --}}
8+
{{-- furnished to do so, subject to the following conditions: --}}
9+
10+
{{-- The above copyright notice and this permission notice shall be included in all --}}
11+
{{-- copies or substantial portions of the Software. --}}
12+
13+
{{-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR --}}
14+
{{-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, --}}
15+
{{-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE --}}
16+
{{-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER --}}
17+
{{-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, --}}
18+
{{-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE --}}
19+
{{-- SOFTWARE. --}}
20+
@extends('layouts.admin')
21+
22+
@section('title')
23+
New Service Option for {{ $service->name }}
24+
@endsection
25+
26+
@section('content')
27+
<div class="col-md-12">
28+
<ul class="breadcrumb">
29+
<li><a href="/admin">Admin Control</a></li>
30+
<li><a href="/admin/services">Services</a></li>
31+
<li><a href="{{ route('admin.services.service', $service->id) }}">{{ $service->name }}</a></li>
32+
<li class="active">New Service Option</li>
33+
</ul>
34+
<h3>Service Option Settings</h3><hr />
35+
<form action="{{ route('admin.services.option.new', $service->id) }}" method="POST">
36+
<div class="row">
37+
<div class="col-md-6 form-group">
38+
<label class="control-label">Name:</label>
39+
<div>
40+
<input type="text" name="name" value="{{ old('name') }}" class="form-control" />
41+
</div>
42+
</div>
43+
<div class="col-md-6 form-group">
44+
<label class="control-label">Description:</label>
45+
<div>
46+
<textarea name="description" class="form-control" rows="3">{{ old('description') }}</textarea>
47+
</div>
48+
</div>
49+
</div>
50+
<div class="row">
51+
<div class="col-md-3 form-group">
52+
<label class="control-label">Tag:</label>
53+
<div>
54+
<input type="text" name="tag" value="{{ old('tag') }}" class="form-control" />
55+
</div>
56+
</div>
57+
<div class="col-md-3 form-group">
58+
<label class="control-label">Executable:</label>
59+
<div>
60+
<input type="text" name="executable" value="{{ old('executable') }}" class="form-control" />
61+
<p class="text-muted"><small>Leave blank to use parent executable.</small></p>
62+
</div>
63+
</div>
64+
<div class="col-md-6 form-group">
65+
<label class="control-label">Docker Image:</label>
66+
<div>
67+
<input type="text" name="docker_image" value="{{ old('docker_image') }}" class="form-control" />
68+
</div>
69+
</div>
70+
</div>
71+
<div class="row">
72+
<div class="col-md-12 form-group">
73+
<label class="control-label">Default Startup Command:</label>
74+
<div>
75+
<input type="text" name="startup" value="{{ old('startup') }}" class="form-control" />
76+
<p class="text-muted"><small>To use the default startup of the parent service simply leave this field blank.</small></p>
77+
</div>
78+
</div>
79+
</div>
80+
<div class="well well-sm">
81+
<div class="row">
82+
<div class="col-md-12">
83+
{!! csrf_field() !!}
84+
<input type="submit" class="btn btn-sm btn-primary" value="Create Service Option" />
85+
</div>
86+
</div>
87+
</div>
88+
</form>
89+
</div>
90+
<script>
91+
$(document).ready(function () {
92+
$('#sidebar_links').find("a[href='/admin/services']").addClass('active');
93+
$('#env_var').on('keyup', function () {
94+
$(this).parent().find('code').html('&#123;&#123;' + escape($(this).val()) + '&#125;&#125;');
95+
});
96+
});
97+
</script>
98+
@endsection

resources/views/admin/services/options/view.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
@extends('layouts.admin')
2121

2222
@section('title')
23-
Manage Services
23+
Manage Service Option {{ $option->name }}
2424
@endsection
2525

2626
@section('content')

resources/views/admin/services/view.blade.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
@extends('layouts.admin')
2121

2222
@section('title')
23-
Manage Services
23+
Manage Service
2424
@endsection
2525

2626
@section('content')
@@ -51,6 +51,13 @@
5151
<td class="text-center">{{ $option->c_servers }}</td>
5252
</tr>
5353
@endforeach
54+
<tr>
55+
<td></td>
56+
<td></td>
57+
<td></td>
58+
<td></td>
59+
<td class="text-center"><a href="{{ route('admin.services.option.new', $service->id) }}"><i class="fa fa-plus"></i></a></td>
60+
</tr>
5461
</tbody>
5562
</table>
5663
<div class="well">

0 commit comments

Comments
 (0)