pelican-panel-mirror/app/Transformers/Api/Application/DatabaseHostTransformer.php
pelican-vehikl 7e7f0be7df
Allow Database Hosts to have multiple Nodes (#767)
* WIP

* Update laravel and migrations

* WIP

* fix tests

* Update composer

* Fix transformer

* Fix filament pages

* WIP

* Update DatabaseHostTransformer

* fix: tests

* pint this files pls

* resolve merge better

* Update migration

* Update Migration, Again

* Update down migration

---------

Co-authored-by: Vehikl <go@vehikl.com>
2024-12-06 20:24:30 -05:00

70 lines
1.9 KiB
PHP

<?php
namespace App\Transformers\Api\Application;
use App\Models\Node;
use App\Models\Database;
use App\Models\DatabaseHost;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\NullResource;
class DatabaseHostTransformer extends BaseTransformer
{
protected array $availableIncludes = [
'databases',
'nodes',
];
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return DatabaseHost::RESOURCE_NAME;
}
/**
* Transform database host into a representation for the application API.
*/
public function transform(DatabaseHost $model): array
{
return [
'id' => $model->id,
'name' => $model->name,
'host' => $model->host,
'port' => $model->port,
'username' => $model->username,
'created_at' => $model->created_at->toAtomString(),
'updated_at' => $model->updated_at->toAtomString(),
];
}
/**
* Include the databases associated with this host.
*/
public function includeDatabases(DatabaseHost $model): Collection|NullResource
{
if (!$this->authorize(Database::RESOURCE_NAME)) {
return $this->null();
}
$model->loadMissing('databases');
return $this->collection($model->getRelation('databases'), $this->makeTransformer(ServerDatabaseTransformer::class), Database::RESOURCE_NAME);
}
/**
* Include the nodes associated with this host.
*/
public function includeNodes(DatabaseHost $model): Collection|NullResource
{
if (!$this->authorize(Node::RESOURCE_NAME)) {
return $this->null();
}
$model->loadMissing('nodes');
return $this->collection($model->getRelation('nodes'), $this->makeTransformer(NodeTransformer::class), Node::RESOURCE_NAME);
}
}