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

157 lines
5.6 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
use Spatie\Fractal\Fractal;
use Illuminate\Http\Response;
use Pterodactyl\Models\Location;
use Illuminate\Http\JsonResponse;
use Pterodactyl\Http\Controllers\Controller;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use Pterodactyl\Services\Locations\LocationUpdateService;
use Pterodactyl\Services\Locations\LocationCreationService;
use Pterodactyl\Services\Locations\LocationDeletionService;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Transformers\Api\Application\LocationTransformer;
2018-01-20 01:58:57 +00:00
use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationsRequest;
use Pterodactyl\Http\Requests\Api\Application\Locations\DeleteLocationRequest;
use Pterodactyl\Http\Requests\Api\Application\Locations\UpdateLocationRequest;
2018-01-04 03:14:53 +00:00
class LocationController extends Controller
{
/**
* @var \Pterodactyl\Services\Locations\LocationCreationService
*/
private $creationService;
/**
* @var \Pterodactyl\Services\Locations\LocationDeletionService
*/
private $deletionService;
/**
* @var \Spatie\Fractal\Fractal
*/
private $fractal;
/**
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
*/
private $repository;
/**
* @var \Pterodactyl\Services\Locations\LocationUpdateService
*/
private $updateService;
/**
* LocationController constructor.
*
* @param \Spatie\Fractal\Fractal $fractal
* @param \Pterodactyl\Services\Locations\LocationCreationService $creationService
* @param \Pterodactyl\Services\Locations\LocationDeletionService $deletionService
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $repository
* @param \Pterodactyl\Services\Locations\LocationUpdateService $updateService
*/
public function __construct(
Fractal $fractal,
LocationCreationService $creationService,
LocationDeletionService $deletionService,
LocationRepositoryInterface $repository,
LocationUpdateService $updateService
) {
$this->creationService = $creationService;
$this->deletionService = $deletionService;
$this->fractal = $fractal;
$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
*/
public function index(GetLocationsRequest $request): array
2018-01-04 03:14:53 +00:00
{
$locations = $this->repository->paginated(100);
2018-01-04 03:14:53 +00:00
return $this->fractal->collection($locations)
->transformWith((new LocationTransformer)->setKey($request->key()))
2018-01-04 03:14:53 +00:00
->withResourceName('location')
->paginateWith(new IlluminatePaginatorAdapter($locations))
->toArray();
}
/**
* Return a single location.
*
2018-01-20 01:58:57 +00:00
* @param \Pterodactyl\Http\Controllers\Api\Application\Locations\GetLocationRequest $request
* @param \Pterodactyl\Models\Location $location
2018-01-04 03:14:53 +00:00
* @return array
*/
public function view(GetLocationRequest $request, Location $location): array
2018-01-04 03:14:53 +00:00
{
return $this->fractal->item($location)
->transformWith((new LocationTransformer)->setKey($request->key()))
2018-01-04 03:14:53 +00:00
->withResourceName('location')
->toArray();
}
/**
* Store a new location on the Panel and return a HTTP/201 response code with the
* new location attached.
*
2018-01-20 01:58:57 +00:00
* @param \Pterodactyl\Http\Controllers\Api\Application\Locations\StoreLocationRequest $request
2018-01-04 03:14:53 +00:00
* @return \Illuminate\Http\JsonResponse
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
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)
->transformWith((new LocationTransformer)->setKey($request->key()))
2018-01-04 03:14:53 +00:00
->withResourceName('location')
->respond(201);
}
/**
* 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
* @param \Pterodactyl\Models\Location $location
2018-01-04 03:14:53 +00:00
* @return array
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
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)
->transformWith((new LocationTransformer)->setKey($request->key()))
2018-01-04 03:14:53 +00:00
->withResourceName('location')
->toArray();
}
/**
* Delete a location from the Panel.
*
2018-01-20 01:58:57 +00:00
* @param \Pterodactyl\Http\Requests\Api\Application\Locations\DeleteLocationRequest $request
* @param \Pterodactyl\Models\Location $location
2018-01-04 03:14:53 +00:00
* @return \Illuminate\Http\Response
*
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException
*/
public function delete(DeleteLocationRequest $request, Location $location): Response
2018-01-04 03:14:53 +00:00
{
$this->deletionService->handle($location);
return response('', 204);
}
}