Skip to content

Commit fe4977f

Browse files
committed
Update admin location routes and controller to use service
Needs tests written, uses new validation on model.
1 parent 26e476a commit fe4977f

File tree

7 files changed

+290
-57
lines changed

7 files changed

+290
-57
lines changed

app/Http/Controllers/Admin/LocationController.php

Lines changed: 79 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,96 +24,127 @@
2424

2525
namespace Pterodactyl\Http\Controllers\Admin;
2626

27-
use Log;
28-
use Alert;
29-
use Illuminate\Http\Request;
3027
use Pterodactyl\Models\Location;
31-
use Pterodactyl\Exceptions\DisplayException;
28+
use Prologue\Alerts\AlertsMessageBag;
29+
use Pterodactyl\Services\LocationService;
3230
use Pterodactyl\Http\Controllers\Controller;
33-
use Pterodactyl\Repositories\LocationRepository;
34-
use Pterodactyl\Exceptions\DisplayValidationException;
31+
use Pterodactyl\Exceptions\DisplayException;
32+
use Pterodactyl\Http\Requests\Admin\LocationRequest;
3533

3634
class LocationController extends Controller
3735
{
36+
/**
37+
* @var \Prologue\Alerts\AlertsMessageBag
38+
*/
39+
protected $alert;
40+
41+
/**
42+
* @var \Pterodactyl\Models\Location
43+
*/
44+
protected $location;
45+
46+
/**
47+
* @var \Pterodactyl\Services\LocationService
48+
*/
49+
protected $service;
50+
51+
/**
52+
* LocationController constructor.
53+
*
54+
* @param \Prologue\Alerts\AlertsMessageBag $alert
55+
* @param \Pterodactyl\Models\Location $location
56+
* @param \Pterodactyl\Services\LocationService $service
57+
*/
58+
public function __construct(AlertsMessageBag $alert, Location $location, LocationService $service)
59+
{
60+
$this->alert = $alert;
61+
$this->location = $location;
62+
$this->service = $service;
63+
}
64+
3865
/**
3966
* Return the location overview page.
4067
*
41-
* @param \Illuminate\Http\Request $request
4268
* @return \Illuminate\View\View
4369
*/
44-
public function index(Request $request)
70+
public function index()
4571
{
4672
return view('admin.locations.index', [
47-
'locations' => Location::withCount('nodes', 'servers')->get(),
73+
'locations' => $this->location->withCount('nodes', 'servers')->get(),
4874
]);
4975
}
5076

5177
/**
5278
* Return the location view page.
5379
*
54-
* @param \Illuminate\Http\Request $request
55-
* @param int $id
80+
* @param \Pterodactyl\Models\Location $location
5681
* @return \Illuminate\View\View
5782
*/
58-
public function view(Request $request, $id)
83+
public function view(Location $location)
5984
{
60-
return view('admin.locations.view', ['location' => Location::with('nodes.servers')->findOrFail($id)]);
85+
$location->load('nodes.servers');
86+
87+
return view('admin.locations.view', ['location' => $location]);
6188
}
6289

6390
/**
6491
* Handle request to create new location.
6592
*
66-
* @param \Illuminate\Http\Request $request
93+
* @param \Pterodactyl\Http\Requests\Admin\LocationRequest $request
6794
* @return \Illuminate\Http\RedirectResponse
95+
*
96+
* @throws \Throwable
97+
* @throws \Watson\Validating\ValidationException
6898
*/
69-
public function create(Request $request)
99+
public function create(LocationRequest $request)
70100
{
71-
$repo = new LocationRepository;
72-
73-
try {
74-
$location = $repo->create($request->intersect(['short', 'long']));
75-
Alert::success('Location was created successfully.')->flash();
76-
77-
return redirect()->route('admin.locations.view', $location->id);
78-
} catch (DisplayValidationException $ex) {
79-
return redirect()->route('admin.locations')->withErrors(json_decode($ex->getMessage()));
80-
} catch (\Exception $ex) {
81-
Log::error($ex);
82-
Alert::error('An unhandled exception occurred while processing this request. This error has been logged.')->flash();
83-
}
101+
$location = $this->service->create($request->normalize());
102+
$this->alert->success('Location was created successfully.')->flash();
84103

85-
return redirect()->route('admin.locations');
104+
return redirect()->route('admin.locations.view', $location->id);
86105
}
87106

88107
/**
89108
* Handle request to update or delete location.
90109
*
91-
* @param \Illuminate\Http\Request $request
92-
* @param int $id
110+
* @param \Pterodactyl\Http\Requests\Admin\LocationRequest $request
111+
* @param \Pterodactyl\Models\Location $location
93112
* @return \Illuminate\Http\RedirectResponse
113+
*
114+
* @throws \Throwable
115+
* @throws \Watson\Validating\ValidationException
94116
*/
95-
public function update(Request $request, $id)
117+
public function update(LocationRequest $request, Location $location)
96118
{
97-
$repo = new LocationRepository;
119+
if ($request->input('action') === 'delete') {
120+
return $this->delete($location);
121+
}
98122

123+
$this->service->update($location, $request->normalize());
124+
$this->alert->success('Location was updated successfully.')->flash();
125+
126+
return redirect()->route('admin.locations.view', $location->id);
127+
}
128+
129+
/**
130+
* Delete a location from the system.
131+
*
132+
* @param \Pterodactyl\Models\Location $location
133+
* @return \Illuminate\Http\RedirectResponse
134+
*
135+
* @throws \Exception
136+
* @throws \Pterodactyl\Exceptions\DisplayException
137+
*/
138+
public function delete(Location $location)
139+
{
99140
try {
100-
if ($request->input('action') !== 'delete') {
101-
$location = $repo->update($id, $request->intersect(['short', 'long']));
102-
Alert::success('Location was updated successfully.')->flash();
103-
} else {
104-
$repo->delete($id);
105-
106-
return redirect()->route('admin.locations');
107-
}
108-
} catch (DisplayValidationException $ex) {
109-
return redirect()->route('admin.locations.view', $id)->withErrors(json_decode($ex->getMessage()));
141+
$this->service->delete($location);
142+
143+
return redirect()->route('admin.locations');
110144
} catch (DisplayException $ex) {
111-
Alert::danger($ex->getMessage())->flash();
112-
} catch (\Exception $ex) {
113-
Log::error($ex);
114-
Alert::error('An unhandled exception occurred while processing this request. This error has been logged.')->flash();
145+
$this->alert->danger($ex->getMessage())->flash();
115146
}
116147

117-
return redirect()->route('admin.locations.view', $id);
148+
return redirect()->route('admin.locations.view', $location->id);
118149
}
119150
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Http\Requests\Admin;
26+
27+
use Pterodactyl\Models\Location;
28+
29+
class LocationRequest extends AdminFormRequest
30+
{
31+
/**
32+
* Setup the validation rules to use for these requests.
33+
*
34+
* @return array
35+
*/
36+
public function rules()
37+
{
38+
return app()->make(Location::class)->getRules();
39+
}
40+
}

app/Models/Location.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
namespace Pterodactyl\Models;
2626

2727
use Illuminate\Database\Eloquent\Model;
28+
use Watson\Validating\ValidatingTrait;
2829

2930
class Location extends Model
3031
{
32+
use ValidatingTrait;
33+
3134
/**
3235
* The table associated with the model.
3336
*
@@ -42,6 +45,16 @@ class Location extends Model
4245
*/
4346
protected $guarded = ['id', 'created_at', 'updated_at'];
4447

48+
/**
49+
* Validation rules to apply when attempting to save a model to the DB.
50+
*
51+
* @var array
52+
*/
53+
protected $rules = [
54+
'short' => 'required|string|between:1,60|unique:locations,short',
55+
'long' => 'required|string|between:1,255',
56+
];
57+
4558
/**
4659
* Gets the nodes in a specificed location.
4760
*

app/Services/LocationService.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Services;
26+
27+
use Pterodactyl\Models\Location;
28+
use Pterodactyl\Exceptions\DisplayException;
29+
30+
class LocationService
31+
{
32+
/**
33+
* @var \Pterodactyl\Models\Location
34+
*/
35+
protected $model;
36+
37+
/**
38+
* LocationService constructor.
39+
*
40+
* @param \Pterodactyl\Models\Location $location
41+
*/
42+
public function __construct(Location $location)
43+
{
44+
$this->model = $location;
45+
}
46+
47+
/**
48+
* Create the location in the database and return it.
49+
*
50+
* @param array $data
51+
* @return \Pterodactyl\Models\Location
52+
*
53+
* @throws \Throwable
54+
* @throws \Watson\Validating\ValidationException
55+
*/
56+
public function create(array $data)
57+
{
58+
$location = $this->model->fill($data);
59+
$location->saveOrFail();
60+
61+
return $location;
62+
}
63+
64+
/**
65+
* Update location model in the DB.
66+
*
67+
* @param \Pterodactyl\Models\Location $location
68+
* @param array $data
69+
* @return \Pterodactyl\Models\Location
70+
*
71+
* @throws \Throwable
72+
* @throws \Watson\Validating\ValidationException
73+
*/
74+
public function update(Location $location, array $data)
75+
{
76+
$location->fill($data)->saveOrFail();
77+
78+
return $location;
79+
}
80+
81+
/**
82+
* Delete a model from the DB.
83+
*
84+
* @param \Pterodactyl\Models\Location $location
85+
* @return bool
86+
*
87+
* @throws \Exception
88+
* @throws \Pterodactyl\Exceptions\DisplayException
89+
*/
90+
public function delete(Location $location)
91+
{
92+
if ($location->nodes()->count() > 0) {
93+
throw new DisplayException('Cannot delete a location that has nodes assigned to it.');
94+
}
95+
96+
return $location->delete();
97+
}
98+
}

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
],
1313
"require": {
1414
"php": ">=7.0.0",
15+
"ext-mbstring": "*",
16+
"ext-pdo_mysql": "*",
17+
"ext-zip": "*",
1518
"aws/aws-sdk-php": "3.26.5",
1619
"barryvdh/laravel-debugbar": "2.3.2",
1720
"daneeveritt/login-notifications": "1.0.0",
1821
"doctrine/dbal": "2.5.12",
1922
"edvinaskrucas/settings": "2.0.0",
20-
"ext-mbstring": "*",
21-
"ext-zip": "*",
22-
"ext-pdo_mysql": "*",
2323
"fideloper/proxy": "3.3.0",
2424
"guzzlehttp/guzzle": "6.2.3",
2525
"igaster/laravel-theme": "1.14.0",
@@ -35,6 +35,7 @@
3535
"prologue/alerts": "0.4.1",
3636
"s1lentium/iptools": "1.1.0",
3737
"spatie/laravel-fractal": "4.0.0",
38+
"watson/validating": "3.0.s1",
3839
"webpatser/laravel-uuid": "2.0.1"
3940
},
4041
"require-dev": {

0 commit comments

Comments
 (0)