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
|
|
|
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
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\Contracts\Repository\LocationRepositoryInterface;
|
2018-01-20 03:47:06 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Application\LocationTransformer;
|
2018-01-27 18:38:56 +00:00
|
|
|
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;
|
2019-01-26 23:26:15 +00:00
|
|
|
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
|
|
|
|
2018-01-27 18:38:56 +00:00
|
|
|
class LocationController extends ApplicationApiController
|
2018-01-04 03:14:53 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Locations\LocationCreationService
|
|
|
|
*/
|
|
|
|
private $creationService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Locations\LocationDeletionService
|
|
|
|
*/
|
|
|
|
private $deletionService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Locations\LocationUpdateService
|
|
|
|
*/
|
|
|
|
private $updateService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LocationController constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Services\Locations\LocationCreationService $creationService
|
|
|
|
* @param \Pterodactyl\Services\Locations\LocationDeletionService $deletionService
|
2018-01-04 03:14:53 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $repository
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Services\Locations\LocationUpdateService $updateService
|
2018-01-04 03:14:53 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
LocationCreationService $creationService,
|
|
|
|
LocationDeletionService $deletionService,
|
|
|
|
LocationRepositoryInterface $repository,
|
|
|
|
LocationUpdateService $updateService
|
|
|
|
) {
|
2018-01-27 18:38:56 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
2018-01-04 03:14:53 +00:00
|
|
|
$this->creationService = $creationService;
|
|
|
|
$this->deletionService = $deletionService;
|
|
|
|
$this->repository = $repository;
|
|
|
|
$this->updateService = $updateService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the locations currently registered on the Panel.
|
|
|
|
*
|
2018-01-20 01:58:57 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationsRequest $request
|
2018-01-04 03:14:53 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-13 02:39:15 +00:00
|
|
|
public function index(GetLocationsRequest $request): array
|
2018-01-04 03:14:53 +00:00
|
|
|
{
|
2020-09-13 18:59:52 +00:00
|
|
|
$locations = QueryBuilder::for(Location::query())
|
|
|
|
->allowedFilters(['short', 'long'])
|
|
|
|
->allowedSorts(['id'])
|
|
|
|
->paginate(100);
|
2018-01-04 03:14:53 +00:00
|
|
|
|
|
|
|
return $this->fractal->collection($locations)
|
2018-01-27 18:38:56 +00:00
|
|
|
->transformWith($this->getTransformer(LocationTransformer::class))
|
2018-01-04 03:14:53 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a single location.
|
|
|
|
*
|
2018-09-03 22:59:30 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationRequest $request
|
2018-01-04 03:14:53 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-27 18:38:56 +00:00
|
|
|
public function view(GetLocationRequest $request): array
|
2018-01-04 03:14:53 +00:00
|
|
|
{
|
2018-01-27 18:38:56 +00:00
|
|
|
return $this->fractal->item($request->getModel(Location::class))
|
|
|
|
->transformWith($this->getTransformer(LocationTransformer::class))
|
2018-01-04 03:14:53 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-13 02:39:15 +00:00
|
|
|
* Store a new location on the Panel and return a HTTP/201 response code with the
|
|
|
|
* new location attached.
|
|
|
|
*
|
2018-11-20 05:04:05 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Locations\StoreLocationRequest $request
|
2018-01-04 03:14:53 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
*/
|
2018-01-13 02:39:15 +00:00
|
|
|
public function store(StoreLocationRequest $request): JsonResponse
|
2018-01-04 03:14:53 +00:00
|
|
|
{
|
2018-01-13 02:39:15 +00:00
|
|
|
$location = $this->creationService->handle($request->validated());
|
2018-01-04 03:14:53 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($location)
|
2018-01-27 18:38:56 +00:00
|
|
|
->transformWith($this->getTransformer(LocationTransformer::class))
|
|
|
|
->addMeta([
|
|
|
|
'resource' => route('api.application.locations.view', [
|
|
|
|
'location' => $location->id,
|
|
|
|
]),
|
|
|
|
])
|
2018-01-04 03:14:53 +00:00
|
|
|
->respond(201);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-13 02:39:15 +00:00
|
|
|
* Update a location on the Panel and return the updated record to the user.
|
|
|
|
*
|
2018-01-20 01:58:57 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Locations\UpdateLocationRequest $request
|
2018-01-04 03:14:53 +00:00
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2018-01-27 18:38:56 +00:00
|
|
|
public function update(UpdateLocationRequest $request): array
|
2018-01-04 03:14:53 +00:00
|
|
|
{
|
2018-01-27 18:38:56 +00:00
|
|
|
$location = $this->updateService->handle($request->getModel(Location::class), $request->validated());
|
2018-01-04 03:14:53 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($location)
|
2018-01-27 18:38:56 +00:00
|
|
|
->transformWith($this->getTransformer(LocationTransformer::class))
|
2018-01-04 03:14:53 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-13 02:39:15 +00:00
|
|
|
* Delete a location from the Panel.
|
|
|
|
*
|
2018-01-20 01:58:57 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Locations\DeleteLocationRequest $request
|
2018-01-04 03:14:53 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException
|
|
|
|
*/
|
2018-01-27 18:38:56 +00:00
|
|
|
public function delete(DeleteLocationRequest $request): Response
|
2018-01-04 03:14:53 +00:00
|
|
|
{
|
2018-01-27 18:38:56 +00:00
|
|
|
$this->deletionService->handle($request->getModel(Location::class));
|
2018-01-04 03:14:53 +00:00
|
|
|
|
|
|
|
return response('', 204);
|
|
|
|
}
|
|
|
|
}
|