2021-01-05 21:52:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application\Databases;
|
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
use Illuminate\Http\Response;
|
2021-01-05 21:52:49 +00:00
|
|
|
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-03-23 21:57:29 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException;
|
2021-01-05 21:52:49 +00:00
|
|
|
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\DeleteDatabaseRequest;
|
2021-02-05 16:43:48 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Databases\UpdateDatabaseRequest;
|
2021-01-05 21:52:49 +00:00
|
|
|
|
|
|
|
class DatabaseController extends ApplicationApiController
|
|
|
|
{
|
2021-02-05 16:43:48 +00:00
|
|
|
private HostCreationService $creationService;
|
|
|
|
private HostUpdateService $updateService;
|
2021-01-09 16:58:55 +00:00
|
|
|
|
2021-01-05 21:52:49 +00:00
|
|
|
/**
|
|
|
|
* DatabaseController constructor.
|
|
|
|
*/
|
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.
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
*/
|
|
|
|
public function index(GetDatabasesRequest $request): array
|
|
|
|
{
|
|
|
|
$perPage = $request->query('per_page', 10);
|
2021-03-23 21:57:29 +00:00
|
|
|
if ($perPage < 1 || $perPage > 100) {
|
|
|
|
throw new QueryValueOutOfRangeHttpException('per_page', 1, 100);
|
2021-01-05 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
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.
|
|
|
|
*
|
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.
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function delete(DeleteDatabaseRequest $request, DatabaseHost $databaseHost): Response
|
2021-01-05 21:52:49 +00:00
|
|
|
{
|
|
|
|
$databaseHost->delete();
|
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2021-01-05 21:52:49 +00:00
|
|
|
}
|
|
|
|
}
|