diff --git a/src/controller/TypeDefinitionController.ts b/src/controller/TypeDefinitionController.ts index 730c66b68879cda4370852429fe2e95f66c69b44..f070118656ec47989c7e6b720ade9a74a80707e5 100644 --- a/src/controller/TypeDefinitionController.ts +++ b/src/controller/TypeDefinitionController.ts @@ -18,11 +18,11 @@ export default class TypeDefinitionController extends BaseController { getTypeDefinition = async (request: Request, response: Response) => { this.repository.findOne({ name: request.params.name }).then((type) => { + this.setJSONLDResponseType(response); if(type) { - response.send(type); + response.send(type.toJSONLD()); } else { response.status(404); - this.setJSONLDResponseType(response); response.send(new APIError("Type Definition not found.")); } }).catch((error) => { @@ -40,14 +40,15 @@ export default class TypeDefinitionController extends BaseController { const pageSize = Math.max(Number(request.query.pageSize ?? "500"), 1); TypeDefinition.search(name ?? "", pageSize, page, this.repository).then((results) => { + this.setJSONLDResponseType(response); + if(results.length == 0) { response.status(404); - this.setJSONLDResponseType(response); response.send(new APIError("No Type Definition found.")); return; } - response.send(results); + response.send(results.map((type) => type.toJSONLD())); }).catch((error) => { logger.error(`Error while searching type definitions: ${JSON.stringify(error)}`); response.status(400); @@ -78,7 +79,8 @@ export default class TypeDefinitionController extends BaseController { }).then(() => { return this.repository.findOneOrFail({ name: request.body.name }); }).then((newDefinition) => { - response.send(newDefinition); + this.setJSONLDResponseType(response); + response.send(newDefinition.toJSONLD()); }).catch((error) => { logger.error(`Error while saving new type definition: ${error}`); // 23505 is the postgres error code for violation of unique constraints diff --git a/src/entity/ComponentInformation.ts b/src/entity/ComponentInformation.ts index 82c5801084f5f1456f27c50059f2ab6726e3ee8c..ec847320450772790ca9ecd8df5baf18df5a5a99 100644 --- a/src/entity/ComponentInformation.ts +++ b/src/entity/ComponentInformation.ts @@ -174,7 +174,7 @@ export default class ComponentInformation { "dateCreated": this.dateCreated.toISOString(), "name": this.name, "comment": this.comment, - "metadata": this.type.toJSONLD(this.metadata), + "metadata": this.type.toJSONLDWithData(this.metadata), "topic": this.topic, "component": { "@id": `${config.baseURL}/component/${this.component.id.toString()}` }, "informationLicense": this.informationLicense, diff --git a/src/entity/TypeDefinition.ts b/src/entity/TypeDefinition.ts index c458db8948f4f7de6937da2e27d2abfef4492a17..081efdc50fe1d913f110f91436d986d7f2a72cb5 100644 --- a/src/entity/TypeDefinition.ts +++ b/src/entity/TypeDefinition.ts @@ -4,7 +4,8 @@ import { Index, PrimaryColumn, Column, - Repository + Repository, + CreateDateColumn } from "typeorm"; import ComponentInformation from "./ComponentInformation"; import { Schema } from "ajv"; @@ -13,6 +14,7 @@ import ajv from "../services/SchemaValidationService"; import logService from "../services/logger"; import config from "../config"; import { ContextDefinition, NodeObject } from "jsonld"; +import ContextDefinitions from "../util/ContextDefinitions"; const logger = logService(module); @@ -23,6 +25,11 @@ export default class TypeDefinition { @PrimaryColumn({ unique: true }) name!: string; + @CreateDateColumn({ + type: "timestamptz" + }) + dateCreated!: Date; + @Column() comment!: string; @@ -104,7 +111,7 @@ export default class TypeDefinition { return ajv.validate(this.schema, data); } - toJSONLD(data: Record<string, unknown>): NodeObject { + toJSONLDWithData(data: Record<string, unknown>): NodeObject { const context = this.context as ContextDefinition; const result: NodeObject = { @@ -116,4 +123,19 @@ export default class TypeDefinition { return result; } + toJSONLD(): NodeObject { + const context = ContextDefinitions.typeDefinition; + + const result: NodeObject = { + "@context": context, + "dateCreated": this.dateCreated.toISOString(), + "comment": this.comment, + "schema": this.schema as NodeObject, + "context": this.context as NodeObject, + "license": this.license + }; + + return result; + } + } \ No newline at end of file diff --git a/src/util/ContextDefinitions.ts b/src/util/ContextDefinitions.ts index bca51469ce70078f520ab39a7d1d02285a2e3f07..28e217a61ffa16078cb0f158e1acf2478fc565e9 100644 --- a/src/util/ContextDefinitions.ts +++ b/src/util/ContextDefinitions.ts @@ -52,9 +52,18 @@ const measurement = function(valueContext: unknown, metadataContext: unknown | u return result; }; +const typeDefinition = { + "name": { "@id": "https://schema.org/url", "@type": "@id"}, + "dateCreated": "https://schema.org/DateTime", + "comment": "http://schema.org/comment", + "schema": "https://json-schema.org/draft/2020-12/json-schema-core.html", + "license": "https://schema.org/license", +}; + export default { component, componentRelation, information, - measurement + measurement, + typeDefinition }; \ No newline at end of file