2021-01-08 16:25:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application\Eggs;
|
|
|
|
|
|
|
|
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;
|
|
|
|
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
|
|
|
use Pterodactyl\Transformers\Api\Application\EggTransformer;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Eggs\GetEggRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Eggs\GetEggsRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Eggs\StoreEggRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Eggs\UpdateEggRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Eggs\DeleteEggRequest;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
|
|
|
|
|
|
|
class EggController extends ApplicationApiController
|
|
|
|
{
|
2021-03-05 17:03:12 +00:00
|
|
|
private EggRepositoryInterface $repository;
|
2021-01-08 16:25:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* EggController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(EggRepositoryInterface $repository)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an array of all eggs on a given nest.
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
*/
|
|
|
|
public function index(GetEggsRequest $request, Nest $nest): array
|
|
|
|
{
|
|
|
|
$eggs = $this->repository->findWhere([
|
|
|
|
['nest_id', '=', $nest->id],
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $this->fractal->collection($eggs)
|
|
|
|
->transformWith($this->getTransformer(EggTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a single egg.
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
*/
|
|
|
|
public function view(GetEggRequest $request, Egg $egg): array
|
|
|
|
{
|
|
|
|
return $this->fractal->item($egg)
|
|
|
|
->transformWith($this->getTransformer(EggTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new egg.
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
*/
|
|
|
|
public function store(StoreEggRequest $request): JsonResponse
|
|
|
|
{
|
|
|
|
$egg = Egg::query()->create($request->validated());
|
|
|
|
|
|
|
|
return $this->fractal->item($egg)
|
|
|
|
->transformWith($this->getTransformer(EggTransformer::class))
|
|
|
|
->respond(JsonResponse::HTTP_CREATED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an egg.
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
*/
|
|
|
|
public function update(UpdateEggRequest $request, Egg $egg): array
|
|
|
|
{
|
|
|
|
$egg->update($request->validated());
|
|
|
|
|
|
|
|
return $this->fractal->item($egg)
|
|
|
|
->transformWith($this->getTransformer(EggTransformer::class))
|
|
|
|
->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
|
|
|
}
|
|
|
|
}
|