misc_pterodactyl-panel/app/Http/Controllers/Api/Application/Nodes/NodeController.php

164 lines
5.7 KiB
PHP
Raw Normal View History

<?php
2018-01-20 01:58:57 +00:00
namespace Pterodactyl\Http\Controllers\Api\Application\Nodes;
use Spatie\Fractal\Fractal;
use Pterodactyl\Models\Node;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Nodes\NodeUpdateService;
use Pterodactyl\Services\Nodes\NodeCreationService;
use Pterodactyl\Services\Nodes\NodeDeletionService;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Transformers\Api\Application\NodeTransformer;
2018-01-20 01:58:57 +00:00
use Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodeRequest;
use Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodesRequest;
use Pterodactyl\Http\Requests\Api\Application\Nodes\StoreNodeRequest;
use Pterodactyl\Http\Requests\Api\Application\Nodes\DeleteNodeRequest;
use Pterodactyl\Http\Requests\Api\Application\Nodes\UpdateNodeRequest;
class NodeController extends Controller
{
/**
* @var \Pterodactyl\Services\Nodes\NodeCreationService
*/
private $creationService;
/**
* @var \Pterodactyl\Services\Nodes\NodeDeletionService
*/
private $deletionService;
/**
* @var \Spatie\Fractal\Fractal
*/
private $fractal;
/**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
*/
private $repository;
/**
* @var \Pterodactyl\Services\Nodes\NodeUpdateService
*/
private $updateService;
/**
* NodeController constructor.
*
* @param \Spatie\Fractal\Fractal $fractal
* @param \Pterodactyl\Services\Nodes\NodeCreationService $creationService
* @param \Pterodactyl\Services\Nodes\NodeDeletionService $deletionService
* @param \Pterodactyl\Services\Nodes\NodeUpdateService $updateService
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $repository
*/
public function __construct(
Fractal $fractal,
NodeCreationService $creationService,
NodeDeletionService $deletionService,
NodeUpdateService $updateService,
NodeRepositoryInterface $repository
) {
$this->fractal = $fractal;
$this->repository = $repository;
$this->creationService = $creationService;
$this->deletionService = $deletionService;
$this->updateService = $updateService;
}
/**
* Return all of the nodes currently available on the Panel.
*
2018-01-20 01:58:57 +00:00
* @param \Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodesRequest $request
* @return array
*/
2018-01-13 20:08:19 +00:00
public function index(GetNodesRequest $request): array
{
$nodes = $this->repository->paginated(100);
2018-01-13 20:08:19 +00:00
return $this->fractal->collection($nodes)
->transformWith((new NodeTransformer)->setKey($request->key()))
->withResourceName('node')
2018-01-13 20:08:19 +00:00
->paginateWith(new IlluminatePaginatorAdapter($nodes))
->toArray();
}
/**
* Return data for a single instance of a node.
*
2018-01-20 01:58:57 +00:00
* @param \Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodeRequest $request
* @param \Pterodactyl\Models\Node $node
* @return array
*/
2018-01-13 20:08:19 +00:00
public function view(GetNodeRequest $request, Node $node): array
{
2018-01-13 20:08:19 +00:00
return $this->fractal->item($node)
->transformWith((new NodeTransformer)->setKey($request->key()))
->withResourceName('node')
->toArray();
}
/**
* Create a new node on the Panel. Returns the created node and a HTTP/201
* status response on success.
*
2018-01-20 01:58:57 +00:00
* @param \Pterodactyl\Http\Requests\Api\Application\Nodes\StoreNodeRequest $request
* @return \Illuminate\Http\JsonResponse
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
2018-01-13 20:08:19 +00:00
public function store(StoreNodeRequest $request): JsonResponse
{
2018-01-13 20:08:19 +00:00
$node = $this->creationService->handle($request->validated());
return $this->fractal->item($node)
2018-01-13 20:08:19 +00:00
->transformWith((new NodeTransformer)->setKey($request->key()))
->withResourceName('node')
->addMeta([
'link' => route('api.admin.node.view', ['node' => $node->id]),
])
->respond(201);
}
/**
* Update an existing node on the Panel.
*
2018-01-20 01:58:57 +00:00
* @param \Pterodactyl\Http\Requests\Api\Application\Nodes\UpdateNodeRequest $request
* @param \Pterodactyl\Models\Node $node
* @return array
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
2018-01-13 20:08:19 +00:00
public function update(UpdateNodeRequest $request, Node $node): array
{
2018-01-13 20:08:19 +00:00
$node = $this->updateService->returnUpdatedModel()->handle($node, $request->validated());
return $this->fractal->item($node)
2018-01-13 20:08:19 +00:00
->transformWith((new NodeTransformer)->setKey($request->key()))
->withResourceName('node')
->toArray();
}
/**
* Deletes a given node from the Panel as long as there are no servers
* currently attached to it.
*
2018-01-20 01:58:57 +00:00
* @param \Pterodactyl\Http\Requests\Api\Application\Nodes\DeleteNodeRequest $request
* @param \Pterodactyl\Models\Node $node
* @return \Illuminate\Http\Response
*
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
*/
2018-01-13 20:08:19 +00:00
public function delete(DeleteNodeRequest $request, Node $node): Response
{
$this->deletionService->handle($node);
2018-01-13 20:08:19 +00:00
return response('', 204);
}
}