Path: blob/1.0-develop/app/Http/Controllers/Api/Application/Locations/LocationController.php
10277 views
<?php12namespace Pterodactyl\Http\Controllers\Api\Application\Locations;34use Illuminate\Http\Response;5use Pterodactyl\Models\Location;6use Illuminate\Http\JsonResponse;7use Spatie\QueryBuilder\QueryBuilder;8use Pterodactyl\Services\Locations\LocationUpdateService;9use Pterodactyl\Services\Locations\LocationCreationService;10use Pterodactyl\Services\Locations\LocationDeletionService;11use Pterodactyl\Transformers\Api\Application\LocationTransformer;12use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;13use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationRequest;14use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationsRequest;15use Pterodactyl\Http\Requests\Api\Application\Locations\StoreLocationRequest;16use Pterodactyl\Http\Requests\Api\Application\Locations\DeleteLocationRequest;17use Pterodactyl\Http\Requests\Api\Application\Locations\UpdateLocationRequest;1819class LocationController extends ApplicationApiController20{21/**22* LocationController constructor.23*/24public function __construct(25private LocationCreationService $creationService,26private LocationDeletionService $deletionService,27private LocationUpdateService $updateService,28) {29parent::__construct();30}3132/**33* Return all the locations currently registered on the Panel.34*/35public function index(GetLocationsRequest $request): array36{37$locations = QueryBuilder::for(Location::query())38->allowedFilters(['short', 'long'])39->allowedSorts(['id'])40->paginate($request->query('per_page') ?? 50);4142return $this->fractal->collection($locations)43->transformWith($this->getTransformer(LocationTransformer::class))44->toArray();45}4647/**48* Return a single location.49*/50public function view(GetLocationRequest $request, Location $location): array51{52return $this->fractal->item($location)53->transformWith($this->getTransformer(LocationTransformer::class))54->toArray();55}5657/**58* Store a new location on the Panel and return an HTTP/201 response code with the59* new location attached.60*61* @throws \Pterodactyl\Exceptions\Model\DataValidationException62*/63public function store(StoreLocationRequest $request): JsonResponse64{65$location = $this->creationService->handle($request->validated());6667return $this->fractal->item($location)68->transformWith($this->getTransformer(LocationTransformer::class))69->addMeta([70'resource' => route('api.application.locations.view', [71'location' => $location->id,72]),73])74->respond(201);75}7677/**78* Update a location on the Panel and return the updated record to the user.79*80* @throws \Pterodactyl\Exceptions\Model\DataValidationException81* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException82*/83public function update(UpdateLocationRequest $request, Location $location): array84{85$location = $this->updateService->handle($location, $request->validated());8687return $this->fractal->item($location)88->transformWith($this->getTransformer(LocationTransformer::class))89->toArray();90}9192/**93* Delete a location from the Panel.94*95* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException96*/97public function delete(DeleteLocationRequest $request, Location $location): Response98{99$this->deletionService->handle($location);100101return response('', 204);102}103}104105106