misc_pterodactyl-panel/app/Http/Controllers/Api/Application/Locations/LocationController.php
2022-12-14 17:05:46 -07:00

105 lines
3.6 KiB
PHP

<?php
namespace Pterodactyl\Http\Controllers\Api\Application\Locations;
use Illuminate\Http\Response;
use Pterodactyl\Models\Location;
use Illuminate\Http\JsonResponse;
use Spatie\QueryBuilder\QueryBuilder;
use Pterodactyl\Services\Locations\LocationUpdateService;
use Pterodactyl\Services\Locations\LocationCreationService;
use Pterodactyl\Services\Locations\LocationDeletionService;
use Pterodactyl\Transformers\Api\Application\LocationTransformer;
use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationRequest;
use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationsRequest;
use Pterodactyl\Http\Requests\Api\Application\Locations\StoreLocationRequest;
use Pterodactyl\Http\Requests\Api\Application\Locations\DeleteLocationRequest;
use Pterodactyl\Http\Requests\Api\Application\Locations\UpdateLocationRequest;
class LocationController extends ApplicationApiController
{
/**
* LocationController constructor.
*/
public function __construct(
private LocationCreationService $creationService,
private LocationDeletionService $deletionService,
private LocationUpdateService $updateService
) {
parent::__construct();
}
/**
* Return all the locations currently registered on the Panel.
*/
public function index(GetLocationsRequest $request): array
{
$perPage = (int) $request->query('per_page', '10');
if ($perPage < 1 || $perPage > 100) {
throw new QueryValueOutOfRangeHttpException('per_page', 1, 100);
}
$locations = QueryBuilder::for(Location::query())
->allowedFilters(['short', 'long'])
->allowedSorts(['id', 'short', 'long'])
->paginate($perPage);
return $this->fractal->collection($locations)
->transformWith(LocationTransformer::class)
->toArray();
}
/**
* Return a single location.
*/
public function view(GetLocationRequest $request, Location $location): array
{
return $this->fractal->item($location)
->transformWith(LocationTransformer::class)
->toArray();
}
/**
* Store a new location on the Panel and return an HTTP/201 response code with the
* new location attached.
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function store(StoreLocationRequest $request): JsonResponse
{
$location = $this->creationService->handle($request->validated());
return $this->fractal->item($location)
->transformWith(LocationTransformer::class)
->respond(201);
}
/**
* Update a location on the Panel and return the updated record to the user.
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function update(UpdateLocationRequest $request, Location $location): array
{
$location = $this->updateService->handle($location, $request->validated());
return $this->fractal->item($location)
->transformWith(LocationTransformer::class)
->toArray();
}
/**
* Delete a location from the Panel.
*
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException
*/
public function delete(DeleteLocationRequest $request, Location $location): Response
{
$this->deletionService->handle($location);
return $this->returnNoContent();
}
}