misc_pterodactyl-panel/app/Http/Controllers/Api/Application/Locations/LocationController.php

120 lines
4.2 KiB
PHP
Raw Normal View History

2018-01-04 03:14:53 +00:00
<?php
2018-01-20 01:58:57 +00:00
namespace Pterodactyl\Http\Controllers\Api\Application\Locations;
2018-01-04 03:14:53 +00:00
2021-03-05 17:03:12 +00:00
use Illuminate\Http\Response;
2018-01-04 03:14:53 +00:00
use Pterodactyl\Models\Location;
use Illuminate\Http\JsonResponse;
2020-09-13 18:59:52 +00:00
use Spatie\QueryBuilder\QueryBuilder;
2018-01-04 03:14:53 +00:00
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;
2018-09-03 22:59:30 +00:00
use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationRequest;
2018-01-20 01:58:57 +00:00
use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationsRequest;
2018-11-20 05:04:05 +00:00
use Pterodactyl\Http\Requests\Api\Application\Locations\StoreLocationRequest;
use Pterodactyl\Http\Requests\Api\Application\Locations\DeleteLocationRequest;
2018-01-20 01:58:57 +00:00
use Pterodactyl\Http\Requests\Api\Application\Locations\UpdateLocationRequest;
2018-01-04 03:14:53 +00:00
class LocationController extends ApplicationApiController
2018-01-04 03:14:53 +00:00
{
private LocationCreationService $creationService;
private LocationDeletionService $deletionService;
private LocationUpdateService $updateService;
2018-01-04 03:14:53 +00:00
/**
* LocationController constructor.
*/
public function __construct(
LocationCreationService $creationService,
LocationDeletionService $deletionService,
LocationUpdateService $updateService
2018-01-04 03:14:53 +00:00
) {
parent::__construct();
2018-01-04 03:14:53 +00:00
$this->creationService = $creationService;
$this->deletionService = $deletionService;
$this->updateService = $updateService;
}
/**
* Return all of the locations currently registered on the Panel.
2021-03-05 17:03:12 +00:00
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
2018-01-04 03:14:53 +00:00
*/
public function index(GetLocationsRequest $request): array
2018-01-04 03:14:53 +00:00
{
$perPage = (int) $request->query('per_page', '10');
if ($perPage < 1 || $perPage > 100) {
throw new QueryValueOutOfRangeHttpException('per_page', 1, 100);
}
2020-09-13 18:59:52 +00:00
$locations = QueryBuilder::for(Location::query())
->allowedFilters(['short', 'long'])
->allowedSorts(['id', 'short', 'long'])
->paginate($perPage);
2018-01-04 03:14:53 +00:00
return $this->fractal->collection($locations)
2021-08-07 21:32:40 +00:00
->transformWith(LocationTransformer::class)
2018-01-04 03:14:53 +00:00
->toArray();
}
/**
* Return a single location.
2021-03-05 17:03:12 +00:00
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
2018-01-04 03:14:53 +00:00
*/
public function view(GetLocationRequest $request, Location $location): array
2018-01-04 03:14:53 +00:00
{
return $this->fractal->item($location)
2021-08-07 21:32:40 +00:00
->transformWith(LocationTransformer::class)
2018-01-04 03:14:53 +00:00
->toArray();
}
/**
* Store a new location on the Panel and return a HTTP/201 response code with the
* new location attached.
*
2018-01-04 03:14:53 +00:00
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
2018-01-04 03:14:53 +00:00
*/
public function store(StoreLocationRequest $request): JsonResponse
2018-01-04 03:14:53 +00:00
{
$location = $this->creationService->handle($request->validated());
2018-01-04 03:14:53 +00:00
return $this->fractal->item($location)
2021-08-07 21:32:40 +00:00
->transformWith(LocationTransformer::class)
2018-01-04 03:14:53 +00:00
->respond(201);
}
/**
* Update a location on the Panel and return the updated record to the user.
*
2018-01-04 03:14:53 +00:00
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
2018-01-04 03:14:53 +00:00
*/
public function update(UpdateLocationRequest $request, Location $location): array
2018-01-04 03:14:53 +00:00
{
$location = $this->updateService->handle($location, $request->validated());
2018-01-04 03:14:53 +00:00
return $this->fractal->item($location)
2021-08-07 21:32:40 +00:00
->transformWith(LocationTransformer::class)
2018-01-04 03:14:53 +00:00
->toArray();
}
/**
* Delete a location from the Panel.
*
2018-01-04 03:14:53 +00:00
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException
*/
2021-03-05 17:03:12 +00:00
public function delete(DeleteLocationRequest $request, Location $location): Response
2018-01-04 03:14:53 +00:00
{
$this->deletionService->handle($location);
2018-01-04 03:14:53 +00:00
2021-03-05 17:03:12 +00:00
return $this->returnNoContent();
2018-01-04 03:14:53 +00:00
}
}