diff --git a/lib/Controller/MetaDataController.php b/lib/Controller/MetaDataController.php index 282ac304..fb72f8a7 100644 --- a/lib/Controller/MetaDataController.php +++ b/lib/Controller/MetaDataController.php @@ -11,6 +11,7 @@ use OCP\AppFramework\Http\JSONResponse; use OCP\IAppConfig; use OCP\IRequest; +use OCP\IURLGenerator; class MetaDataController extends Controller { @@ -43,6 +44,7 @@ public function page(?string $getParameter) } /** + * @PublicPage * @NoAdminRequired * @NoCSRFRequired */ @@ -77,6 +79,7 @@ public function index(ObjectService $objectService, SearchService $searchService } /** + * @PublicPage * @NoAdminRequired * @NoCSRFRequired */ @@ -107,13 +110,15 @@ public function show(string|int $id, ObjectService $objectService): JSONResponse * @NoAdminRequired * @NoCSRFRequired */ - public function create(ObjectService $objectService): JSONResponse + public function create(ObjectService $objectService, IURLGenerator $urlGenerator): JSONResponse { $data = $this->request->getParams(); // Remove fields we should never post unset($data['id']); + + foreach($data as $key => $value) { if(str_starts_with($key, '_')) { unset($data[$key]); @@ -123,7 +128,18 @@ public function create(ObjectService $objectService): JSONResponse if($this->config->hasKey($this->appName, 'mongoStorage') === false || $this->config->getValueString($this->appName, 'mongoStorage') !== '1' ) { - return new JSONResponse($this->metaDataMapper->createFromArray(object: $data)); + $object = $this->metaDataMapper->createFromArray(object: $data); + + $id = $object->getId(); + + if($object->getSource() === null) { + $source = $urlGenerator->getAbsoluteURL($urlGenerator->linkToRoute(routeName:"opencatalogi.metadata.show", arguments: ['id' => $id])); + $object->setSource($source); + $this->metaDataMapper->update($object); + } + + + return new JSONResponse($object); } $dbConfig['base_uri'] = $this->config->getValueString(app: $this->appName, key: 'mongodbLocation'); $dbConfig['headers']['api-key'] = $this->config->getValueString(app: $this->appName, key: 'mongodbKey'); @@ -136,6 +152,14 @@ public function create(ObjectService $objectService): JSONResponse config: $dbConfig ); + if(isset($data['source']) === false || $data['source'] === null) { + $returnData['source'] = $urlGenerator->getAbsoluteURL($urlGenerator->linkToRoute(routeName:"opencatalogi.metadata.show", arguments: ['id' => $returnData['id']])); + $returnData = $objectService->saveObject( + data: $data, + config: $dbConfig + ); + } + // get post from requests return new JSONResponse($returnData); } @@ -149,7 +173,7 @@ public function update(string|int $id, ObjectService $objectService): JSONRespon $data = $this->request->getParams(); // Remove fields we should never post - unset($data['id']); + unset($data['id'],$data['source']); foreach($data as $key => $value) { if(str_starts_with($key, '_')) { unset($data[$key]); diff --git a/lib/Db/MetaData.php b/lib/Db/MetaData.php index 759cf14c..1f7d3433 100644 --- a/lib/Db/MetaData.php +++ b/lib/Db/MetaData.php @@ -12,8 +12,9 @@ class MetaData extends Entity implements JsonSerializable protected ?string $title = null; protected ?string $version = null; protected ?string $description = null; - protected ?array $required = []; - protected ?array $properties = []; + protected ?array $required = []; + protected ?array $properties = []; + protected ?string $source = null; public function __construct() { $this->addType(fieldName: 'title', type: 'string'); @@ -63,6 +64,7 @@ public function jsonSerialize(): array 'description' => $this->description, 'required' => $this->required, 'properties' => $this->properties, + 'source' => $this->source, ]; $jsonFields = $this->getJsonFields(); diff --git a/lib/Migration/Version6Date20240809141351.php b/lib/Migration/Version6Date20240809141351.php new file mode 100644 index 00000000..8f8563ee --- /dev/null +++ b/lib/Migration/Version6Date20240809141351.php @@ -0,0 +1,69 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCA\OpenCatalogi\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\DB\Types; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +/** + * FIXME Auto-generated migration step: Please modify to your needs! + */ +class Version6Date20240809141351 extends SimpleMigrationStep { + + /** + * @param IOutput $output + * @param Closure(): ISchemaWrapper $schemaClosure + * @param array $options + */ + public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + } + + /** + * @param IOutput $output + * @param Closure(): ISchemaWrapper $schemaClosure + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** + * @var ISchemaWrapper $schema + */ + $schema = $schemaClosure(); + + if($schema->hasTable(tableName: 'metadata') === true) { + $table = $schema->getTable(tableName: 'metadata'); + + if($table->hasColumn(name: 'source') === false) { + $table->addColumn( + name: 'source', + typeName: Types::STRING, + options: [ + 'notNull' => false, + 'default' => null + ]); + $output->info('source should be added to metadata'); + } + + } + + return $schema; + } + + /** + * @param IOutput $output + * @param Closure(): ISchemaWrapper $schemaClosure + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + } +}