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

140 lines
4.8 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;
2018-01-20 01:58:57 +00:00
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
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;
2018-01-20 01:58:57 +00:00
class ServerController extends ApplicationApiController
2018-01-20 01:58:57 +00:00
{
/**
* @var \Pterodactyl\Services\Servers\ServerCreationService
*/
private $creationService;
2018-01-20 19:48:02 +00:00
/**
* @var \Pterodactyl\Services\Servers\ServerDeletionService
*/
private $deletionService;
2018-01-20 01:58:57 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
private $repository;
/**
* ServerController constructor.
*
2019-09-06 04:32:57 +00:00
* @param \Pterodactyl\Services\Servers\ServerCreationService $creationService
* @param \Pterodactyl\Services\Servers\ServerDeletionService $deletionService
2018-01-20 01:58:57 +00:00
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
*/
public function __construct(
ServerCreationService $creationService,
ServerDeletionService $deletionService,
ServerRepositoryInterface $repository
) {
2018-01-20 19:48:02 +00:00
parent::__construct();
$this->creationService = $creationService;
2018-01-20 19:48:02 +00:00
$this->deletionService = $deletionService;
2018-01-20 01:58:57 +00:00
$this->repository = $repository;
}
/**
* Return all of the servers that currently exist on the Panel.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\GetServersRequest $request
*
* @return array
* @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 = 10;
} else if ($perPage > 100) {
throw new BadRequestHttpException('"per_page" query parameter must be below 100.');
}
$servers = QueryBuilder::for(Server::query())
->allowedFilters(['uuid', 'name', 'image', 'external_id'])
->allowedSorts(['id', 'uuid'])
->paginate($perPage);
2018-01-20 01:58:57 +00:00
return $this->fractal->collection($servers)
->transformWith($this->getTransformer(ServerTransformer::class))
->toArray();
}
/**
* Create a new server on the system.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\StoreServerRequest $request
*
* @return \Illuminate\Http\JsonResponse
*
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
{
$server = $this->creationService->handle($request->validated(), $request->getDeploymentObject());
return $this->fractal->item($server)
->transformWith($this->getTransformer(ServerTransformer::class))
->respond(201);
}
/**
* Show a single server transformed for the application API.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\GetServerRequest $request
* @param \Pterodactyl\Models\Server $server
*
* @return array
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function view(GetServerRequest $request, Server $server): array
{
return $this->fractal->item($server)
->transformWith($this->getTransformer(ServerTransformer::class))
2018-01-20 01:58:57 +00:00
->toArray();
}
2018-01-20 19:48:02 +00:00
/**
* Deletes a server.
*
2018-01-20 19:48:02 +00:00
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest $request
2019-09-06 04:32:57 +00:00
* @param \Pterodactyl\Models\Server $server
* @param string $force
*
2018-01-20 19:48:02 +00:00
* @return \Illuminate\Http\Response
*
* @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();
}
2018-01-20 01:58:57 +00:00
}