From 8d88d0afc0b557a7ce0d792bf4e43976e6084c84 Mon Sep 17 00:00:00 2001 From: Ruben van der Linde Date: Sun, 8 Dec 2024 22:49:52 +0100 Subject: [PATCH] Add uri to objects --- lib/Controller/AttachmentsController.php | 13 ++ lib/Controller/CatalogiController.php | 13 ++ lib/Controller/OrganizationsController.php | 14 +- lib/Controller/PublicationTypesController.php | 13 +- lib/Controller/PublicationsController.php | 12 ++ lib/Controller/ThemesController.php | 16 +- lib/Db/Attachment.php | 3 + lib/Db/AttachmentMapper.php | 13 +- lib/Db/Catalog.php | 3 + lib/Db/CatalogMapper.php | 11 +- lib/Db/Organization.php | 3 + lib/Db/OrganizationMapper.php | 10 ++ lib/Db/Publication.php | 3 + lib/Db/PublicationMapper.php | 11 +- lib/Db/PublicationType.php | 3 + lib/Db/PublicationTypeMapper.php | 12 +- lib/Db/Theme.php | 3 + lib/Db/ThemeMapper.php | 13 +- lib/Migration/Version6Date20241208222530.php | 139 ++++++++++++++++++ 19 files changed, 299 insertions(+), 9 deletions(-) create mode 100644 lib/Migration/Version6Date20241208222530.php diff --git a/lib/Controller/AttachmentsController.php b/lib/Controller/AttachmentsController.php index d692f5ff..4e37bfe6 100644 --- a/lib/Controller/AttachmentsController.php +++ b/lib/Controller/AttachmentsController.php @@ -16,6 +16,7 @@ use OCP\IAppConfig; use OCP\IRequest; use OCP\IUserSession; +use OCP\IURLGenerator; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Symfony\Component\Uid\Uuid; @@ -129,6 +130,12 @@ public function create(ObjectService $objectService): JSONResponse // Save the new attachment object. $object = $this->objectService->saveObject('attachment', $data); + // If we do not have an uri, we need to generate one + if (isset($object['uri']) === false) { + $object['uri'] = $this->urlGenerator->linkToRoute('openCatalogi.attachments.show', ['id' => $object['id']]); + $object = $this->objectService->saveObject('attachment', $object); + } + // Return the created object as a JSON response. return new JSONResponse($object); } @@ -152,6 +159,12 @@ public function update(string|int $id, ObjectService $objectService): JSONRespon // Ensure the ID in the data matches the ID in the URL $data['id'] = $id; + + // If we do not have an uri, we need to generate one + if (isset($data['uri']) === false) { + $data['uri'] = $this->urlGenerator->linkToRoute('openCatalogi.attachments.show', ['id' => $data['id']]); + $data = $this->objectService->saveObject('attachment', $data); + } // Save the updated attachment object $object = $this->objectService->saveObject('attachment', $data); diff --git a/lib/Controller/CatalogiController.php b/lib/Controller/CatalogiController.php index 002e7fea..55724cb0 100644 --- a/lib/Controller/CatalogiController.php +++ b/lib/Controller/CatalogiController.php @@ -13,6 +13,7 @@ use OCP\AppFramework\Http\JSONResponse; use OCP\IAppConfig; use OCP\IRequest; +use OCP\IURLGenerator; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -114,6 +115,12 @@ public function create(ObjectService $objectService): JSONResponse // Save the new catalog object $object = $this->objectService->saveObject('catalog', $data); + // If we do not have an uri, we need to generate one + if (isset($object['uri']) === false) { + $object['uri'] = $this->urlGenerator->linkToRoute('openCatalogi.catalogs.show', ['id' => $object['id']]); + $object = $this->objectService->saveObject('catalog', $object); + } + // Update all external directories $this->broadcastService->broadcast(); @@ -142,6 +149,12 @@ public function update(string|int $id, ObjectService $objectService): JSONRespon // Ensure the ID in the data matches the ID in the URL $data['id'] = $id; + // If we do not have an uri, we need to generate one + if (isset($data['uri']) === false) { + $data['uri'] = $this->urlGenerator->linkToRoute('openCatalogi.catalogs.show', ['id' => $data['id']]); + $data = $this->objectService->saveObject('catalog', $data); + } + // Save the updated catalog object $object = $this->objectService->saveObject('catalog', $data); diff --git a/lib/Controller/OrganizationsController.php b/lib/Controller/OrganizationsController.php index e4865f42..1ad1141d 100644 --- a/lib/Controller/OrganizationsController.php +++ b/lib/Controller/OrganizationsController.php @@ -10,7 +10,7 @@ use OCP\AppFramework\Http\JSONResponse; use OCP\IAppConfig; use OCP\IRequest; - +use OCP\IURLGenerator; /** * Class OrganizationsController * @@ -109,6 +109,12 @@ public function create(): JSONResponse // Save the new organization object $object = $this->objectService->saveObject('organization', $data); + // If we do not have an uri, we need to generate one + if (isset($object['uri']) === false) { + $object['uri'] = $this->urlGenerator->linkToRoute('openCatalogi.organizations.show', ['id' => $object['id']]); + $object = $this->objectService->saveObject('organization', $object); + } + // Return the created object as a JSON response return new JSONResponse($object); } @@ -130,6 +136,12 @@ public function update(string|int $id): JSONResponse // Ensure the ID in the data matches the ID in the URL $data['id'] = $id; + // If we do not have an uri, we need to generate one + if (isset($data['uri']) === false) { + $data['uri'] = $this->urlGenerator->linkToRoute('openCatalogi.organizations.show', ['id' => $data['id']]); + $data = $this->objectService->saveObject('organization', $data); + } + // Save the updated organization object $object = $this->objectService->saveObject('organization', $data); diff --git a/lib/Controller/PublicationTypesController.php b/lib/Controller/PublicationTypesController.php index eba92db1..de4444e0 100644 --- a/lib/Controller/PublicationTypesController.php +++ b/lib/Controller/PublicationTypesController.php @@ -11,7 +11,7 @@ use OCP\AppFramework\Http\JSONResponse; use OCP\IAppConfig; use OCP\IRequest; - +use OCP\IURLGenerator; /** * Class PublicationTypesController * @@ -118,6 +118,12 @@ public function create(): JSONResponse // Save the new publication type object $object = $this->objectService->saveObject('publicationType', $data); + // If we do not have an uri, we need to generate one + if (isset($object['uri']) === false) { + $object['uri'] = $this->urlGenerator->linkToRoute('openCatalogi.publicationTypes.show', ['id' => $object['id']]); + $object = $this->objectService->saveObject('publicationType', $object); + } + // Update all external directories $this->broadcastService->broadcast(); @@ -142,6 +148,11 @@ public function update(string|int $id): JSONResponse // Ensure the ID in the data matches the ID in the URL $data['id'] = $id; + // If we do not have an uri, we need to generate one + if (isset($data['uri']) === false) { + $data['uri'] = $this->urlGenerator->linkToRoute('openCatalogi.publicationTypes.show', ['id' => $data['id']]); + } + // Save the updated publication type object $object = $this->objectService->saveObject('publicationType', $data); diff --git a/lib/Controller/PublicationsController.php b/lib/Controller/PublicationsController.php index 47a1c8de..84555ff1 100644 --- a/lib/Controller/PublicationsController.php +++ b/lib/Controller/PublicationsController.php @@ -24,6 +24,7 @@ use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\IAppConfig; use OCP\IRequest; +use OCP\IURLGenerator; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Symfony\Component\Uid\Uuid; @@ -197,6 +198,12 @@ public function create(ObjectService $objectService): JSONResponse // Save the new publication object $object = $this->objectService->saveObject('publication', $data); + // If we do not have an uri, we need to generate one + if (isset($object['uri']) === false) { + $object['uri'] = $this->urlGenerator->linkToRoute('openCatalogi.publications.show', ['id' => $object['id']]); + $object = $this->objectService->saveObject('publication', $object); + } + // Return the created object as a JSON response return new JSONResponse($object); } @@ -221,6 +228,11 @@ public function update(string|int $id, ObjectService $objectService): JSONRespon // Ensure the ID in the data matches the ID in the URL $data['id'] = $id; + // If we do not have an uri, we need to generate one + if (isset($object['uri']) === false) { + $object['uri'] = $this->urlGenerator->linkToRoute('openCatalogi.publications.show', ['id' => $object['id']]); + } + // Save the updated publication object $object = $this->objectService->saveObject('publication', $data); diff --git a/lib/Controller/ThemesController.php b/lib/Controller/ThemesController.php index 341d5292..b9b2fb66 100644 --- a/lib/Controller/ThemesController.php +++ b/lib/Controller/ThemesController.php @@ -11,7 +11,7 @@ use OCP\AppFramework\Http\JSONResponse; use OCP\IAppConfig; use OCP\IRequest; - +use OCP\IURLGenerator; /** * Class ThemesController * @@ -97,6 +97,12 @@ public function create(): JSONResponse // Save the new theme object $object = $this->objectService->saveObject('theme', $data); + // If we do not have an uri, we need to generate one + if (isset($object['uri']) === false) { + $object['uri'] = $this->urlGenerator->linkToRoute('openCatalogi.themes.show', ['id' => $object['id']]); + $object = $this->objectService->saveObject('theme', $object); + } + // Return the created object as a JSON response return new JSONResponse($object); } @@ -116,7 +122,13 @@ public function update(string|int $id): JSONResponse $data = $this->request->getParams(); // Ensure the ID in the data matches the ID in the URL - $data['id'] = $id; + $data['id'] = $id; + + // If we do not have an uri, we need to generate one + if (isset($data['uri']) === false) { + $data['uri'] = $this->urlGenerator->linkToRoute('openCatalogi.themes.show', ['id' => $data['id']]); + $data = $this->objectService->saveObject('theme', $data); + } // Save the updated theme object $object = $this->objectService->saveObject('theme', $data); diff --git a/lib/Db/Attachment.php b/lib/Db/Attachment.php index dd55e64a..5aa72062 100644 --- a/lib/Db/Attachment.php +++ b/lib/Db/Attachment.php @@ -9,6 +9,7 @@ class Attachment extends Entity implements JsonSerializable { protected ?string $uuid = null; + protected ?string $uri = null; protected ?string $version = '0.0.1'; protected ?string $reference = null; protected ?string $title = null; @@ -32,6 +33,7 @@ class Attachment extends Entity implements JsonSerializable public function __construct() { $this->addType(fieldName: 'uuid', type: 'string'); + $this->addType(fieldName: 'uri', type: 'string'); $this->addType(fieldName: 'version', type: 'string'); $this->addType(fieldName: 'reference', type: 'string'); $this->addType(fieldName: 'title', type: 'string'); @@ -96,6 +98,7 @@ public function jsonSerialize(): array $array = [ 'id' => $this->id, 'uuid' => $this->uuid, + 'uri' => $this->uri, 'version' => $this->version, 'reference' => $this->reference, 'title' => $this->title, diff --git a/lib/Db/AttachmentMapper.php b/lib/Db/AttachmentMapper.php index f5eec661..c24bb802 100644 --- a/lib/Db/AttachmentMapper.php +++ b/lib/Db/AttachmentMapper.php @@ -9,6 +9,7 @@ use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use OCP\IURLGenerator; use Symfony\Component\Uid\Uuid; /** @@ -25,8 +26,9 @@ class AttachmentMapper extends QBMapper * Constructor for AttachmentMapper * * @param IDBConnection $db The database connection + * @param IURLGenerator $urlGenerator The URL generator */ - public function __construct(IDBConnection $db) + public function __construct(IDBConnection $db, IURLGenerator $urlGenerator) { parent::__construct($db, tableName: 'ocat_attachments'); } @@ -105,6 +107,8 @@ public function findAll(int $limit = null, int $offset = null, array $filters = public function createFromArray(array $object): Attachment { $attachment = new Attachment(); + + // Hydrate the attachment with the new data $attachment->hydrate(object: $object); // Set uuid if not provided @@ -112,6 +116,9 @@ public function createFromArray(array $object): Attachment $attachment->setUuid(Uuid::v4()); } + // Set the uri + $attachment->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('opencatalogi.attachments.show', ['id' => $attachment->getUuid()]))); + return $this->insert(entity: $attachment); } @@ -135,8 +142,12 @@ public function updateFromArray(int $id, array $object, bool $updateVersion = tr return $this->createFromArray($object); } + // Hydrate the attachment with the new data $attachment->hydrate($object); + // Set the uri + $attachment->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('opencatalogi.attachments.show', ['id' => $attachment->getUuid()]))); + if ($updateVersion === true) { // Update the version $version = explode('.', $attachment->getVersion()); diff --git a/lib/Db/Catalog.php b/lib/Db/Catalog.php index bc24ca9d..20aed8db 100644 --- a/lib/Db/Catalog.php +++ b/lib/Db/Catalog.php @@ -9,6 +9,7 @@ class Catalog extends Entity implements JsonSerializable { protected ?string $uuid = null; + protected ?string $uri = null; protected ?string $version = '0.0.1'; protected ?string $title = null; protected ?string $summary = null; @@ -23,6 +24,7 @@ class Catalog extends Entity implements JsonSerializable public function __construct() { $this->addType(fieldName: 'uuid', type: 'string'); + $this->addType(fieldName: 'uri', type: 'string'); $this->addType(fieldName: 'version', type: 'string'); $this->addType(fieldName: 'title', type: 'string'); $this->addType(fieldName: 'summary', type: 'string'); @@ -78,6 +80,7 @@ public function jsonSerialize(): array { $array = [ 'id' => $this->id, + 'uri' => $this->uri, 'uuid' => $this->uuid, 'version' => $this->version, 'title' => $this->title, diff --git a/lib/Db/CatalogMapper.php b/lib/Db/CatalogMapper.php index 825597de..9670e9d9 100644 --- a/lib/Db/CatalogMapper.php +++ b/lib/Db/CatalogMapper.php @@ -7,6 +7,7 @@ use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use OCP\IURLGenerator; use Symfony\Component\Uid\Uuid; /** @@ -23,8 +24,9 @@ class CatalogMapper extends QBMapper * Constructor for CatalogMapper * * @param IDBConnection $db The database connection + * @param IURLGenerator $urlGenerator The URL generator */ - public function __construct(IDBConnection $db) + public function __construct(IDBConnection $db, IURLGenerator $urlGenerator) { parent::__construct($db, tableName: 'ocat_catalogi'); } @@ -128,6 +130,9 @@ public function createFromArray(array $object): Catalog $catalog->setUuid(Uuid::v4()); } + // Set the uri + $catalog->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('opencatalogi.catalogs.show', ['id' => $catalog->getUuid()]))); + return $this->insert(entity: $catalog); } @@ -149,8 +154,12 @@ public function updateFromArray(int $id, array $object, bool $updateVersion = tr return $this->createFromArray($object); } + // Hydrate the catalog with the new data $catalog->hydrate($object); + // Set the uri + $catalog->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('opencatalogi.catalogs.show', ['id' => $catalog->getUuid()]))); + if ($updateVersion === true) { // Update the version $version = explode('.', $catalog->getVersion()); diff --git a/lib/Db/Organization.php b/lib/Db/Organization.php index 8d227c7a..9e46735e 100644 --- a/lib/Db/Organization.php +++ b/lib/Db/Organization.php @@ -9,6 +9,7 @@ class Organization extends Entity implements JsonSerializable { protected ?string $uuid = null; + protected ?string $uri = null; protected ?string $version = '0.0.1'; protected ?string $title = null; protected ?string $summary = null; @@ -24,6 +25,7 @@ class Organization extends Entity implements JsonSerializable public function __construct() { $this->addType(fieldName: 'uuid', type: 'string'); + $this->addType(fieldName: 'uri', type: 'string'); $this->addType(fieldName: 'version', type: 'string'); $this->addType(fieldName: 'title', type: 'string'); $this->addType(fieldName: 'summary', type: 'string'); @@ -79,6 +81,7 @@ public function jsonSerialize(): array $array = [ 'id' => $this->id, 'uuid' => $this->uuid, + 'uri' => $this->uri, 'version' => $this->version, 'title' => $this->title, 'summary' => $this->summary, diff --git a/lib/Db/OrganizationMapper.php b/lib/Db/OrganizationMapper.php index c83a859c..b6a0841c 100644 --- a/lib/Db/OrganizationMapper.php +++ b/lib/Db/OrganizationMapper.php @@ -7,6 +7,7 @@ use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use OCP\IURLGenerator; use Symfony\Component\Uid\Uuid; /** @@ -23,6 +24,7 @@ class OrganizationMapper extends QBMapper * Constructor for OrganizationMapper * * @param IDBConnection $db The database connection + * @param IURLGenerator $urlGenerator The URL generator */ public function __construct(IDBConnection $db) { @@ -123,6 +125,10 @@ public function createFromArray(array $object): Organization if ($organization->getUuid() === null) { $organization->setUuid(Uuid::v4()); } + + // Set the uri + $organization->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('opencatalogi.organizations.show', ['id' => $organization->getUuid()]))); + return $this->insert(entity: $organization); } @@ -144,8 +150,12 @@ public function updateFromArray(int $id, array $object, bool $updateVersion = tr return $this->createFromArray($object); } + // Hydrate the organization with the new data $organization->hydrate($object); + // Set the uri + $organization->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('opencatalogi.organizations.show', ['id' => $organization->getUuid()]))); + if ($updateVersion === true) { // Update the version $version = explode('.', $organization->getVersion()); diff --git a/lib/Db/Publication.php b/lib/Db/Publication.php index 4b3d3e49..f82a17ec 100644 --- a/lib/Db/Publication.php +++ b/lib/Db/Publication.php @@ -9,6 +9,7 @@ class Publication extends Entity implements JsonSerializable { protected ?string $uuid = null; + protected ?string $uri = null; protected ?string $version = '0.0.1'; protected ?string $title = null; protected ?string $reference = null; @@ -39,6 +40,7 @@ class Publication extends Entity implements JsonSerializable public function __construct() { $this->addType(fieldName: 'uuid', type: 'string'); + $this->addType(fieldName: 'uri', type: 'string'); $this->addType(fieldName: 'version', type: 'string'); $this->addType(fieldName: 'title', type: 'string'); $this->addType(fieldName: 'reference', type: 'string'); @@ -125,6 +127,7 @@ public function jsonSerialize(): array { $array = [ 'id' => $this->id, + 'uri' => $this->uri, 'uuid' => $this->uuid, 'version' => $this->version, 'title' => $this->title, diff --git a/lib/Db/PublicationMapper.php b/lib/Db/PublicationMapper.php index c3ced4f0..dc59d07a 100644 --- a/lib/Db/PublicationMapper.php +++ b/lib/Db/PublicationMapper.php @@ -8,6 +8,7 @@ use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\Types; use OCP\IDBConnection; +use OCP\IURLGenerator; use Symfony\Component\Uid\Uuid; /** @@ -24,8 +25,9 @@ class PublicationMapper extends QBMapper * Constructor for PublicationMapper * * @param IDBConnection $db The database connection + * @param IURLGenerator $urlGenerator The URL generator */ - public function __construct(IDBConnection $db) + public function __construct(IDBConnection $db, IURLGenerator $urlGenerator) { parent::__construct($db, tableName: 'ocat_publications'); } @@ -212,6 +214,9 @@ public function createFromArray(array $object): Publication $publication->setUuid(Uuid::v4()); } + // Set the uri + $publication->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('opencatalogi.publications.show', ['id' => $publication->getUuid()]))); + return $this->insert(entity: $publication); } @@ -233,8 +238,12 @@ public function updateFromArray(int $id, array $object, bool $updateVersion = tr return $this->createFromArray($object); } + // Hydrate the publication with the new data $publication->hydrate(object: $object); + // Set the uri + $publication->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('opencatalogi.publications.show', ['id' => $publication->getUuid()]))); + if ($updateVersion === true) { // Update the version $version = explode('.', $publication->getVersion()); diff --git a/lib/Db/PublicationType.php b/lib/Db/PublicationType.php index cfc9885d..dd729b82 100644 --- a/lib/Db/PublicationType.php +++ b/lib/Db/PublicationType.php @@ -10,6 +10,7 @@ class PublicationType extends Entity implements JsonSerializable { protected ?string $uuid = null; + protected ?string $uri = null; protected ?string $version = '0.0.1'; protected ?string $title = null; protected ?string $description = null; @@ -23,6 +24,7 @@ class PublicationType extends Entity implements JsonSerializable public function __construct() { $this->addType(fieldName: 'uuid', type: 'string'); + $this->addType(fieldName: 'uri', type: 'string'); $this->addType(fieldName: 'version', type: 'string'); $this->addType(fieldName: 'title', type: 'string'); $this->addType(fieldName: 'description', type: 'string'); @@ -87,6 +89,7 @@ public function jsonSerialize(): array $array = [ 'id' => $this->id, + 'uri' => $this->uri, 'uuid' => $this->uuid, 'version' => $this->version, 'title' => $this->title, diff --git a/lib/Db/PublicationTypeMapper.php b/lib/Db/PublicationTypeMapper.php index 7a2cc336..b32b31da 100644 --- a/lib/Db/PublicationTypeMapper.php +++ b/lib/Db/PublicationTypeMapper.php @@ -9,6 +9,7 @@ use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use OCP\IURLGenerator; use Symfony\Component\Uid\Uuid; /** @@ -23,8 +24,9 @@ class PublicationTypeMapper extends QBMapper * Constructor for PublicationTypeMapper * * @param IDBConnection $db The database connection + * @param IURLGenerator $urlGenerator The URL generator */ - public function __construct(IDBConnection $db) + public function __construct(IDBConnection $db, IURLGenerator $urlGenerator) { parent::__construct($db, tableName: 'ocat_publication_types'); } @@ -141,6 +143,10 @@ public function createFromArray(array $object): PublicationType if ($publicationType->getUuid() === null) { $publicationType->setUuid(Uuid::v4()); } + + // Set the uri + $publicationType->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('opencatalogi.publication_types.show', ['id' => $publicationType->getUuid()]))); + return $this->insert(entity: $publicationType); } @@ -164,8 +170,12 @@ public function updateFromArray(int $id, array $object, bool $updateVersion = tr return $this->createFromArray($object); } + // Hydrate the publication type with the new data $publicationType->hydrate($object); + // Set the uri + $publicationType->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('opencatalogi.publication_types.show', ['id' => $publicationType->getUuid()]))); + if ($updateVersion === true) { // Update the version $version = explode('.', $publicationType->getVersion()); diff --git a/lib/Db/Theme.php b/lib/Db/Theme.php index 881fdf95..8451c217 100644 --- a/lib/Db/Theme.php +++ b/lib/Db/Theme.php @@ -9,6 +9,7 @@ class Theme extends Entity implements JsonSerializable { protected ?string $uuid = null; + protected ?string $uri = null; protected ?string $version = '0.0.1'; protected ?string $title = null; protected ?string $summary = null; @@ -19,6 +20,7 @@ class Theme extends Entity implements JsonSerializable public function __construct() { $this->addType(fieldName: 'uuid', type: 'string'); + $this->addType(fieldName: 'uri', type: 'string'); $this->addType(fieldName: 'version', type: 'string'); $this->addType(fieldName: 'title', type: 'string'); $this->addType(fieldName: 'summary', type: 'string'); @@ -70,6 +72,7 @@ public function jsonSerialize(): array { $array = [ 'id' => $this->id, + 'uri' => $this->uri, 'uuid' => $this->uuid, 'version' => $this->version, 'title' => $this->title, diff --git a/lib/Db/ThemeMapper.php b/lib/Db/ThemeMapper.php index 272159ea..47f9c506 100644 --- a/lib/Db/ThemeMapper.php +++ b/lib/Db/ThemeMapper.php @@ -9,6 +9,7 @@ use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use OCP\IURLGenerator; use Symfony\Component\Uid\Uuid; /** @@ -25,8 +26,9 @@ class ThemeMapper extends QBMapper * Constructor for ThemeMapper * * @param IDBConnection $db The database connection + * @param IURLGenerator $urlGenerator The URL generator */ - public function __construct(IDBConnection $db) + public function __construct(IDBConnection $db, IURLGenerator $urlGenerator) { parent::__construct($db, tableName: 'ocat_themes'); } @@ -127,6 +129,10 @@ public function createFromArray(array $object): Theme if ($theme->getUuid() === null) { $theme->setUuid(Uuid::v4()); } + + // Set the uri + $theme->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('opencatalogi.themes.show', ['id' => $theme->getUuid()]))); + return $this->insert(entity: $theme); } @@ -150,7 +156,12 @@ public function updateFromArray(int $id, array $object, bool $updateVersion = tr return $this->createFromArray($object); } + // Hydrate the theme with the new data $theme->hydrate($object); + + // Set the uri + $theme->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('opencatalogi.themes.show', ['id' => $theme->getUuid()]))); + if ($updateVersion === true) { // Update the version diff --git a/lib/Migration/Version6Date20241208222530.php b/lib/Migration/Version6Date20241208222530.php new file mode 100644 index 00000000..33ecb6c5 --- /dev/null +++ b/lib/Migration/Version6Date20241208222530.php @@ -0,0 +1,139 @@ +getTable('ocat_attachments'); + if ($table->hasColumn('uri') === false) { + $table->addColumn( + name: 'uri', + typeName: Types::STRING, + options: [ + 'notnull' => true, + 'length' => 255 + ] + )->setDefault(''); + } + + // Update catalogi table + $table = $schema->getTable('ocat_catalogi'); + if ($table->hasColumn('uri') === false) { + $table->addColumn( + name: 'uri', + typeName: Types::STRING, + options: [ + 'notnull' => true, + 'length' => 255 + ] + )->setDefault(''); + } + + // Update organizations table + $table = $schema->getTable('ocat_organizations'); + if ($table->hasColumn('uri') === false) { + $table->addColumn( + name: 'uri', + typeName: Types::STRING, + options: [ + 'notnull' => true, + 'length' => 255 + ] + )->setDefault(''); + } + + // Update publications table + $table = $schema->getTable('ocat_publications'); + if ($table->hasColumn('uri') === false) { + $table->addColumn( + name: 'uri', + typeName: Types::STRING, + options: [ + 'notnull' => true, + 'length' => 255 + ] + )->setDefault(''); + } + + // Update publication types table + $table = $schema->getTable('ocat_publication_types'); + if ($table->hasColumn('uri') === false) { + $table->addColumn( + name: 'uri', + typeName: Types::STRING, + options: [ + 'notnull' => true, + 'length' => 255 + ] + )->setDefault(''); + if (!$table->hasIndex('ocat_publication_uuid_index')) { + $table->addIndex(['uuid'], 'ocat_publication_uuid_index'); + } + } + + // Update themes table + $table = $schema->getTable('ocat_themes'); + if ($table->hasColumn('uri') === false) { + $table->addColumn( + name: 'uri', + typeName: Types::STRING, + options: [ + 'notnull' => true, + 'length' => 255 + ] + )->setDefault(''); + if (!$table->hasIndex('ocat_themes_uuid_index')) { + $table->addIndex(['uuid'], 'ocat_themes_uuid_index'); + } + } + + return $schema; + } + + /** + * @param IOutput $output + * @param Closure(): ISchemaWrapper $schemaClosure + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + } +}