2018-01-27 18:38:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application\Nests;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Nest;
|
2021-01-03 23:45:07 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Pterodactyl\Services\Nests\NestUpdateService;
|
|
|
|
use Pterodactyl\Services\Nests\NestCreationService;
|
|
|
|
use Pterodactyl\Services\Nests\NestDeletionService;
|
2018-01-27 18:38:56 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
|
|
|
|
use Pterodactyl\Transformers\Api\Application\NestTransformer;
|
2021-01-03 23:45:07 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nests\GetNestRequest;
|
2021-01-05 21:52:49 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2018-01-27 18:38:56 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nests\GetNestsRequest;
|
2021-01-08 16:25:40 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nests\StoreEggRequest;
|
2021-01-03 23:45:07 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nests\UpdateNestRequest;
|
2021-01-08 16:25:40 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nests\DeleteEggRequest;
|
2018-01-27 18:38:56 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
|
|
|
|
|
|
|
class NestController extends ApplicationApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2021-01-03 23:45:07 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Nests\NestCreationService
|
|
|
|
*/
|
|
|
|
protected $nestCreationService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Nests\NestDeletionService
|
|
|
|
*/
|
|
|
|
protected $nestDeletionService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Nests\NestUpdateService
|
|
|
|
*/
|
|
|
|
protected $nestUpdateService;
|
|
|
|
|
2018-01-27 18:38:56 +00:00
|
|
|
/**
|
|
|
|
* NestController constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $repository
|
2021-01-03 23:45:07 +00:00
|
|
|
* @param \Pterodactyl\Services\Nests\NestCreationService $nestCreationService
|
|
|
|
* @param \Pterodactyl\Services\Nests\NestDeletionService $nestDeletionService
|
|
|
|
* @param \Pterodactyl\Services\Nests\NestUpdateService $nestUpdateService
|
2018-01-27 18:38:56 +00:00
|
|
|
*/
|
2021-01-03 23:45:07 +00:00
|
|
|
public function __construct(
|
|
|
|
NestRepositoryInterface $repository,
|
|
|
|
NestCreationService $nestCreationService,
|
|
|
|
NestDeletionService $nestDeletionService,
|
|
|
|
NestUpdateService $nestUpdateService
|
|
|
|
) {
|
2018-01-27 18:38:56 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->repository = $repository;
|
2021-01-03 23:45:07 +00:00
|
|
|
|
|
|
|
$this->nestCreationService = $nestCreationService;
|
|
|
|
$this->nestDeletionService = $nestDeletionService;
|
|
|
|
$this->nestUpdateService = $nestUpdateService;
|
2018-01-27 18:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all Nests that exist on the Panel.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Nests\GetNestsRequest $request
|
2021-01-01 22:55:30 +00:00
|
|
|
*
|
2018-01-27 18:38:56 +00:00
|
|
|
* @return array
|
2021-01-01 22:55:30 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2018-01-27 18:38:56 +00:00
|
|
|
*/
|
|
|
|
public function index(GetNestsRequest $request): array
|
|
|
|
{
|
2021-01-05 21:52:49 +00:00
|
|
|
$perPage = $request->query('per_page', 10);
|
|
|
|
if ($perPage < 1) {
|
|
|
|
$perPage = 10;
|
|
|
|
} else if ($perPage > 100) {
|
|
|
|
throw new BadRequestHttpException('"per_page" query parameter must be below 100.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$nests = $this->repository->paginated($perPage);
|
2018-01-27 18:38:56 +00:00
|
|
|
|
|
|
|
return $this->fractal->collection($nests)
|
|
|
|
->transformWith($this->getTransformer(NestTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return information about a single Nest model.
|
|
|
|
*
|
2021-01-03 23:45:07 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Nests\GetNestRequest $request
|
|
|
|
* @param \Pterodactyl\Models\Nest $nest
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
*/
|
|
|
|
public function view(GetNestRequest $request, Nest $nest): array
|
|
|
|
{
|
|
|
|
return $this->fractal->item($nest)
|
|
|
|
->transformWith($this->getTransformer(NestTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new nest.
|
|
|
|
*
|
2021-01-08 16:25:40 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Nests\StoreEggRequest $request
|
2021-01-03 23:45:07 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
*/
|
2021-01-08 16:25:40 +00:00
|
|
|
public function store(StoreEggRequest $request): array
|
2021-01-03 23:45:07 +00:00
|
|
|
{
|
|
|
|
$nest = $this->nestCreationService->handle($request->validated());
|
|
|
|
|
|
|
|
return $this->fractal->item($nest)
|
|
|
|
->transformWith($this->getTransformer(NestTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an existing nest.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Nests\UpdateNestRequest $request
|
2021-01-01 22:55:30 +00:00
|
|
|
* @param \Pterodactyl\Models\Nest $nest
|
|
|
|
*
|
2018-01-27 18:38:56 +00:00
|
|
|
* @return array
|
2021-01-01 22:55:30 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2021-01-03 23:45:07 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2018-01-27 18:38:56 +00:00
|
|
|
*/
|
2021-01-03 23:45:07 +00:00
|
|
|
public function update(UpdateNestRequest $request, Nest $nest): array
|
2018-01-27 18:38:56 +00:00
|
|
|
{
|
2021-01-03 23:45:07 +00:00
|
|
|
$this->nestUpdateService->handle($nest->id, $request->validated());
|
|
|
|
|
2021-01-01 22:55:30 +00:00
|
|
|
return $this->fractal->item($nest)
|
2018-01-27 18:38:56 +00:00
|
|
|
->transformWith($this->getTransformer(NestTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
2021-01-03 23:45:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an existing nest.
|
|
|
|
*
|
2021-01-08 16:25:40 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Nests\DeleteEggRequest $request
|
2021-01-03 23:45:07 +00:00
|
|
|
* @param \Pterodactyl\Models\Nest $nest
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
|
|
|
|
*/
|
2021-01-08 16:25:40 +00:00
|
|
|
public function delete(DeleteEggRequest $request, Nest $nest): JsonResponse
|
2021-01-03 23:45:07 +00:00
|
|
|
{
|
|
|
|
$this->nestDeletionService->handle($nest->id);
|
|
|
|
|
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
|
|
|
}
|
2018-01-27 18:38:56 +00:00
|
|
|
}
|