Skip to content

Commit 323f1d9

Browse files
committed
Completed model updates for Services
1 parent 09d23de commit 323f1d9

26 files changed

+299
-167
lines changed

app/Http/Controllers/API/ServiceController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function view(Request $request, $id)
5353
return [
5454
'service' => $service,
5555
'options' => Models\ServiceOptions::select('id', 'name', 'description', 'tag', 'docker_image')
56-
->where('parent_service', $service->id)
56+
->where('service_id', $service->id)
5757
->with('variables')
5858
->with('packs')
5959
->get(),

app/Http/Controllers/Admin/PackController.php

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -42,77 +42,48 @@ public function __construct()
4242
//
4343
}
4444

45-
protected function formatServices()
46-
{
47-
$options = Models\ServiceOptions::select(
48-
'services.name AS p_service',
49-
'service_options.id',
50-
'service_options.name'
51-
)->join('services', 'services.id', '=', 'service_options.parent_service')->get();
52-
53-
$array = [];
54-
foreach ($options as &$option) {
55-
if (! array_key_exists($option->p_service, $array)) {
56-
$array[$option->p_service] = [];
57-
}
58-
59-
$array[$option->p_service] = array_merge($array[$option->p_service], [[
60-
'id' => $option->id,
61-
'name' => $option->name,
62-
]]);
63-
}
64-
65-
return $array;
66-
}
67-
6845
public function listAll(Request $request)
6946
{
70-
return view('admin.services.packs.index', [
71-
'services' => Models\Service::all(),
72-
]);
47+
return view('admin.services.packs.index', ['services' => Models\Service::all()]);
7348
}
7449

7550
public function listByOption(Request $request, $id)
7651
{
77-
$option = Models\ServiceOptions::findOrFail($id);
78-
7952
return view('admin.services.packs.byoption', [
80-
'packs' => Models\ServicePack::where('option', $option->id)->get(),
81-
'service' => Models\Service::findOrFail($option->parent_service),
82-
'option' => $option,
53+
'option' => Models\ServiceOptions::with('service', 'packs')->findOrFail($id)
8354
]);
8455
}
8556

8657
public function listByService(Request $request, $id)
8758
{
8859
return view('admin.services.packs.byservice', [
89-
'service' => Models\Service::findOrFail($id),
90-
'options' => Models\ServiceOptions::select(
91-
'service_options.id',
92-
'service_options.name',
93-
DB::raw('(SELECT COUNT(id) FROM service_packs WHERE service_packs.option = service_options.id) AS p_count')
94-
)->where('parent_service', $id)->get(),
60+
'service' => Models\Service::with('options', 'options.packs')->findOrFail($id),
9561
]);
9662
}
9763

9864
public function new(Request $request, $opt = null)
9965
{
10066
return view('admin.services.packs.new', [
101-
'services' => $this->formatServices(),
102-
'packFor' => $opt,
67+
'services' => Models\Service::with('options')->get(),
10368
]);
10469
}
10570

