mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 04:04:45 +02:00
Add api for database hosts (#159)
* add application api endpoints for database hosts * run pint * forgot to lint this one * Update app/Http/Controllers/Api/Application/DatabaseHosts/DatabaseHostController.php Co-authored-by: Devonte W <devnote.dev75@gmail.com> * Update routes/api-application.php Co-authored-by: Devonte W <devnote.dev75@gmail.com> * rename all "databaseHost" to "database_host" --------- Co-authored-by: Devonte W <devnote.dev75@gmail.com>
This commit is contained in:
parent
8a617d416e
commit
afd9f2eb0e
@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api\Application\DatabaseHosts;
|
||||||
|
|
||||||
|
use Illuminate\Http\Response;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use App\Models\DatabaseHost;
|
||||||
|
use Spatie\QueryBuilder\QueryBuilder;
|
||||||
|
use App\Services\Databases\Hosts\HostUpdateService;
|
||||||
|
use App\Services\Databases\Hosts\HostCreationService;
|
||||||
|
use App\Transformers\Api\Application\DatabaseHostTransformer;
|
||||||
|
use App\Http\Controllers\Api\Application\ApplicationApiController;
|
||||||
|
use App\Http\Requests\Api\Application\DatabaseHosts\GetDatabaseHostRequest;
|
||||||
|
use App\Http\Requests\Api\Application\DatabaseHosts\StoreDatabaseHostRequest;
|
||||||
|
use App\Http\Requests\Api\Application\DatabaseHosts\DeleteDatabaseHostRequest;
|
||||||
|
use App\Http\Requests\Api\Application\DatabaseHosts\UpdateDatabaseHostRequest;
|
||||||
|
|
||||||
|
class DatabaseHostController extends ApplicationApiController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* DatabaseHostController constructor.
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
private HostCreationService $creationService,
|
||||||
|
private HostUpdateService $updateService
|
||||||
|
) {
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all the database hosts currently registered on the Panel.
|
||||||
|
*/
|
||||||
|
public function index(GetDatabaseHostRequest $request): array
|
||||||
|
{
|
||||||
|
$databases = QueryBuilder::for(DatabaseHost::query())
|
||||||
|
->allowedFilters(['name', 'host'])
|
||||||
|
->allowedSorts(['id', 'name', 'host'])
|
||||||
|
->paginate($request->query('per_page') ?? 10);
|
||||||
|
|
||||||
|
return $this->fractal->collection($databases)
|
||||||
|
->transformWith($this->getTransformer(DatabaseHostTransformer::class))
|
||||||
|
->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a single database host.
|
||||||
|
*/
|
||||||
|
public function view(GetDatabaseHostRequest $request, DatabaseHost $databaseHost): array
|
||||||
|
{
|
||||||
|
return $this->fractal->item($databaseHost)
|
||||||
|
->transformWith($this->getTransformer(DatabaseHostTransformer::class))
|
||||||
|
->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a new database host on the Panel and return an HTTP/201 response code with the
|
||||||
|
* new database host attached.
|
||||||
|
*
|
||||||
|
* @throws \Throwable
|
||||||
|
*/
|
||||||
|
public function store(StoreDatabaseHostRequest $request): JsonResponse
|
||||||
|
{
|
||||||
|
$databaseHost = $this->creationService->handle($request->validated());
|
||||||
|
|
||||||
|
return $this->fractal->item($databaseHost)
|
||||||
|
->transformWith($this->getTransformer(DatabaseHostTransformer::class))
|
||||||
|
->addMeta([
|
||||||
|
'resource' => route('api.application.databases.view', [
|
||||||
|
'database_host' => $databaseHost->id,
|
||||||
|
]),
|
||||||
|
])
|
||||||
|
->respond(201);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a database host on the Panel and return the updated record to the user.
|
||||||
|
*
|
||||||
|
* @throws \Throwable
|
||||||
|
*/
|
||||||
|
public function update(UpdateDatabaseHostRequest $request, DatabaseHost $databaseHost): array
|
||||||
|
{
|
||||||
|
$databaseHost = $this->updateService->handle($databaseHost->id, $request->validated());
|
||||||
|
|
||||||
|
return $this->fractal->item($databaseHost)
|
||||||
|
->transformWith($this->getTransformer(DatabaseHostTransformer::class))
|
||||||
|
->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a database host from the Panel.
|
||||||
|
*
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function delete(DeleteDatabaseHostRequest $request, DatabaseHost $databaseHost): Response
|
||||||
|
{
|
||||||
|
$databaseHost->delete();
|
||||||
|
|
||||||
|
return $this->returnNoContent();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Api\Application\DatabaseHosts;
|
||||||
|
|
||||||
|
use App\Services\Acl\Api\AdminAcl;
|
||||||
|
use App\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||||
|
|
||||||
|
class DeleteDatabaseHostRequest extends ApplicationApiRequest
|
||||||
|
{
|
||||||
|
protected ?string $resource = AdminAcl::RESOURCE_DATABASE_HOSTS;
|
||||||
|
|
||||||
|
protected int $permission = AdminAcl::WRITE;
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Api\Application\DatabaseHosts;
|
||||||
|
|
||||||
|
use App\Services\Acl\Api\AdminAcl;
|
||||||
|
use App\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||||
|
|
||||||
|
class GetDatabaseHostRequest extends ApplicationApiRequest
|
||||||
|
{
|
||||||
|
protected ?string $resource = AdminAcl::RESOURCE_DATABASE_HOSTS;
|
||||||
|
|
||||||
|
protected int $permission = AdminAcl::READ;
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Api\Application\DatabaseHosts;
|
||||||
|
|
||||||
|
use App\Models\DatabaseHost;
|
||||||
|
use App\Services\Acl\Api\AdminAcl;
|
||||||
|
use App\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||||
|
|
||||||
|
class StoreDatabaseHostRequest extends ApplicationApiRequest
|
||||||
|
{
|
||||||
|
protected ?string $resource = AdminAcl::RESOURCE_DATABASE_HOSTS;
|
||||||
|
|
||||||
|
protected int $permission = AdminAcl::WRITE;
|
||||||
|
|
||||||
|
public function rules(array $rules = null): array
|
||||||
|
{
|
||||||
|
return $rules ?? DatabaseHost::getRules();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Api\Application\DatabaseHosts;
|
||||||
|
|
||||||
|
use App\Models\DatabaseHost;
|
||||||
|
|
||||||
|
class UpdateDatabaseHostRequest extends StoreDatabaseHostRequest
|
||||||
|
{
|
||||||
|
public function rules(array $rules = null): array
|
||||||
|
{
|
||||||
|
/** @var DatabaseHost $databaseHost */
|
||||||
|
$databaseHost = $this->route()->parameter('database_host');
|
||||||
|
|
||||||
|
return $rules ?? DatabaseHost::getRulesForUpdate($databaseHost->id);
|
||||||
|
}
|
||||||
|
}
|
@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
namespace App\Transformers\Api\Application;
|
namespace App\Transformers\Api\Application;
|
||||||
|
|
||||||
|
use App\Models\Node;
|
||||||
use App\Models\Database;
|
use App\Models\Database;
|
||||||
use App\Models\DatabaseHost;
|
use App\Models\DatabaseHost;
|
||||||
|
use League\Fractal\Resource\Item;
|
||||||
use League\Fractal\Resource\Collection;
|
use League\Fractal\Resource\Collection;
|
||||||
use League\Fractal\Resource\NullResource;
|
use League\Fractal\Resource\NullResource;
|
||||||
use App\Services\Acl\Api\AdminAcl;
|
use App\Services\Acl\Api\AdminAcl;
|
||||||
@ -12,6 +14,7 @@ class DatabaseHostTransformer extends BaseTransformer
|
|||||||
{
|
{
|
||||||
protected array $availableIncludes = [
|
protected array $availableIncludes = [
|
||||||
'databases',
|
'databases',
|
||||||
|
'node',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,4 +57,20 @@ class DatabaseHostTransformer extends BaseTransformer
|
|||||||
|
|
||||||
return $this->collection($model->getRelation('databases'), $this->makeTransformer(ServerDatabaseTransformer::class), Database::RESOURCE_NAME);
|
return $this->collection($model->getRelation('databases'), $this->makeTransformer(ServerDatabaseTransformer::class), Database::RESOURCE_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Include the node associated with this host.
|
||||||
|
*
|
||||||
|
* @throws \App\Exceptions\Transformer\InvalidTransformerLevelException
|
||||||
|
*/
|
||||||
|
public function includeNode(DatabaseHost $model): Item|NullResource
|
||||||
|
{
|
||||||
|
if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {
|
||||||
|
return $this->null();
|
||||||
|
}
|
||||||
|
|
||||||
|
$model->loadMissing('node');
|
||||||
|
|
||||||
|
return $this->item($model->getRelation('node'), $this->makeTransformer(NodeTransformer::class), Node::RESOURCE_NAME);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,3 +97,22 @@ Route::prefix('/eggs')->group(function () {
|
|||||||
Route::get('/', [Application\Eggs\EggController::class, 'index'])->name('api.application.eggs.eggs');
|
Route::get('/', [Application\Eggs\EggController::class, 'index'])->name('api.application.eggs.eggs');
|
||||||
Route::get('/{egg:id}', [Application\Eggs\EggController::class, 'view'])->name('api.application.eggs.eggs.view');
|
Route::get('/{egg:id}', [Application\Eggs\EggController::class, 'view'])->name('api.application.eggs.eggs.view');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Database Host Controller Routes
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| Endpoint: /api/application/database-hosts
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
Route::group(['prefix' => '/database-hosts'], function () {
|
||||||
|
Route::get('/', [Application\DatabaseHosts\DatabaseHostController::class, 'index'])->name('api.application.databasehosts');
|
||||||
|
Route::get('/{database_host:id}', [Application\DatabaseHosts\DatabaseHostController::class, 'view'])->name('api.application.databasehosts.view');
|
||||||
|
|
||||||
|
Route::post('/', [Application\DatabaseHosts\DatabaseHostController::class, 'store']);
|
||||||
|
|
||||||
|
Route::patch('/{database_host:id}', [Application\DatabaseHosts\DatabaseHostController::class, 'update']);
|
||||||
|
|
||||||
|
Route::delete('/{database_host:id}', [Application\DatabaseHosts\DatabaseHostController::class, 'delete']);
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user