Skip to content

Commit

Permalink
Add uri to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvdlinde committed Dec 8, 2024
1 parent 6e83bd9 commit 8d88d0a
Show file tree
Hide file tree
Showing 19 changed files with 299 additions and 9 deletions.
13 changes: 13 additions & 0 deletions lib/Controller/AttachmentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions lib/Controller/CatalogiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);

Expand Down
14 changes: 13 additions & 1 deletion lib/Controller/OrganizationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;

use OCP\IURLGenerator;
/**
* Class OrganizationsController
*
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);

Expand Down
13 changes: 12 additions & 1 deletion lib/Controller/PublicationTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;

use OCP\IURLGenerator;
/**
* Class PublicationTypesController
*
Expand Down Expand Up @@ -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();

Expand All @@ -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);

Expand Down
12 changes: 12 additions & 0 deletions lib/Controller/PublicationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);

Expand Down
16 changes: 14 additions & 2 deletions lib/Controller/ThemesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;

use OCP\IURLGenerator;
/**
* Class ThemesController
*
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions lib/Db/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 12 additions & 1 deletion lib/Db/AttachmentMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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');
}
Expand Down Expand Up @@ -105,13 +107,18 @@ 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
if ($attachment->getUuid() === null) {
$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);
}

Expand All @@ -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());
Expand Down
3 changes: 3 additions & 0 deletions lib/Db/Catalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
Expand Down Expand Up @@ -78,6 +80,7 @@ public function jsonSerialize(): array
{
$array = [
'id' => $this->id,
'uri' => $this->uri,
'uuid' => $this->uuid,
'version' => $this->version,
'title' => $this->title,
Expand Down
11 changes: 10 additions & 1 deletion lib/Db/CatalogMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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');
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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());
Expand Down
3 changes: 3 additions & 0 deletions lib/Db/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 8d88d0a

Please sign in to comment.