Skip to content

Commit a50bb5d

Browse files
committed
add ability to create new service
1 parent e42547a commit a50bb5d

File tree

4 files changed

+146
-2
lines changed

4 files changed

+146
-2
lines changed

app/Http/Controllers/Admin/ServiceController.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,27 @@ public function getIndex(Request $request)
5353

5454
public function getNew(Request $request)
5555
{
56-
//
56+
return view('admin.services.new');
5757
}
5858

5959
public function postNew(Request $request)
6060
{
61-
//
61+
try {
62+
$repo = new ServiceRepository\Service;
63+
$id = $repo->create($request->except([
64+
'_token'
65+
]));
66+
Alert::success('Successfully created new service!')->flash();
67+
return redirect()->route('admin.services.service', $id);
68+
} catch (DisplayValidationException $ex) {
69+
return redirect()->route('admin.services.new')->withErrors(json_decode($ex->getMessage()))->withInput();
70+
} catch (DisplayException $ex) {
71+
Alert::danger($ex->getMessage())->flash();
72+
} catch (\Exception $ex) {
73+
Log::error($ex);
74+
Alert::danger('An error occured while attempting to add a new service.')->flash();
75+
}
76+
return redirect()->route('admin.services.new')->withInput();
6277
}
6378

6479
public function getService(Request $request, $service)

app/Http/Routes/AdminRoutes.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,15 @@ public function map(Router $router) {
348348
'uses' => 'Admin\ServiceController@getIndex'
349349
]);
350350

351+
$router->get('/new', [
352+
'as' => 'admin.services.new',
353+
'uses' => 'Admin\ServiceController@getNew'
354+
]);
355+
356+
$router->post('/new', [
357+
'uses' => 'Admin\ServiceController@postNew'
358+
]);
359+
351360
$router->get('/service/{id}', [
352361
'as' => 'admin.services.service',
353362
'uses' => 'Admin\ServiceController@getService'

app/Repositories/ServiceRepository/Service.php

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

43+
public function create(array $data)
44+
{
45+
$validator = Validator::make($data, [
46+
'name' => 'required|string|min:1|max:255',
47+
'description' => 'required|string',
48+
'file' => 'required|regex:/^[\w.-]{1,50}$/',
49+
'executable' => 'required|max:255|regex:/^(.*)$/',
50+
'startup' => 'required|string'
51+
]);
52+
53+
if ($validator->fails()) {
54+
throw new DisplayValidationException($validator->errors());
55+
}
56+
57+
if (Models\Service::where('file', $data['file'])->first()) {
58+
throw new DisplayException('A service using that configuration file already exists on the system.');
59+
}
60+
61+
$service = new Models\Service;
62+
$service->fill($data);
63+
$service->save();
64+
65+
return $service->id;
66+
}
67+
4368
public function update($id, array $data)
4469
{
4570
$service = Models\Service::findOrFail($id);
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
Manage Services
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="{{ route('admin.services') }}">Services</a></li>
31+
<li class="active">New Service</li>
32+
</ul>
33+
<h3 class="nopad">Add New Service</h3><hr />
34+
<form action="{{ route('admin.services.new') }}" method="POST">
35+
<div class="row">
36+
<div class="col-md-6 form-group">
37+
<label class="control-label">Service Name:</label>
38+
<div>
39+
<input type="text" name="name" class="form-control" value="{{ old('name') }}" />
40+
<p class="text-muted"><small>This should be a descriptive category name that emcompasses all of the options within the service.</small></p>
41+
</div>
42+
</div>
43+
<div class="col-md-6 form-group">
44+
<label class="control-label">Service Description:</label>
45+
<div>
46+
<textarea name="description" class="form-control" rows="4">{{ old('description') }}</textarea>
47+
</div>
48+
</div>
49+
</div>
50+
<div class="row">
51+
<div class="col-md-6 form-group">
52+
<label class="control-label">Service Configuration File:</label>
53+
<div class="input-group">
54+
<span class="input-group-addon">/src/services/</span>
55+
<input type="text" name="file" class="form-control" value="{{ old('file') }}" />
56+
<span class="input-group-addon">/index.js</span>
57+
</div>
58+
<p class="text-muted"><small>This should be the name of the folder on the daemon that contains all of the service logic.</small></p>
59+
</div>
60+
<div class="col-md-6 form-group">
61+
<label class="control-label">Display Executable:</label>
62+
<div>
63+
<input type="text" name="executable" class="form-control" value="{{ old('executable') }}" />
64+
</div>
65+
<p class="text-muted"><small>Changing this has no effect on operation of the daemon, it is simply used for display purposes on the panel. This can be changed per-option.</small></p>
66+
</div>
67+
</div>
68+
<div class="row">
69+
<div class="col-md-12 form-group">
70+
<label class="control-label">Default Startup:</label>
71+
<div class="input-group">
72+
<span class="input-group-addon" id="disp_exec"></span>
73+
<input type="text" name="startup" class="form-control" value="{{ old('startup') }}" />
74+
</div>
75+
<p class="text-muted"><small>This is the default startup that will be used for all servers created using this service. This can be changed per-option.</small></p>
76+
</div>
77+
</div>
78+
<div class="row">
79+
<div class="col-md-12">
80+
<div class="alert alert-info">You will be able to add service options and variables once the service is created.</div>
81+
{!! csrf_field() !!}
82+
<input type="submit" class="btn btn-sm btn-primary" value="Add New Service" />
83+
</div>
84+
</div>
85+
</form>
86+
</div>
87+
<script>
88+
$(document).ready(function () {
89+
$('#sidebar_links').find("a[href='/admin/services/new']").addClass('active');
90+
$('input[name="executable"]').on('keyup', function() {
91+
$("#disp_exec").html(escape($(this).val()));
92+
});
93+
});
94+
</script>
95+
@endsection

0 commit comments

Comments
 (0)