10671
public function create(Request $request)
10772
{
10873
try {
10974
$repo = new Pack;
110-
$id = $repo->create($request->except([
111-
'_token',
75+
$pack = $repo->create($request->only([
76+
'name',
77+
'version',
78+
'description',
79+
'option',
80+
'selectable',
81+
'visible',
82+
'file_upload',
11283
]));
11384
Alert::success('Successfully created new service!')->flash();
11485

115-
return redirect()->route('admin.services.packs.edit', $id)->withInput();
86+
return redirect()->route('admin.services.packs.edit', $pack->id)->withInput();
11687
} catch (DisplayValidationException $ex) {
11788
return redirect()->route('admin.services.packs.new', $request->input('option'))->withErrors(json_decode($ex->getMessage()))->withInput();
11889
} catch (DisplayException $ex) {
@@ -127,15 +98,12 @@ public function create(Request $request)
12798

12899
public function edit(Request $request, $id)
129100
{
130-
$pack = Models\ServicePack::findOrFail($id);
131-
$option = Models\ServiceOptions::select('id', 'parent_service', 'name')->where('id', $pack->option)->first();
101+
$pack = Models\ServicePack::with('option.service')->findOrFail($id);
132102

133103
return view('admin.services.packs.edit', [
134104
'pack' => $pack,
135-
'services' => $this->formatServices(),
105+
'services' => Models\Service::all()->load('options'),
136106
'files' => Storage::files('packs/' . $pack->uuid),
137-
'service' => Models\Service::findOrFail($option->parent_service),
138-
'option' => $option,
139107
]);
140108
}
141109

@@ -159,8 +127,13 @@ public function update(Request $request, $id)
159127
} else {
160128
try {
161129
$repo = new Pack;
162-
$repo->update($id, $request->except([
163-
'_token',
130+
$repo->update($id, $request->only([
131+
'name',
132+
'version',
133+
'description',
134+
'option',
135+
'selectable',
136+
'visible',
164137
]));
165138
Alert::success('Service pack has been successfully updated.')->flash();
166139
} catch (DisplayValidationException $ex) {
@@ -215,21 +188,18 @@ public function export(Request $request, $id, $files = false)
215188
public function uploadForm(Request $request, $for = null)
216189
{
217190
return view('admin.services.packs.upload', [
218-
'services' => $this->formatServices(),
219-
'for' => $for,
191+
'services' => Models\Service::all()->load('options'),
220192
]);
221193
}
222194

223195
public function postUpload(Request $request)
224196
{
225197
try {
226198
$repo = new Pack;
227-
$id = $repo->createWithTemplate($request->except([
228-
'_token',
229-
]));
199+
$pack = $repo->createWithTemplate($request->only(['option', 'file_upload']));
230200
Alert::success('Successfully created new service!')->flash();
231201

232-
return redirect()->route('admin.services.packs.edit', $id)->withInput();
202+
return redirect()->route('admin.services.packs.edit', $pack->id)->withInput();
233203
} catch (DisplayValidationException $ex) {
234204
return redirect()->back()->withErrors(json_decode($ex->getMessage()))->withInput();
235205
} catch (DisplayException $ex) {

app/Http/Controllers/Admin/ServersController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public function postNewServerServiceOptions(Request $request)
244244

245245
$service = Models\Service::select('executable', 'startup')->where('id', $request->input('service'))->first();
246246

247-
return response()->json(Models\ServiceOptions::select('id', 'name', 'docker_image')->where('parent_service', $request->input('service'))->orderBy('name', 'asc')->get());
247+
return response()->json(Models\ServiceOptions::select('id', 'name', 'docker_image')->where('service_id', $request->input('service'))->orderBy('name', 'asc')->get());
248248
}
249249

250250
/**
@@ -264,7 +264,7 @@ public function postNewServerOptionDetails(Request $request)
264264
$option = Models\ServiceOptions::select(
265265
DB::raw('COALESCE(service_options.executable, services.executable) as executable'),
266266
DB::raw('COALESCE(service_options.startup, services.startup) as startup')
267-
)->leftJoin('services', 'services.id', '=', 'service_options.parent_service')
267+
)->leftJoin('services', 'services.id', '=', 'service_options.service_id')
268268
->where('service_options.id', $request->input('option'))
269269
->first();
270270

app/Http/Controllers/Admin/ServiceController.php

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ public function __construct()
4545
public function getIndex(Request $request)
4646
{
4747
return view('admin.services.index', [
48-
'services' => Models\Service::select(
49-
'services.*',
50-
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.service = services.id) as c_servers')
51-
)->get(),
48+
'services' => Models\Service::withCount('servers')->get(),
5249
]);
5350
}
5451

@@ -61,12 +58,16 @@ public function postNew(Request $request)
6158
{
6259
try {
6360
$repo = new ServiceRepository\Service;
64-
$id = $repo->create($request->except([
65-
'_token',
61+
$service = $repo->create($request->only([
62+
'name',
63+
'description',
64+
'file',
65+
'executable',
66+
'startup',
6667
]));
6768
Alert::success('Successfully created new service!')->flash();
6869

69-
return redirect()->route('admin.services.service', $id);
70+
return redirect()->route('admin.services.service', $service->id);
7071
} catch (DisplayValidationException $ex) {
7172
return redirect()->route('admin.services.new')->withErrors(json_decode($ex->getMessage()))->withInput();
7273
} catch (DisplayException $ex) {
@@ -82,20 +83,20 @@ public function postNew(Request $request)
8283
public function getService(Request $request, $service)
8384
{
8485
return view('admin.services.view', [
85-
'service' => Models\Service::findOrFail($service),
86-
'options' => Models\ServiceOptions::select(
87-
'service_options.*',
88-
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.option = service_options.id) as c_servers')
89-
)->where('parent_service', $service)->get(),
86+
'service' => Models\Service::with('options', 'options.servers')->findOrFail($service),
9087
]);
9188
}
9289

9390
public function postService(Request $request, $service)
9491
{
9592
try {
9693
$repo = new ServiceRepository\Service;
97-
$repo->update($service, $request->except([
98-
'_token',
94+
$repo->update($service, $request->only([
95+
'name',
96+
'description',
97+
'file',
98+
'executable',
99+
'startup',
99100
]));
100101
Alert::success('Successfully updated this service.')->flash();
101102
} catch (DisplayValidationException $ex) {
@@ -130,25 +131,25 @@ public function deleteService(Request $request, $service)
130131

131132
public function getOption(Request $request, $service, $option)
132133
{
133-
$opt = Models\ServiceOptions::findOrFail($option);
134+
$option = Models\ServiceOptions::with('service', 'variables')->findOrFail($option);
135+
$option->setRelation('servers', $option->servers()->with('user')->paginate(25));
134136

135137
return view('admin.services.options.view', [
136-
'service' => Models\Service::findOrFail($opt->parent_service),
137-
'option' => $opt,
138-
'variables' => Models\ServiceVariables::where('option_id', $option)->get(),
139-
'servers' => Models\Server::select('servers.*', 'users.email as a_ownerEmail')
140-
->join('users', 'users.id', '=', 'servers.owner_id')
141-
->where('option', $option)
142-
->paginate(10),
138+
'option' => $option,
143139
]);
144140
}
145141

146142
public function postOption(Request $request, $service, $option)
147143
{
148144
try {
149145
$repo = new ServiceRepository\Option;
150-
$repo->update($option, $request->except([
151-
'_token',
146+
$repo->update($option, $request->only([
147+
'name',
148+
'description',
149+
'tag',
150+
'executable',
151+
'docker_image',
152+
'startup',
152153
]));
153154
Alert::success('Option settings successfully updated.')->flash();
154155
} catch (DisplayValidationException $ex) {
@@ -164,13 +165,12 @@ public function postOption(Request $request, $service, $option)
164165
public function deleteOption(Request $request, $service, $option)
165166
{
166167
try {
167-
$service = Models\ServiceOptions::select('parent_service')->where('id', $option)->first();
168168
$repo = new ServiceRepository\Option;
169169
$repo->delete($option);
170170

171171
Alert::success('Successfully deleted that option.')->flash();
172172

173-
return redirect()->route('admin.services.service', $service->parent_service);
173+
return redirect()->route('admin.services.service', $service);
174174
} catch (DisplayException $ex) {
175175
Alert::danger($ex->getMessage())->flash();
176176
} catch (\Exception $ex) {
@@ -218,17 +218,23 @@ public function postOptionVariable(Request $request, $service, $option, $variabl
218218
public function getNewVariable(Request $request, $service, $option)
219219
{
220220
return view('admin.services.options.variable', [
221-
'service' => Models\Service::findOrFail($service),
222-
'option' => Models\ServiceOptions::where('parent_service', $service)->where('id', $option)->firstOrFail(),
221+
'option' => Models\ServiceOptions::with('service')->findOrFail($option),
223222
]);
224223
}
225224

226225
public function postNewVariable(Request $request, $service, $option)
227226
{
228227
try {
229228
$repo = new ServiceRepository\Variable;
230-
$repo->create($option, $request->except([
231-
'_token',
229+
$repo->create($option, $request->only([
230+
'name',
231+
'description',
232+
'env_variable',
233+
'default_value',
234+
'user_viewable',
235+
'user_editable',
236+
'required',
237+
'regex',
232238
]));
233239
Alert::success('Successfully added new variable to this option.')->flash();
234240

@@ -305,8 +311,9 @@ public function postConfiguration(Request $request, $serviceId)
305311
{
306312
try {
307313
$repo = new ServiceRepository\Service;
308-
$repo->updateFile($serviceId, $request->except([
309-
'_token',
314+
$repo->updateFile($serviceId, $request->only([
315+
'file',
316+
'contents',
310317
]));
311318

312319
return response('', 204);

app/Http/Controllers/Server/ServerController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public function getStartup(Request $request, $uuid)
224224

225225
$service = Models\Service::select(
226226
DB::raw('IFNULL(service_options.executable, services.executable) as executable')
227-
)->leftJoin('service_options', 'service_options.parent_service', '=', 'services.id')
227+
)->leftJoin('service_options', 'service_options.service_id', '=', 'services.id')
228228
->where('service_options.id', $server->option_id)
229229
->where('services.id', $server->service_id)
230230
->first();

app/Models/Service.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,38 @@ class Service extends Model
4040
*
4141
* @var array
4242
*/
43-
protected $guarded = ['id', 'created_at', 'updated_at'];
43+
protected $fillable = ['id', 'created_at', 'updated_at'];
44+
45+
/**
46+
* Gets all service options associated with this service.
47+
*
48+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
49+
*/
50+
public function options()
51+
{
52+
return $this->hasMany(ServiceOptions::class);
53+
}
54+
55+
/**
56+
* Returns all of the packs associated with a service, regardless of the service option.
57+
*
58+
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
59+
*/
60+
public function packs()
61+
{
62+
return $this->hasManyThrough(
63+
'Pterodactyl\Models\ServicePack', 'Pterodactyl\Models\ServiceOptions',
64+
'service_id', 'option_id'
65+
);
66+
}
67+
68+
/**
69+
* Gets all servers associated with this service.
70+
*
71+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
72+
*/
73+
public function servers()
74+
{
75+
return $this->hasMany(Server::class);
76+
}
4477
}

0 commit comments

Comments
 (0)