mirror of
				https://github.com/pelican-dev/panel.git
				synced 2025-10-31 10:16:52 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			106 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			106 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
 | |
| 
 | |
| use Illuminate\Http\Response;
 | |
| use Pterodactyl\Models\Server;
 | |
| use Illuminate\Http\JsonResponse;
 | |
| use Spatie\QueryBuilder\QueryBuilder;
 | |
| use Pterodactyl\Services\Servers\ServerCreationService;
 | |
| use Pterodactyl\Services\Servers\ServerDeletionService;
 | |
| use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
 | |
| use Pterodactyl\Transformers\Api\Application\ServerTransformer;
 | |
| use Pterodactyl\Http\Requests\Api\Application\Servers\GetServerRequest;
 | |
| use Pterodactyl\Http\Requests\Api\Application\Servers\GetServersRequest;
 | |
| use Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest;
 | |
| use Pterodactyl\Http\Requests\Api\Application\Servers\StoreServerRequest;
 | |
| use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
 | |
| 
 | |
| class ServerController extends ApplicationApiController
 | |
| {
 | |
|     /**
 | |
|      * @var \Pterodactyl\Services\Servers\ServerCreationService
 | |
|      */
 | |
|     private $creationService;
 | |
| 
 | |
|     /**
 | |
|      * @var \Pterodactyl\Services\Servers\ServerDeletionService
 | |
|      */
 | |
|     private $deletionService;
 | |
| 
 | |
|     /**
 | |
|      * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
 | |
|      */
 | |
|     private $repository;
 | |
| 
 | |
|     /**
 | |
|      * ServerController constructor.
 | |
|      */
 | |
|     public function __construct(
 | |
|         ServerCreationService $creationService,
 | |
|         ServerDeletionService $deletionService,
 | |
|         ServerRepositoryInterface $repository
 | |
|     ) {
 | |
|         parent::__construct();
 | |
| 
 | |
|         $this->creationService = $creationService;
 | |
|         $this->deletionService = $deletionService;
 | |
|         $this->repository = $repository;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Return all of the servers that currently exist on the Panel.
 | |
|      */
 | |
|     public function index(GetServersRequest $request): array
 | |
|     {
 | |
|         $servers = QueryBuilder::for(Server::query())
 | |
|             ->allowedFilters(['uuid', 'name', 'image', 'external_id'])
 | |
|             ->allowedSorts(['id', 'uuid'])
 | |
|             ->paginate(100);
 | |
| 
 | |
|         return $this->fractal->collection($servers)
 | |
|             ->transformWith($this->getTransformer(ServerTransformer::class))
 | |
|             ->toArray();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Create a new server on the system.
 | |
|      *
 | |
|      * @throws \Throwable
 | |
|      * @throws \Illuminate\Validation\ValidationException
 | |
|      * @throws \Pterodactyl\Exceptions\DisplayException
 | |
|      * @throws \Pterodactyl\Exceptions\Model\DataValidationException
 | |
|      * @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.
 | |
|      */
 | |
|     public function view(GetServerRequest $request): array
 | |
|     {
 | |
|         return $this->fractal->item($request->getModel(Server::class))
 | |
|             ->transformWith($this->getTransformer(ServerTransformer::class))
 | |
|             ->toArray();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @throws \Pterodactyl\Exceptions\DisplayException
 | |
|      */
 | |
|     public function delete(ServerWriteRequest $request, Server $server, string $force = ''): Response
 | |
|     {
 | |
|         $this->deletionService->withForce($force === 'force')->handle($server);
 | |
| 
 | |
|         return $this->returnNoContent();
 | |
|     }
 | |
| }
 | 
