diff --git a/app/Http/Controllers/Api/Application/DatabaseHosts/DatabaseHostController.php b/app/Http/Controllers/Api/Application/DatabaseHosts/DatabaseHostController.php new file mode 100644 index 0000000000..58c46c4940 --- /dev/null +++ b/app/Http/Controllers/Api/Application/DatabaseHosts/DatabaseHostController.php @@ -0,0 +1,100 @@ +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(); + } +} diff --git a/app/Http/Requests/Api/Application/DatabaseHosts/DeleteDatabaseHostRequest.php b/app/Http/Requests/Api/Application/DatabaseHosts/DeleteDatabaseHostRequest.php new file mode 100644 index 0000000000..b920790679 --- /dev/null +++ b/app/Http/Requests/Api/Application/DatabaseHosts/DeleteDatabaseHostRequest.php @@ -0,0 +1,13 @@ +route()->parameter('database_host'); + + return $rules ?? DatabaseHost::getRulesForUpdate($databaseHost->id); + } +} diff --git a/app/Transformers/Api/Application/DatabaseHostTransformer.php b/app/Transformers/Api/Application/DatabaseHostTransformer.php index d01afb43aa..1a47b905a7 100644 --- a/app/Transformers/Api/Application/DatabaseHostTransformer.php +++ b/app/Transformers/Api/Application/DatabaseHostTransformer.php @@ -2,8 +2,10 @@ namespace App\Transformers\Api\Application; +use App\Models\Node; use App\Models\Database; use App\Models\DatabaseHost; +use League\Fractal\Resource\Item; use League\Fractal\Resource\Collection; use League\Fractal\Resource\NullResource; use App\Services\Acl\Api\AdminAcl; @@ -12,6 +14,7 @@ class DatabaseHostTransformer extends BaseTransformer { protected array $availableIncludes = [ 'databases', + 'node', ]; /** @@ -54,4 +57,20 @@ public function includeDatabases(DatabaseHost $model): Collection|NullResource 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); + } } diff --git a/routes/api-application.php b/routes/api-application.php index 983a9a4933..e5f7908038 100644 --- a/routes/api-application.php +++ b/routes/api-application.php @@ -97,3 +97,22 @@ 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'); }); + +/* +|-------------------------------------------------------------------------- +| 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']); +});