2021-01-05 21:52:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application\Databases;
|
|
|
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Pterodactyl\Models\DatabaseHost;
|
|
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
2021-01-09 16:58:55 +00:00
|
|
|
use Pterodactyl\Services\Databases\Hosts\HostUpdateService;
|
|
|
|
use Pterodactyl\Services\Databases\Hosts\HostCreationService;
|
2021-01-05 21:52:49 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
|
|
use Pterodactyl\Transformers\Api\Application\DatabaseHostTransformer;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Databases\GetDatabaseRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Databases\GetDatabasesRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Databases\StoreDatabaseRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Databases\UpdateDatabaseRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Databases\DeleteDatabaseRequest;
|
|
|
|
|
|
|
|
class DatabaseController extends ApplicationApiController
|
|
|
|
{
|
2021-01-09 16:58:55 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Databases\Hosts\HostCreationService
|
|
|
|
*/
|
|
|
|
private $creationService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Databases\Hosts\HostUpdateService
|
|
|
|
*/
|
|
|
|
private $updateService;
|
|
|
|
|
2021-01-05 21:52:49 +00:00
|
|
|
/**
|
|
|
|
* DatabaseController constructor.
|
2021-01-09 16:58:55 +00:00
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Services\Databases\Hosts\HostCreationService $creationService
|
|
|
|
* @param \Pterodactyl\Services\Databases\Hosts\HostUpdateService $updateService
|
2021-01-05 21:52:49 +00:00
|
|
|
*/
|
2021-01-09 16:58:55 +00:00
|
|
|
public function __construct(HostCreationService $creationService, HostUpdateService $updateService)
|
2021-01-05 21:52:49 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
2021-01-09 16:58:55 +00:00
|
|
|
|
|
|
|
$this->creationService = $creationService;
|
|
|
|
$this->updateService = $updateService;
|
2021-01-05 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of all database hosts.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Databases\GetDatabasesRequest $request
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
*/
|
|
|
|
public function index(GetDatabasesRequest $request): array
|
|
|
|
{
|
|
|
|
$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.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$databases = QueryBuilder::for(DatabaseHost::query())
|
|
|
|
->allowedFilters(['name', 'host'])
|
|
|
|
->allowedSorts(['id', 'name', 'host'])
|
|
|
|
->paginate($perPage);
|
|
|
|
|
|
|
|
return $this->fractal->collection($databases)
|
|
|
|
->transformWith($this->getTransformer(DatabaseHostTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a single database host.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Databases\GetDatabaseRequest $request
|
|
|
|
* @param \Pterodactyl\Models\DatabaseHost $databaseHost
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
*/
|
|
|
|
public function view(GetDatabaseRequest $request, DatabaseHost $databaseHost): array
|
|
|
|
{
|
|
|
|
return $this->fractal->item($databaseHost)
|
|
|
|
->transformWith($this->getTransformer(DatabaseHostTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new database host.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Databases\StoreDatabaseRequest $request
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-01-09 16:58:55 +00:00
|
|
|
* @throws \Throwable
|
2021-01-05 21:52:49 +00:00
|
|
|
*/
|
|
|
|
public function store(StoreDatabaseRequest $request): JsonResponse
|
|
|
|
{
|
2021-01-09 16:58:55 +00:00
|
|
|
$databaseHost = $this->creationService->handle($request->validated());
|
2021-01-05 21:52:49 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($databaseHost)
|
|
|
|
->transformWith($this->getTransformer(DatabaseHostTransformer::class))
|
|
|
|
->respond(JsonResponse::HTTP_CREATED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a database host.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Databases\UpdateDatabaseRequest $request
|
|
|
|
* @param \Pterodactyl\Models\DatabaseHost $databaseHost
|
|
|
|
*
|
|
|
|
* @return array
|
2021-01-09 16:58:55 +00:00
|
|
|
* @throws \Throwable
|
2021-01-05 21:52:49 +00:00
|
|
|
*/
|
|
|
|
public function update(UpdateDatabaseRequest $request, DatabaseHost $databaseHost): array
|
|
|
|
{
|
2021-01-09 16:58:55 +00:00
|
|
|
$databaseHost = $this->updateService->handle($databaseHost->id, $request->validated());
|
2021-01-05 21:52:49 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($databaseHost)
|
|
|
|
->transformWith($this->getTransformer(DatabaseHostTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a database host.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Databases\DeleteDatabaseRequest $request
|
|
|
|
* @param \Pterodactyl\Models\DatabaseHost $databaseHost
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function delete(DeleteDatabaseRequest $request, DatabaseHost $databaseHost): JsonResponse
|
|
|
|
{
|
|
|
|
$databaseHost->delete();
|
|
|
|
|
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
|
|
|
}
|
|
|
|
}
|