Skip to content

Commit f32cee3

Browse files
committed
Add location control through API
1 parent 15289b7 commit f32cee3

File tree

4 files changed

+176
-19
lines changed

4 files changed

+176
-19
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\API\Admin\Locations;
4+
5+
use Spatie\Fractal\Fractal;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Http\Response;
8+
use Pterodactyl\Models\Location;
9+
use Illuminate\Http\JsonResponse;
10+
use Pterodactyl\Http\Controllers\Controller;
11+
use Pterodactyl\Http\Requests\Admin\LocationFormRequest;
12+
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
13+
use Pterodactyl\Services\Locations\LocationUpdateService;
14+
use Pterodactyl\Services\Locations\LocationCreationService;
15+
use Pterodactyl\Services\Locations\LocationDeletionService;
16+
use Pterodactyl\Transformers\Api\Admin\LocationTransformer;
17+
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
18+
19+
class LocationController extends Controller
20+
{
21+
/**
22+
* @var \Pterodactyl\Services\Locations\LocationCreationService
23+
*/
24+
private $creationService;
25+
26+
/**
27+
* @var \Pterodactyl\Services\Locations\LocationDeletionService
28+
*/
29+
private $deletionService;
30+
31+
/**
32+
* @var \Spatie\Fractal\Fractal
33+
*/
34+
private $fractal;
35+
36+
/**
37+
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
38+
*/
39+
private $repository;
40+
41+
/**
42+
* @var \Pterodactyl\Services\Locations\LocationUpdateService
43+
*/
44+
private $updateService;
45+
46+
/**
47+
* LocationController constructor.
48+
*
49+
* @param \Spatie\Fractal\Fractal $fractal
50+
* @param \Pterodactyl\Services\Locations\LocationCreationService $creationService
51+
* @param \Pterodactyl\Services\Locations\LocationDeletionService $deletionService
52+
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $repository
53+
* @param \Pterodactyl\Services\Locations\LocationUpdateService $updateService
54+
*/
55+
public function __construct(
56+
Fractal $fractal,
57+
LocationCreationService $creationService,
58+
LocationDeletionService $deletionService,
59+
LocationRepositoryInterface $repository,
60+
LocationUpdateService $updateService
61+
) {
62+
$this->creationService = $creationService;
63+
$this->deletionService = $deletionService;
64+
$this->fractal = $fractal;
65+
$this->repository = $repository;
66+
$this->updateService = $updateService;
67+
}
68+
69+
/**
70+
* Return all of the locations currently registered on the Panel.
71+
*
72+
* @param \Illuminate\Http\Request $request
73+
* @return array
74+
*/
75+
public function index(Request $request): array
76+
{
77+
$locations = $this->repository->all(50);
78+
79+
return $this->fractal->collection($locations)
80+
->transformWith(new LocationTransformer($request))
81+
->withResourceName('location')
82+
->paginateWith(new IlluminatePaginatorAdapter($locations))
83+
->toArray();
84+
}
85+
86+
/**
87+
* Return a single location.
88+
*
89+
* @param \Illuminate\Http\Request $request
90+
* @param \Pterodactyl\Models\Location $location
91+
* @return array
92+
*/
93+
public function view(Request $request, Location $location): array
94+
{
95+
return $this->fractal->item($location)
96+
->transformWith(new LocationTransformer($request))
97+
->withResourceName('location')
98+
->toArray();
99+
}
100+
101+
/**
102+
* @param \Pterodactyl\Http\Requests\Admin\LocationFormRequest $request
103+
* @return \Illuminate\Http\JsonResponse
104+
*
105+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
106+
*/
107+
public function store(LocationFormRequest $request): JsonResponse
108+
{
109+
$location = $this->creationService->handle($request->normalize());
110+
111+
return $this->fractal->item($location)
112+
->transformWith(new LocationTransformer($request))
113+
->withResourceName('location')
114+
->respond(201);
115+
}
116+
117+
/**
118+
* @param \Pterodactyl\Http\Requests\Admin\LocationFormRequest $request
119+
* @param \Pterodactyl\Models\Location $location
120+
* @return array
121+
*
122+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
123+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
124+
*/
125+
public function update(LocationFormRequest $request, Location $location): array
126+
{
127+
$location = $this->updateService->handle($location, $request->normalize());
128+
129+
return $this->fractal->item($location)
130+
->transformWith(new LocationTransformer($request))
131+
->withResourceName('location')
132+
->toArray();
133+
}
134+
135+
/**
136+
* @param \Pterodactyl\Models\Location $location
137+
* @return \Illuminate\Http\Response
138+
*
139+
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException
140+
*/
141+
public function delete(Location $location): Response
142+
{
143+
$this->deletionService->handle($location);
144+
145+
return response('', 204);
146+
}
147+
}

