From 4c5112cce7322e40c662e934c2e3ab7f6a568894 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 13 Feb 2024 17:31:26 +0100 Subject: [PATCH] mapping @id to id --- matsen-tool/openapi.yaml | 105 ++++++++++-------- matsen-tool/src/app/_helpers/api.helper.ts | 13 +++ .../src/app/_helpers/property.interceptor.ts | 59 ++++++++++ matsen-tool/src/app/app.module.ts | 2 + .../src/app/core/api/v1/model/contact.ts | 3 +- .../app/core/api/v1/model/contactJsonhal.ts | 3 +- .../app/core/api/v1/model/contactJsonld.ts | 3 +- .../src/app/core/api/v1/model/partner.ts | 10 +- .../app/core/api/v1/model/partnerJsonhal.ts | 10 +- .../app/core/api/v1/model/partnerJsonld.ts | 11 +- .../partners-detail.component.ts | 2 +- .../src/app/partners/partners.component.ts | 10 +- 12 files changed, 159 insertions(+), 72 deletions(-) create mode 100644 matsen-tool/src/app/_helpers/api.helper.ts create mode 100644 matsen-tool/src/app/_helpers/property.interceptor.ts diff --git a/matsen-tool/openapi.yaml b/matsen-tool/openapi.yaml index 464096c..0845ef6 100644 --- a/matsen-tool/openapi.yaml +++ b/matsen-tool/openapi.yaml @@ -1119,11 +1119,12 @@ components: - string - 'null' partner: - anyOf: - - - $ref: '#/components/schemas/Partner' - - - type: 'null' + 'owl:maxCardinality': 1 + type: + - string + - 'null' + format: iri-reference + example: 'https://example.com/' birthday: type: - string @@ -1179,11 +1180,12 @@ components: - string - 'null' partner: - anyOf: - - - $ref: '#/components/schemas/Partner.jsonhal' - - - type: 'null' + 'owl:maxCardinality': 1 + type: + - string + - 'null' + format: iri-reference + example: 'https://example.com/' birthday: type: - string @@ -1253,11 +1255,12 @@ components: - string - 'null' partner: - anyOf: - - - $ref: '#/components/schemas/Partner.jsonld' - - - type: 'null' + 'owl:maxCardinality': 1 + type: + - string + - 'null' + format: iri-reference + example: 'https://example.com/' birthday: type: - string @@ -1362,10 +1365,10 @@ components: type: object description: '' deprecated: false + required: + - name + - type properties: - id: - readOnly: true - type: integer name: type: string type: @@ -1398,13 +1401,6 @@ components: type: - string - 'null' - createdAt: - type: string - format: date-time - contacts: - type: array - items: - $ref: '#/components/schemas/Contact' logo: 'owl:maxCardinality': 1 type: @@ -1412,10 +1408,24 @@ components: - 'null' format: iri-reference example: 'https://example.com/' + createdAt: + type: + - string + - 'null' + format: date-time + contacts: + type: array + items: + type: string + format: iri-reference + example: 'https://example.com/' Partner.jsonhal: type: object description: '' deprecated: false + required: + - name + - type properties: _links: type: object @@ -1426,9 +1436,6 @@ components: href: type: string format: iri-reference - id: - readOnly: true - type: integer name: type: string type: @@ -1461,13 +1468,6 @@ components: type: - string - 'null' - createdAt: - type: string - format: date-time - contacts: - type: array - items: - $ref: '#/components/schemas/Contact.jsonhal' logo: 'owl:maxCardinality': 1 type: @@ -1475,10 +1475,24 @@ components: - 'null' format: iri-reference example: 'https://example.com/' + createdAt: + type: + - string + - 'null' + format: date-time + contacts: + type: array + items: + type: string + format: iri-reference + example: 'https://example.com/' Partner.jsonld: type: object description: '' deprecated: false + required: + - name + - type properties: '@context': readOnly: true @@ -1503,9 +1517,6 @@ components: '@type': readOnly: true type: string - id: - readOnly: true - type: integer name: type: string type: @@ -1538,13 +1549,6 @@ components: type: - string - 'null' - createdAt: - type: string - format: date-time - contacts: - type: array - items: - $ref: '#/components/schemas/Contact.jsonld' logo: 'owl:maxCardinality': 1 type: @@ -1552,6 +1556,17 @@ components: - 'null' format: iri-reference example: 'https://example.com/' + createdAt: + type: + - string + - 'null' + format: date-time + contacts: + type: array + items: + type: string + format: iri-reference + example: 'https://example.com/' Post: type: object description: '' diff --git a/matsen-tool/src/app/_helpers/api.helper.ts b/matsen-tool/src/app/_helpers/api.helper.ts new file mode 100644 index 0000000..3a7a31a --- /dev/null +++ b/matsen-tool/src/app/_helpers/api.helper.ts @@ -0,0 +1,13 @@ +export class ApiHelper { + + public static extractId(iri: string | undefined) { + if (iri !== undefined) { + const iriRegex = /\/(\d+)$/; + const match = iri.match(iriRegex); + if (match && match[1]) { + return match[1]; + } + } + return undefined; + } +} \ No newline at end of file diff --git a/matsen-tool/src/app/_helpers/property.interceptor.ts b/matsen-tool/src/app/_helpers/property.interceptor.ts new file mode 100644 index 0000000..e64ebaf --- /dev/null +++ b/matsen-tool/src/app/_helpers/property.interceptor.ts @@ -0,0 +1,59 @@ +// property.interceptor.ts + +import { Injectable } from '@angular/core'; +import { + HttpInterceptor, + HttpRequest, + HttpHandler, + HttpEvent, HttpResponse, +} from '@angular/common/http'; +import { Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; + +@Injectable() +export class PropertyInterceptor implements HttpInterceptor { + intercept( + request: HttpRequest, + next: HttpHandler + ): Observable> { + return next.handle(request).pipe( + map((event) => { + if (event instanceof HttpResponse && event.body) { + let modifiedBody; + + if (event.body['hydra:member']) { + modifiedBody = { + ...event.body, + 'hydra:member': this.mapDataItems( + event.body['hydra:member'] + ), + }; + } else { + // Wenn keine 'hydra:member' vorhanden ist, betrachte das gesamte Objekt + modifiedBody = this.mapDataItem(event.body); + } + + return event.clone({ + body: modifiedBody, + }); + } + + return event; + }) + ); + } + + private mapDataItems(items: any[]): any[] { + return items.map((item) => this.mapDataItem(item)); + } + + private mapDataItem(item: any): any { + if (item && item['@id']) { + return { + ...item, + id: item['@id'], + }; + } + return item; + } +} diff --git a/matsen-tool/src/app/app.module.ts b/matsen-tool/src/app/app.module.ts index 35f0b6f..cbcf247 100644 --- a/matsen-tool/src/app/app.module.ts +++ b/matsen-tool/src/app/app.module.ts @@ -24,6 +24,7 @@ import { ProductsDetailComponent } from './products/products-detail/products-det import { DocumentsComponent } from './documents/documents.component'; import { NewContactComponent } from './partners/new-contact/new-contact.component'; import { ModalComponent } from './_components/modal/modal.component'; +import {PropertyInterceptor} from "@app/_helpers/property.interceptor"; export function apiConfigFactory(): Configuration { const params: ConfigurationParameters = { @@ -74,6 +75,7 @@ export function HttpLoaderFactory(http: HttpClient) { providers: [ {provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true}, {provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true}, + {provide: HTTP_INTERCEPTORS, useClass: PropertyInterceptor, multi: true}, ], bootstrap: [AppComponent] diff --git a/matsen-tool/src/app/core/api/v1/model/contact.ts b/matsen-tool/src/app/core/api/v1/model/contact.ts index fea5b3b..90458c6 100644 --- a/matsen-tool/src/app/core/api/v1/model/contact.ts +++ b/matsen-tool/src/app/core/api/v1/model/contact.ts @@ -9,7 +9,6 @@ * https://openapi-generator.tech * Do not edit the class manually. */ -import { Partner } from './partner'; /** @@ -18,7 +17,7 @@ import { Partner } from './partner'; export interface Contact { firstName: string | null; lastName: string | null; - partner?: Partner | null; + partner?: string | null; birthday?: string | null; image?: string | null; position?: string | null; diff --git a/matsen-tool/src/app/core/api/v1/model/contactJsonhal.ts b/matsen-tool/src/app/core/api/v1/model/contactJsonhal.ts index 35cac2b..39e21c8 100644 --- a/matsen-tool/src/app/core/api/v1/model/contactJsonhal.ts +++ b/matsen-tool/src/app/core/api/v1/model/contactJsonhal.ts @@ -10,7 +10,6 @@ * Do not edit the class manually. */ import { ContactJsonhalLinks } from './contactJsonhalLinks'; -import { PartnerJsonhal } from './partnerJsonhal'; /** @@ -20,7 +19,7 @@ export interface ContactJsonhal { _links?: ContactJsonhalLinks; firstName: string | null; lastName: string | null; - partner?: PartnerJsonhal | null; + partner?: string | null; birthday?: string | null; image?: string | null; position?: string | null; diff --git a/matsen-tool/src/app/core/api/v1/model/contactJsonld.ts b/matsen-tool/src/app/core/api/v1/model/contactJsonld.ts index fffb681..e261272 100644 --- a/matsen-tool/src/app/core/api/v1/model/contactJsonld.ts +++ b/matsen-tool/src/app/core/api/v1/model/contactJsonld.ts @@ -9,7 +9,6 @@ * https://openapi-generator.tech * Do not edit the class manually. */ -import { PartnerJsonld } from './partnerJsonld'; /** @@ -21,7 +20,7 @@ export interface ContactJsonld { readonly type?: string; firstName: string | null; lastName: string | null; - partner?: PartnerJsonld | null; + partner?: string | null; birthday?: string | null; image?: string | null; position?: string | null; diff --git a/matsen-tool/src/app/core/api/v1/model/partner.ts b/matsen-tool/src/app/core/api/v1/model/partner.ts index db11498..84e85e6 100644 --- a/matsen-tool/src/app/core/api/v1/model/partner.ts +++ b/matsen-tool/src/app/core/api/v1/model/partner.ts @@ -9,25 +9,23 @@ * https://openapi-generator.tech * Do not edit the class manually. */ -import { Contact } from './contact'; /** * */ export interface Partner { - readonly id?: number; - name?: string; - type?: Partner.TypeEnum; + name: string; + type: Partner.TypeEnum; street?: string | null; streetNo?: string | null; zip?: string | null; city?: string | null; country?: string | null; website?: string | null; - createdAt?: string; - contacts?: Array; logo?: string | null; + createdAt?: string | null; + contacts?: Array; } export namespace Partner { export type TypeEnum = 'customer' | 'supplier' | 'service'; diff --git a/matsen-tool/src/app/core/api/v1/model/partnerJsonhal.ts b/matsen-tool/src/app/core/api/v1/model/partnerJsonhal.ts index 0d25c18..774ec25 100644 --- a/matsen-tool/src/app/core/api/v1/model/partnerJsonhal.ts +++ b/matsen-tool/src/app/core/api/v1/model/partnerJsonhal.ts @@ -10,7 +10,6 @@ * Do not edit the class manually. */ import { ContactJsonhalLinks } from './contactJsonhalLinks'; -import { ContactJsonhal } from './contactJsonhal'; /** @@ -18,18 +17,17 @@ import { ContactJsonhal } from './contactJsonhal'; */ export interface PartnerJsonhal { _links?: ContactJsonhalLinks; - readonly id?: number; - name?: string; - type?: PartnerJsonhal.TypeEnum; + name: string; + type: PartnerJsonhal.TypeEnum; street?: string | null; streetNo?: string | null; zip?: string | null; city?: string | null; country?: string | null; website?: string | null; - createdAt?: string; - contacts?: Array; logo?: string | null; + createdAt?: string | null; + contacts?: Array; } export namespace PartnerJsonhal { export type TypeEnum = 'customer' | 'supplier' | 'service'; diff --git a/matsen-tool/src/app/core/api/v1/model/partnerJsonld.ts b/matsen-tool/src/app/core/api/v1/model/partnerJsonld.ts index d14a9de..3dffe02 100644 --- a/matsen-tool/src/app/core/api/v1/model/partnerJsonld.ts +++ b/matsen-tool/src/app/core/api/v1/model/partnerJsonld.ts @@ -9,7 +9,6 @@ * https://openapi-generator.tech * Do not edit the class manually. */ -import { ContactJsonld } from './contactJsonld'; /** @@ -17,18 +16,18 @@ import { ContactJsonld } from './contactJsonld'; */ export interface PartnerJsonld { context?: string | null; - readonly id?: number; - name?: string; - type?: PartnerJsonld.TypeEnum; + readonly id?: string; + name: string; + type: PartnerJsonld.TypeEnum; street?: string | null; streetNo?: string | null; zip?: string | null; city?: string | null; country?: string | null; website?: string | null; - createdAt?: string; - contacts?: Array; logo?: string | null; + createdAt?: string | null; + contacts?: Array; } export namespace PartnerJsonld { export type TypeEnum = 'customer' | 'supplier' | 'service'; diff --git a/matsen-tool/src/app/partners/partners-detail/partners-detail.component.ts b/matsen-tool/src/app/partners/partners-detail/partners-detail.component.ts index 73657fd..40e0d5d 100644 --- a/matsen-tool/src/app/partners/partners-detail/partners-detail.component.ts +++ b/matsen-tool/src/app/partners/partners-detail/partners-detail.component.ts @@ -36,7 +36,7 @@ export class PartnersDetailComponent implements OnInit { ) { this.id = ""; this.partnerDetailSub = new Subscription(); - this.partner = {}; + this.partner = {} as PartnerJsonld; this.partnerLogoSub = new Subscription(); this.partnerLogo = ""; this.partnerContactsSub = new Subscription(); diff --git a/matsen-tool/src/app/partners/partners.component.ts b/matsen-tool/src/app/partners/partners.component.ts index 75d3d03..297e2fa 100644 --- a/matsen-tool/src/app/partners/partners.component.ts +++ b/matsen-tool/src/app/partners/partners.component.ts @@ -6,6 +6,7 @@ import {Subscription} from "rxjs"; import {PartnerJsonld, PartnerService} from "@app/core/api/v1"; import {MatPaginator, MatPaginatorIntl, MatPaginatorModule, PageEvent} from "@angular/material/paginator"; import {OrderFilter} from "@app/_models/orderFilter"; +import {ApiHelper} from "@app/_helpers/api.helper"; @Component({ selector: 'app-partners', @@ -124,8 +125,13 @@ export class PartnersComponent implements OnInit, AfterViewInit { this.getData(); } - navigateToDetails(partner: any) { + navigateToDetails(element: any) { + const partner: PartnerJsonld = element as PartnerJsonld; // this.partnerService.setPartnerData(partner); - this.router.navigate(['/customers', partner.name]); + //console.log(partner['@id']); + console.log(ApiHelper.extractId(partner.id)); + this.router.navigate(['/customers', ApiHelper.extractId(partner.id)]); } + + protected readonly PartnerJsonld = PartnerJsonld; }