2018-08-22 04:47:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
2018-08-25 21:43:21 +00:00
|
|
|
use Illuminate\Http\Response;
|
2018-08-22 04:47:01 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2018-08-26 21:01:00 +00:00
|
|
|
use Pterodactyl\Models\Database;
|
2018-08-22 04:47:01 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\DatabaseTransformer;
|
2018-08-25 21:43:21 +00:00
|
|
|
use Pterodactyl\Services\Databases\DatabaseManagementService;
|
2018-08-23 05:29:20 +00:00
|
|
|
use Pterodactyl\Services\Databases\DeployServerDatabaseService;
|
2018-08-22 04:47:01 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
|
|
|
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
2018-08-23 05:29:20 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\GetDatabasesRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\StoreDatabaseRequest;
|
2018-08-25 21:43:21 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\DeleteDatabaseRequest;
|
2018-08-22 04:47:01 +00:00
|
|
|
|
|
|
|
class DatabaseController extends ClientApiController
|
|
|
|
{
|
2018-08-23 05:29:20 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Databases\DeployServerDatabaseService
|
|
|
|
*/
|
|
|
|
private $deployDatabaseService;
|
|
|
|
|
2018-08-22 04:47:01 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2018-08-25 21:43:21 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
|
|
|
|
*/
|
|
|
|
private $managementService;
|
|
|
|
|
2018-08-22 04:47:01 +00:00
|
|
|
/**
|
|
|
|
* DatabaseController constructor.
|
|
|
|
*
|
2018-08-25 21:43:21 +00:00
|
|
|
* @param \Pterodactyl\Services\Databases\DatabaseManagementService $managementService
|
2018-08-22 04:47:01 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
|
2018-08-23 05:29:20 +00:00
|
|
|
* @param \Pterodactyl\Services\Databases\DeployServerDatabaseService $deployDatabaseService
|
2018-08-22 04:47:01 +00:00
|
|
|
*/
|
2018-08-25 21:43:21 +00:00
|
|
|
public function __construct(
|
|
|
|
DatabaseManagementService $managementService,
|
|
|
|
DatabaseRepositoryInterface $repository,
|
|
|
|
DeployServerDatabaseService $deployDatabaseService
|
|
|
|
) {
|
2018-08-22 04:47:01 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
2018-08-23 05:29:20 +00:00
|
|
|
$this->deployDatabaseService = $deployDatabaseService;
|
2018-08-22 04:47:01 +00:00
|
|
|
$this->repository = $repository;
|
2018-08-25 21:43:21 +00:00
|
|
|
$this->managementService = $managementService;
|
2018-08-22 04:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-23 05:29:20 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Databases\GetDatabasesRequest $request
|
2018-08-22 04:47:01 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function index(GetDatabasesRequest $request): array
|
|
|
|
{
|
|
|
|
$databases = $this->repository->getDatabasesForServer($request->getModel(Server::class)->id);
|
|
|
|
|
|
|
|
return $this->fractal->collection($databases)
|
|
|
|
->transformWith($this->getTransformer(DatabaseTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
2018-08-23 05:29:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new database for the given server and return it.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Databases\StoreDatabaseRequest $request
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException
|
|
|
|
*/
|
|
|
|
public function store(StoreDatabaseRequest $request): array
|
|
|
|
{
|
|
|
|
$database = $this->deployDatabaseService->handle($request->getModel(Server::class), $request->validated());
|
|
|
|
|
|
|
|
return $this->fractal->item($database)
|
|
|
|
->parseIncludes(['password'])
|
|
|
|
->transformWith($this->getTransformer(DatabaseTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
2018-08-25 21:43:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Databases\DeleteDatabaseRequest $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
public function delete(DeleteDatabaseRequest $request): Response
|
|
|
|
{
|
2018-08-26 21:01:00 +00:00
|
|
|
$this->managementService->delete($request->getModel(Database::class)->id);
|
|
|
|
|
2018-08-25 21:43:21 +00:00
|
|
|
return Response::create('', Response::HTTP_NO_CONTENT);
|
|
|
|
}
|
2018-08-22 04:47:01 +00:00
|
|
|
}
|