app/Http/Controllers/API/Admin/Users/UserController.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,11 @@ public function index(Request $request): array
7878
{
7979
$users = $this->repository->all(config('pterodactyl.paginate.api.users'));
8080

81-
$fractal = $this->fractal->collection($users)
81+
return $this->fractal->collection($users)
8282
->transformWith(new UserTransformer($request))
8383
->withResourceName('user')
84-
->paginateWith(new IlluminatePaginatorAdapter($users));
85-
86-
if (config('pterodactyl.api.include_on_list') && $request->filled('include')) {
87-
$fractal->parseIncludes(explode(',', $request->input('include')));
88-
}
89-
90-
return $fractal->toArray();
84+
->paginateWith(new IlluminatePaginatorAdapter($users))
85+
->toArray();
9186
}
9287

9388
/**
@@ -100,15 +95,10 @@ public function index(Request $request): array
10095
*/
10196
public function view(Request $request, User $user): array
10297
{
103-
$fractal = $this->fractal->item($user)
98+
return $this->fractal->item($user)
10499
->transformWith(new UserTransformer($request))
105-
->withResourceName('user');
106-
107-
if ($request->filled('include')) {
108-
$fractal->parseIncludes(explode(',', $request->input('include')));
109-
}
110-
111-
return $fractal->toArray();
100+
->withResourceName('user')
101+
->toArray();
112102
}
113103

114104
/**

app/Services/Locations/LocationDeletionService.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Pterodactyl\Services\Locations;
1111

1212
use Webmozart\Assert\Assert;
13+
use Pterodactyl\Models\Location;
1314
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
1415
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
1516
use Pterodactyl\Exceptions\Service\Location\HasActiveNodesException;
@@ -43,15 +44,16 @@ public function __construct(
4344
/**
4445
* Delete an existing location.
4546
*
46-
* @param int $location
47+
* @param int|\Pterodactyl\Models\Location $location
4748
* @return int|null
4849
*
49-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
5050
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException
5151
*/
5252
public function handle($location)
5353
{
54-
Assert::integerish($location, 'First argument passed to handle must be numeric, received %s.');
54+
$location = ($location instanceof Location) ? $location->id : $location;
55+
56+
Assert::integerish($location, 'First argument passed to handle must be numeric or an instance of ' . Location::class . ', received %s.');
5557

5658
$count = $this->nodeRepository->findCountWhere([['location_id', '=', $location]]);
5759
if ($count > 0) {

routes/api-admin.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,21 @@
3535

3636
Route::delete('/{node}', 'Nodes\NodeController@delete')->name('api.admin.node.delete');
3737
});
38+
39+
/*
40+
|--------------------------------------------------------------------------
41+
| Location Controller Routes
42+
|--------------------------------------------------------------------------
43+
|
44+
| Endpoint: /api/admin/locations
45+
|
46+
*/
47+
Route::group(['prefix' => '/locations'], function () {
48+
Route::get('/', 'Locations\LocationController@index')->name('api.admin.location.list');
49+
Route::get('/{location}', 'Locations\LocationController@view')->name('api.admin.location.view');
50+
51+
Route::post('/', 'Locations\LocationController@store')->name('api.admin.location.store');
52+
Route::patch('/{location}', 'Locations\LocationController@update')->name('api.admin.location.update');
53+
54+
Route::delete('/{location}', 'Locations\LocationController@delete')->name('api.admin.location.delete');
55+
});

0 commit comments

Comments
 (0)