2018-01-27 18:38:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application\Nests;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Nest;
|
2022-12-15 00:05:46 +00:00
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
|
|
|
use Pterodactyl\Services\Nests\NestUpdateService;
|
|
|
|
use Pterodactyl\Services\Nests\NestCreationService;
|
|
|
|
use Pterodactyl\Services\Nests\NestDeletionService;
|
|
|
|
use Pterodactyl\Services\Eggs\Sharing\EggImporterService;
|
|
|
|
use Pterodactyl\Transformers\Api\Application\EggTransformer;
|
2018-01-27 18:38:56 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Application\NestTransformer;
|
2022-12-15 00:05:46 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nests\GetNestRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Eggs\ImportEggRequest;
|
2018-01-27 18:38:56 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nests\GetNestsRequest;
|
2022-12-15 00:05:46 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nests\StoreNestRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nests\DeleteNestRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nests\UpdateNestRequest;
|
2018-01-27 18:38:56 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
|
|
|
|
|
|
|
class NestController extends ApplicationApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* NestController constructor.
|
|
|
|
*/
|
2022-12-15 00:05:46 +00:00
|
|
|
public function __construct(
|
|
|
|
private NestCreationService $nestCreationService,
|
|
|
|
private NestDeletionService $nestDeletionService,
|
|
|
|
private NestUpdateService $nestUpdateService,
|
|
|
|
private EggImporterService $eggImporterService
|
|
|
|
) {
|
2018-01-27 18:38:56 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all Nests that exist on the Panel.
|
|
|
|
*/
|
|
|
|
public function index(GetNestsRequest $request): array
|
|
|
|
{
|
2022-12-15 00:05:46 +00:00
|
|
|
$perPage = (int) $request->query('per_page', '10');
|
|
|
|
if ($perPage > 100) {
|
|
|
|
throw new QueryValueOutOfRangeHttpException('per_page', 1, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
$nests = QueryBuilder::for(Nest::query())
|
|
|
|
->allowedFilters(['id', 'name', 'author'])
|
|
|
|
->allowedSorts(['id', 'name', 'author']);
|
|
|
|
if ($perPage > 0) {
|
|
|
|
$nests = $nests->paginate($perPage);
|
|
|
|
}
|
2018-01-27 18:38:56 +00:00
|
|
|
|
|
|
|
return $this->fractal->collection($nests)
|
2022-12-15 00:05:46 +00:00
|
|
|
->transformWith(NestTransformer::class)
|
2018-01-27 18:38:56 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return information about a single Nest model.
|
|
|
|
*/
|
2022-12-15 00:05:46 +00:00
|
|
|
public function view(GetNestRequest $request, Nest $nest): array
|
|
|
|
{
|
|
|
|
return $this->fractal->item($nest)
|
|
|
|
->transformWith(NestTransformer::class)
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new nest.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
*/
|
|
|
|
public function store(StoreNestRequest $request): array
|
2018-01-27 18:38:56 +00:00
|
|
|
{
|
2022-12-15 00:05:46 +00:00
|
|
|
$nest = $this->nestCreationService->handle($request->validated());
|
|
|
|
|
2022-05-22 18:10:01 +00:00
|
|
|
return $this->fractal->item($nest)
|
2022-12-15 00:05:46 +00:00
|
|
|
->transformWith(NestTransformer::class)
|
2018-01-27 18:38:56 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
2022-12-15 00:05:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports an egg.
|
|
|
|
*/
|
|
|
|
public function import(ImportEggRequest $request, Nest $nest): array
|
|
|
|
{
|
|
|
|
$egg = $this->eggImporterService->handleContent(
|
|
|
|
$nest->id,
|
|
|
|
$request->getContent(),
|
|
|
|
$request->headers->get('Content-Type'),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->fractal->item($egg)
|
|
|
|
->transformWith(EggTransformer::class)
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an existing nest.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
public function update(UpdateNestRequest $request, Nest $nest): array
|
|
|
|
{
|
|
|
|
$this->nestUpdateService->handle($nest->id, $request->validated());
|
|
|
|
|
|
|
|
return $this->fractal->item($nest)
|
|
|
|
->transformWith(NestTransformer::class)
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an existing nest.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
|
|
|
|
*/
|
|
|
|
public function delete(DeleteNestRequest $request, Nest $nest): Response
|
|
|
|
{
|
|
|
|
$this->nestDeletionService->handle($nest->id);
|
|
|
|
|
|
|
|
return $this->returnNoContent();
|
|
|
|
}
|
2018-01-27 18:38:56 +00:00
|
|
|
}
|