diff --git a/app/Http/Controllers/API/Admin/Locations/LocationController.php b/app/Http/Controllers/API/Admin/Locations/LocationController.php new file mode 100644 index 000000000..a78ddcb5e --- /dev/null +++ b/app/Http/Controllers/API/Admin/Locations/LocationController.php @@ -0,0 +1,147 @@ +creationService = $creationService; + $this->deletionService = $deletionService; + $this->fractal = $fractal; + $this->repository = $repository; + $this->updateService = $updateService; + } + + /** + * Return all of the locations currently registered on the Panel. + * + * @param \Illuminate\Http\Request $request + * @return array + */ + public function index(Request $request): array + { + $locations = $this->repository->all(50); + + return $this->fractal->collection($locations) + ->transformWith(new LocationTransformer($request)) + ->withResourceName('location') + ->paginateWith(new IlluminatePaginatorAdapter($locations)) + ->toArray(); + } + + /** + * Return a single location. + * + * @param \Illuminate\Http\Request $request + * @param \Pterodactyl\Models\Location $location + * @return array + */ + public function view(Request $request, Location $location): array + { + return $this->fractal->item($location) + ->transformWith(new LocationTransformer($request)) + ->withResourceName('location') + ->toArray(); + } + + /** + * @param \Pterodactyl\Http\Requests\Admin\LocationFormRequest $request + * @return \Illuminate\Http\JsonResponse + * + * @throws \Pterodactyl\Exceptions\Model\DataValidationException + */ + public function store(LocationFormRequest $request): JsonResponse + { + $location = $this->creationService->handle($request->normalize()); + + return $this->fractal->item($location) + ->transformWith(new LocationTransformer($request)) + ->withResourceName('location') + ->respond(201); + } + + /** + * @param \Pterodactyl\Http\Requests\Admin\LocationFormRequest $request + * @param \Pterodactyl\Models\Location $location + * @return array + * + * @throws \Pterodactyl\Exceptions\Model\DataValidationException + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + */ + public function update(LocationFormRequest $request, Location $location): array + { + $location = $this->updateService->handle($location, $request->normalize()); + + return $this->fractal->item($location) + ->transformWith(new LocationTransformer($request)) + ->withResourceName('location') + ->toArray(); + } + + /** + * @param \Pterodactyl\Models\Location $location + * @return \Illuminate\Http\Response + * + * @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException + */ + public function delete(Location $location): Response + { + $this->deletionService->handle($location); + + return response('', 204); + } +} diff --git a/app/Http/Controllers/API/Admin/Users/UserController.php b/app/Http/Controllers/API/Admin/Users/UserController.php index 54cdaae0a..a1b076411 100644 --- a/app/Http/Controllers/API/Admin/Users/UserController.php +++ b/app/Http/Controllers/API/Admin/Users/UserController.php @@ -78,16 +78,11 @@ class UserController extends Controller { $users = $this->repository->all(config('pterodactyl.paginate.api.users')); - $fractal = $this->fractal->collection($users) + return $this->fractal->collection($users) ->transformWith(new UserTransformer($request)) ->withResourceName('user') - ->paginateWith(new IlluminatePaginatorAdapter($users)); - - if (config('pterodactyl.api.include_on_list') && $request->filled('include')) { - $fractal->parseIncludes(explode(',', $request->input('include'))); - } - - return $fractal->toArray(); + ->paginateWith(new IlluminatePaginatorAdapter($users)) + ->toArray(); } /** @@ -100,15 +95,10 @@ class UserController extends Controller */ public function view(Request $request, User $user): array { - $fractal = $this->fractal->item($user) + return $this->fractal->item($user) ->transformWith(new UserTransformer($request)) - ->withResourceName('user'); - - if ($request->filled('include')) { - $fractal->parseIncludes(explode(',', $request->input('include'))); - } - - return $fractal->toArray(); + ->withResourceName('user') + ->toArray(); } /** diff --git a/app/Services/Locations/LocationDeletionService.php b/app/Services/Locations/LocationDeletionService.php index 34dab547a..5d1495d16 100644 --- a/app/Services/Locations/LocationDeletionService.php +++ b/app/Services/Locations/LocationDeletionService.php @@ -10,6 +10,7 @@ namespace Pterodactyl\Services\Locations; use Webmozart\Assert\Assert; +use Pterodactyl\Models\Location; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Exceptions\Service\Location\HasActiveNodesException; @@ -43,15 +44,16 @@ class LocationDeletionService /** * Delete an existing location. * - * @param int $location + * @param int|\Pterodactyl\Models\Location $location * @return int|null * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException */ public function handle($location) { - Assert::integerish($location, 'First argument passed to handle must be numeric, received %s.'); + $location = ($location instanceof Location) ? $location->id : $location; + + Assert::integerish($location, 'First argument passed to handle must be numeric or an instance of ' . Location::class . ', received %s.'); $count = $this->nodeRepository->findCountWhere([['location_id', '=', $location]]); if ($count > 0) { diff --git a/routes/api-admin.php b/routes/api-admin.php index 13e45a7c4..b4160122e 100644 --- a/routes/api-admin.php +++ b/routes/api-admin.php @@ -35,3 +35,21 @@ Route::group(['prefix' => '/nodes'], function () { Route::delete('/{node}', 'Nodes\NodeController@delete')->name('api.admin.node.delete'); }); + +/* +|-------------------------------------------------------------------------- +| Location Controller Routes +|-------------------------------------------------------------------------- +| +| Endpoint: /api/admin/locations +| +*/ +Route::group(['prefix' => '/locations'], function () { + Route::get('/', 'Locations\LocationController@index')->name('api.admin.location.list'); + Route::get('/{location}', 'Locations\LocationController@view')->name('api.admin.location.view'); + + Route::post('/', 'Locations\LocationController@store')->name('api.admin.location.store'); + Route::patch('/{location}', 'Locations\LocationController@update')->name('api.admin.location.update'); + + Route::delete('/{location}', 'Locations\LocationController@delete')->name('api.admin.location.delete'); +});