misc_pterodactyl-panel/app/Http/Controllers/Api/Application/Servers/ServerController.php

134 lines
5 KiB
PHP
Raw Normal View History

2018-01-20 01:58:57 +00:00
<?php
namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
2018-01-20 01:58:57 +00:00
2018-01-20 19:48:02 +00:00
use Illuminate\Http\Response;
use Pterodactyl\Models\Server;
use Illuminate\Http\JsonResponse;
use Spatie\QueryBuilder\QueryBuilder;
use Pterodactyl\Services\Servers\ServerCreationService;
2018-01-20 19:48:02 +00:00
use Pterodactyl\Services\Servers\ServerDeletionService;
use Pterodactyl\Services\Servers\BuildModificationService;
use Pterodactyl\Services\Servers\DetailsModificationService;
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException;
use Pterodactyl\Http\Requests\Api\Application\Servers\GetServerRequest;
use Pterodactyl\Http\Requests\Api\Application\Servers\GetServersRequest;
2018-01-20 19:48:02 +00:00
use Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest;
use Pterodactyl\Http\Requests\Api\Application\Servers\StoreServerRequest;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
use Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerRequest;
2018-01-20 01:58:57 +00:00
class ServerController extends ApplicationApiController
2018-01-20 01:58:57 +00:00
{
2021-03-05 15:46:14 +00:00
private ServerCreationService $creationService;
private ServerDeletionService $deletionService;
private BuildModificationService $buildModificationService;
private DetailsModificationService $detailsModificationService;
2018-01-20 01:58:57 +00:00
/**
* ServerController constructor.
*/
public function __construct(
ServerCreationService $creationService,
ServerDeletionService $deletionService,
BuildModificationService $buildModificationService,
DetailsModificationService $detailsModificationService
) {
2018-01-20 19:48:02 +00:00
parent::__construct();
$this->creationService = $creationService;
2018-01-20 19:48:02 +00:00
$this->deletionService = $deletionService;
$this->buildModificationService = $buildModificationService;
$this->detailsModificationService = $detailsModificationService;
2018-01-20 01:58:57 +00:00
}
/**
* Return all of the servers that currently exist on the Panel.
2021-03-05 15:46:14 +00:00
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function index(GetServersRequest $request): array
2018-01-20 01:58:57 +00:00
{
$perPage = $request->query('per_page', 10);
if ($perPage < 1 || $perPage > 100) {
throw new QueryValueOutOfRangeHttpException('per_page', 1, 100);
}
$servers = QueryBuilder::for(Server::query())
->allowedFilters(['id', 'uuid', 'uuidShort', 'name', 'owner_id', 'node_id', 'external_id'])
->allowedSorts(['id', 'uuid', 'uuidShort', 'name', 'owner_id', 'node_id', 'status'])
->paginate($perPage);
2018-01-20 01:58:57 +00:00
return $this->fractal->collection($servers)
2021-08-07 21:32:40 +00:00
->transformWith(ServerTransformer::class)
->toArray();
}
/**
* Create a new server on the system.
*
2019-11-16 21:33:01 +00:00
* @throws \Throwable
* @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
*/
public function store(StoreServerRequest $request): JsonResponse
{
2021-10-29 06:04:28 +00:00
$server = $this->creationService->handle($request->validated());
return $this->fractal->item($server)
2021-08-07 21:32:40 +00:00
->transformWith(ServerTransformer::class)
->respond(Response::HTTP_CREATED);
}
/**
* Show a single server transformed for the application API.
2021-03-05 15:46:14 +00:00
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function view(GetServerRequest $request, Server $server): array
{
return $this->fractal->item($server)
2021-08-07 21:32:40 +00:00
->transformWith(ServerTransformer::class)
2018-01-20 01:58:57 +00:00
->toArray();
}
2018-01-20 19:48:02 +00:00
/**
2021-03-05 15:46:14 +00:00
* Deletes a server.
*
2018-01-20 19:48:02 +00:00
* @throws \Pterodactyl\Exceptions\DisplayException
2021-03-05 15:46:14 +00:00
* @throws \Throwable
2018-01-20 19:48:02 +00:00
*/
public function delete(ServerWriteRequest $request, Server $server, string $force = ''): Response
{
$this->deletionService->withForce($force === 'force')->handle($server);
return $this->returnNoContent();
}
/**
* Update a server.
*
* @throws \Throwable
* @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
*/
public function update(UpdateServerRequest $request, Server $server): array
{
$server = $this->buildModificationService->handle($server, $request->validated());
$server = $this->detailsModificationService->returnUpdatedModel()->handle($server, $request->validated());
return $this->fractal->item($server)
->transformWith(ServerTransformer::class)
->toArray();
}
2018-01-20 01:58:57 +00:00
}