Skip to content

Commit

Permalink
Add source to metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
rjzondervan committed Aug 9, 2024
1 parent fba074a commit 142ae0d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
30 changes: 27 additions & 3 deletions lib/Controller/MetaDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;
use OCP\IURLGenerator;

class MetaDataController extends Controller
{
Expand Down Expand Up @@ -43,6 +44,7 @@ public function page(?string $getParameter)
}

/**
* @PublicPage
* @NoAdminRequired
* @NoCSRFRequired
*/
Expand Down Expand Up @@ -77,6 +79,7 @@ public function index(ObjectService $objectService, SearchService $searchService
}

/**
* @PublicPage
* @NoAdminRequired
* @NoCSRFRequired
*/
Expand Down Expand Up @@ -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]);
Expand All @@ -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');
Expand All @@ -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);
}
Expand All @@ -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]);
Expand Down
6 changes: 4 additions & 2 deletions lib/Db/MetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -63,6 +64,7 @@ public function jsonSerialize(): array
'description' => $this->description,
'required' => $this->required,
'properties' => $this->properties,
'source' => $this->source,
];

$jsonFields = $this->getJsonFields();
Expand Down
69 changes: 69 additions & 0 deletions lib/Migration/Version6Date20240809141351.php
Original file line number Diff line number Diff line change
@@ -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 {
}
}

0 comments on commit 142ae0d

Please sign in to comment.