2021-01-08 16:25:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application\Eggs;
|
|
|
|
|
2021-10-23 19:13:25 +00:00
|
|
|
use Ramsey\Uuid\Uuid;
|
2021-01-08 16:25:40 +00:00
|
|
|
use Pterodactyl\Models\Egg;
|
|
|
|
use Pterodactyl\Models\Nest;
|
2021-03-05 17:03:12 +00:00
|
|
|
use Illuminate\Http\Response;
|
2021-01-08 16:25:40 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2021-10-23 19:13:25 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-03-23 21:57:29 +00:00
|
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
2021-10-30 20:23:29 +00:00
|
|
|
use Pterodactyl\Services\Eggs\Sharing\EggExporterService;
|
2021-01-08 16:25:40 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Application\EggTransformer;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Eggs\GetEggRequest;
|
2021-03-23 21:57:29 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException;
|
2021-01-08 16:25:40 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Eggs\GetEggsRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Eggs\StoreEggRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Eggs\DeleteEggRequest;
|
2021-03-23 21:57:29 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Eggs\UpdateEggRequest;
|
2021-10-30 20:23:29 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Eggs\ExportEggRequest;
|
2021-01-08 16:25:40 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
|
|
|
|
|
|
|
class EggController extends ApplicationApiController
|
|
|
|
{
|
2021-10-30 20:23:29 +00:00
|
|
|
private EggExporterService $eggExporterService;
|
|
|
|
|
|
|
|
public function __construct(EggExporterService $eggExporterService)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->eggExporterService = $eggExporterService;
|
|
|
|
}
|
|
|
|
|
2021-01-08 16:25:40 +00:00
|
|
|
/**
|
|
|
|
* Return an array of all eggs on a given nest.
|
|
|
|
*/
|
|
|
|
public function index(GetEggsRequest $request, Nest $nest): array
|
|
|
|
{
|
2021-07-14 22:43:59 +00:00
|
|
|
$perPage = $request->query('per_page', 10);
|
2021-03-23 21:57:29 +00:00
|
|
|
if ($perPage > 100) {
|
|
|
|
throw new QueryValueOutOfRangeHttpException('per_page', 1, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
$eggs = QueryBuilder::for(Egg::query())
|
2021-07-14 22:43:59 +00:00
|
|
|
->where('nest_id', '=', $nest->id)
|
2021-03-23 21:57:29 +00:00
|
|
|
->allowedFilters(['id', 'name', 'author'])
|
|
|
|
->allowedSorts(['id', 'name', 'author']);
|
|
|
|
if ($perPage > 0) {
|
|
|
|
$eggs = $eggs->paginate($perPage);
|
|
|
|
}
|
2021-01-08 16:25:40 +00:00
|
|
|
|
|
|
|
return $this->fractal->collection($eggs)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(EggTransformer::class)
|
2021-01-08 16:25:40 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a single egg.
|
|
|
|
*/
|
|
|
|
public function view(GetEggRequest $request, Egg $egg): array
|
|
|
|
{
|
|
|
|
return $this->fractal->item($egg)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(EggTransformer::class)
|
2021-01-08 16:25:40 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new egg.
|
|
|
|
*/
|
|
|
|
public function store(StoreEggRequest $request): JsonResponse
|
|
|
|
{
|
2021-10-23 19:13:25 +00:00
|
|
|
$validated = $request->validated();
|
|
|
|
Log::info(json_encode($validated, JSON_PRETTY_PRINT));
|
|
|
|
$merged = array_merge($validated, [
|
|
|
|
'uuid' => Uuid::uuid4()->toString(),
|
|
|
|
// TODO: allow this to be set in the request, and default to config value if null or not present.
|
|
|
|
'author' => config('pterodactyl.service.author'),
|
|
|
|
]);
|
|
|
|
Log::info(json_encode($merged, JSON_PRETTY_PRINT));
|
|
|
|
|
|
|
|
$egg = Egg::query()->create($merged);
|
2021-01-08 16:25:40 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($egg)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(EggTransformer::class)
|
2021-10-30 20:23:29 +00:00
|
|
|
->respond(Response::HTTP_CREATED);
|
2021-01-08 16:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an egg.
|
|
|
|
*/
|
|
|
|
public function update(UpdateEggRequest $request, Egg $egg): array
|
|
|
|
{
|
|
|
|
$egg->update($request->validated());
|
|
|
|
|
|
|
|
return $this->fractal->item($egg)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(EggTransformer::class)
|
2021-01-08 16:25:40 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an egg.
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function delete(DeleteEggRequest $request, Egg $egg): Response
|
2021-01-08 16:25:40 +00:00
|
|
|
{
|
|
|
|
$egg->delete();
|
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2021-01-08 16:25:40 +00:00
|
|
|
}
|
2021-10-30 20:23:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Exports an egg.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
public function export(ExportEggRequest $request, int $eggId): JsonResponse
|
|
|
|
{
|
|
|
|
return new JsonResponse($this->eggExporterService->handle($eggId));
|
|
|
|
}
|
2021-01-08 16:25:40 +00:00
|
|
|
}
|