From 1c376b296413434a23d256268346741b071b082c Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Tue, 17 Oct 2023 16:28:36 +0200 Subject: [PATCH] CST-12174 Implemented data-service for the api calls --- .../admin-ldn-services.module.ts | 2 + .../ldn-service-form-edit.component.html | 6 +- .../ldn-service-form-edit.component.ts | 669 +++++++++--------- .../ldn-service-form.component.html | 2 +- .../ldn-service-form.component.ts | 313 ++++---- .../ldn-services-data.service.ts | 104 +-- .../ldn-services-directory.component.html | 6 +- .../ldn-services-directory.component.ts | 255 +++---- .../ldn-services-guard.service.ts | 2 +- .../ldn-service-constraint.model.ts | 26 - .../ldn-service-patterns.model.ts | 13 + .../ldn-service.constrain.model.ts | 3 + .../ldn-service.resource-type.ts | 1 + .../ldn-services-model/ldn-services.model.ts | 21 +- src/app/admin/admin-routing-paths.ts | 8 +- 15 files changed, 704 insertions(+), 727 deletions(-) delete mode 100644 src/app/admin/admin-ldn-services/ldn-services-model/ldn-service-constraint.model.ts create mode 100644 src/app/admin/admin-ldn-services/ldn-services-model/ldn-service-patterns.model.ts create mode 100644 src/app/admin/admin-ldn-services/ldn-services-model/ldn-service.constrain.model.ts diff --git a/src/app/admin/admin-ldn-services/admin-ldn-services.module.ts b/src/app/admin/admin-ldn-services/admin-ldn-services.module.ts index 1fd67d53b9..c3642ea88d 100644 --- a/src/app/admin/admin-ldn-services/admin-ldn-services.module.ts +++ b/src/app/admin/admin-ldn-services/admin-ldn-services.module.ts @@ -6,6 +6,7 @@ import { SharedModule } from '../../shared/shared.module'; import { LdnServiceNewComponent } from './ldn-service-new/ldn-service-new.component'; import { LdnServiceFormComponent } from './ldn-service-form/ldn-service-form.component'; import { LdnServiceFormEditComponent } from './ldn-service-form-edit/ldn-service-form-edit.component'; +import { FormsModule } from '@angular/forms'; @@ -14,6 +15,7 @@ import { LdnServiceFormEditComponent } from './ldn-service-form-edit/ldn-service CommonModule, SharedModule, AdminLdnServicesRoutingModule, + FormsModule ], declarations: [ LdnServicesOverviewComponent, diff --git a/src/app/admin/admin-ldn-services/ldn-service-form-edit/ldn-service-form-edit.component.html b/src/app/admin/admin-ldn-services/ldn-service-form-edit/ldn-service-form-edit.component.html index 516dce4137..988e0a53a1 100644 --- a/src/app/admin/admin-ldn-services/ldn-service-form-edit/ldn-service-form-edit.component.html +++ b/src/app/admin/admin-ldn-services/ldn-service-form-edit/ldn-service-form-edit.component.html @@ -112,7 +112,7 @@
-
@@ -176,7 +176,7 @@
-
@@ -215,7 +215,7 @@ {{ 'service.overview.delete.body' | translate }}
- -
diff --git a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts index 345f5182aa..933dff8901 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts @@ -1,5 +1,4 @@ -import { ChangeDetectorRef, Component, OnDestroy, OnInit, TemplateRef, ViewChild } from '@angular/core'; -import { LdnDirectoryService } from '../ldn-services-services/ldn-directory.service'; +import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit, TemplateRef, ViewChild } from '@angular/core'; import { Observable, Subscription } from 'rxjs'; import { RemoteData } from '../../../core/data/remote-data'; import { PaginatedList } from '../../../core/data/paginated-list.model'; @@ -11,170 +10,122 @@ import { LdnServicesService } from 'src/app/admin/admin-ldn-services/ldn-service import { PaginationService } from 'src/app/core/pagination/pagination.service'; import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; import { hasValue } from '../../../shared/empty.util'; -import { HttpClient } from '@angular/common/http'; -import { getFirstCompletedRemoteData } from "../../../core/shared/operators"; +import { Operation } from 'fast-json-patch'; +import { getFirstCompletedRemoteData } from '../../../core/shared/operators'; +import { NotificationsService } from '../../../shared/notifications/notifications.service'; +import { TranslateService } from '@ngx-translate/core'; +import { Router } from '@angular/router'; + @Component({ - selector: 'ds-ldn-services-directory', - templateUrl: './ldn-services-directory.component.html', - styleUrls: ['./ldn-services-directory.component.scss'], + selector: 'ds-ldn-services-directory', + templateUrl: './ldn-services-directory.component.html', + styleUrls: ['./ldn-services-directory.component.scss'], + changeDetection: ChangeDetectionStrategy.Default }) export class LdnServicesOverviewComponent implements OnInit, OnDestroy { - selectedServiceId: number | null = null; - servicesData: any[] = []; - @ViewChild('deleteModal', {static: true}) deleteModal: TemplateRef; - ldnServicesRD$: Observable>>; - config: FindListOptions = Object.assign(new FindListOptions(), { - elementsPerPage: 20 + selectedServiceId: string | number | null = null; + servicesData: any[] = []; + @ViewChild('deleteModal', {static: true}) deleteModal: TemplateRef; + ldnServicesRD$: Observable>>; + config: FindListOptions = Object.assign(new FindListOptions(), { + elementsPerPage: 20 + }); + pageConfig: PaginationComponentOptions = Object.assign(new PaginationComponentOptions(), { + id: 'po', + pageSize: 20 + }); + isProcessingSub: Subscription; + private modalRef: any; + + constructor( + protected ldnServicesService: LdnServicesService, + protected paginationService: PaginationService, + protected modalService: NgbModal, + private cdRef: ChangeDetectorRef, + private notificationService: NotificationsService, + private translateService: TranslateService, + private router: Router, + ) { + } + + ngOnInit(): void { + this.setLdnServices(); + } + + setLdnServices() { + this.ldnServicesRD$ = this.paginationService.getFindListOptions(this.pageConfig.id, this.config).pipe( + switchMap((config) => this.ldnServicesService.findAll(config, true, false).pipe( + getFirstCompletedRemoteData() + )) + ); + + this.ldnServicesRD$.subscribe((rd: RemoteData>) => { + console.log(rd); + if (rd.hasSucceeded) { + this.notificationService.success(this.translateService.get('notification.loaded.success')); + } else { + this.notificationService.error(this.translateService.get('notification.loaded.failure')); + } }); - pageConfig: PaginationComponentOptions = Object.assign(new PaginationComponentOptions(), { - id: 'po', - pageSize: 20 - }); - isProcessingSub: Subscription; - private modalRef: any; + } - - - - constructor( - protected processLdnService: LdnServicesService, - private ldnServicesService: LdnServicesService, - protected paginationService: PaginationService, - protected modalService: NgbModal, - public ldnDirectoryService: LdnDirectoryService, - private http: HttpClient, - private cdRef: ChangeDetectorRef - ) { + ngOnDestroy(): void { + this.paginationService.clearPagination(this.pageConfig.id); + if (hasValue(this.isProcessingSub)) { + this.isProcessingSub.unsubscribe(); } + } - ngOnInit(): void { - this.setLdnServices2(); - this.setLdnServices(); - /*this.ldnDirectoryService.listLdnServices();*/ - /*this.ldnServicesRD$.subscribe(data => { - console.log('searchByLdnUrl()', data); - });*/ + openDeleteModal(content) { + this.modalRef = this.modalService.open(content); + } - /*this.ldnServicesRD$.pipe( - tap(data => { - console.log('ldnServicesRD$ data:', data); - }) - ).subscribe(() => { - this.searchByLdnUrl(); - });*/ + closeModal() { + this.modalRef.close(); + this.cdRef.detectChanges(); + } - } + selectServiceToDelete(serviceId: number) { + this.selectedServiceId = serviceId; + this.openDeleteModal(this.deleteModal); + } - setLdnServices() { - this.ldnServicesService.findAll().pipe(getFirstCompletedRemoteData()).subscribe((remoteData) => { - - if (remoteData.hasSucceeded) { - const ldnservices = remoteData.payload.page; - console.log(ldnservices); - } else { - console.error('Error fetching LDN services:', remoteData.errorMessage); - } - }); - } - - setLdnServices2() { - this.ldnServicesRD$ = this.paginationService.getFindListOptions(this.pageConfig.id, this.config).pipe( - switchMap((config) => this.ldnServicesService.findAll(config, true, false)) - - ); - } - - ngOnDestroy(): void { - this.paginationService.clearPagination(this.pageConfig.id); - if (hasValue(this.isProcessingSub)) { - this.isProcessingSub.unsubscribe(); + deleteSelected(serviceId: string, ldnServicesService: LdnServicesService): void { + if (this.selectedServiceId !== null) { + ldnServicesService.delete(serviceId).pipe(getFirstCompletedRemoteData()).subscribe((rd: RemoteData) => { + if (rd.hasSucceeded) { + this.servicesData = this.servicesData.filter(service => service.id !== serviceId); + this.cdRef.detectChanges(); + this.closeModal(); + this.notificationService.success(this.translateService.get('notification.created.success')); + } else { + this.notificationService.error(this.translateService.get('notification.created.failure')); + this.cdRef.detectChanges(); } + }); } - - openDeleteModal(content) { - this.modalRef = this.modalService.open(content); - } - - closeModal() { - this.modalRef.close(); - this.cdRef.detectChanges(); - } + } - findAllServices(): void { - this.retrieveAll().subscribe( - (response) => { - this.servicesData = response._embedded.ldnservices; - console.log('ServicesData =', this.servicesData); - this.cdRef.detectChanges(); - }, - (error) => { - console.error('Error:', error); - } - ); - } - - retrieveAll(): Observable { - const url = 'http://localhost:8080/server/api/ldn/ldnservices'; - return this.http.get(url); - } - - - - - deleteSelected() { - if (this.selectedServiceId !== null) { - const deleteUrl = `http://localhost:8080/server/api/ldn/ldnservices/${this.selectedServiceId}`; - this.http.delete(deleteUrl).subscribe( - () => { - this.closeModal(); - this.findAllServices(); - }, - (error) => { - console.error('Error deleting service:', error); - } - ); + toggleStatus(ldnService: any, ldnServicesService: LdnServicesService): void { + const newStatus = !ldnService.enabled; + let status = ldnService.enabled; + const patchOperation: Operation = { + op: 'replace', + path: '/enabled', + value: newStatus, + }; + ldnServicesService.patch(ldnService, [patchOperation]).pipe(getFirstCompletedRemoteData()).subscribe( + () => { + if (status !== newStatus) { + this.notificationService.success(this.translateService.get('ldn-enable-service.notification.success.title'), + this.translateService.get('ldn-enable-service.notification.success.content')); + } else { + this.notificationService.error(this.translateService.get('ldn-enable-service.notification.error.title'), + this.translateService.get('ldn-enable-service.notification.error.content')); + } } - } - - selectServiceToDelete(serviceId: number) { - this.selectedServiceId = serviceId; - this.openDeleteModal(this.deleteModal); - } - - toggleStatus(ldnService: any): void { - const newStatus = !ldnService.enabled; - - const apiUrl = `http://localhost:8080/server/api/ldn/ldnservices/${ldnService.id}`; - const patchOperation = { - op: 'replace', - path: '/enabled', - value: newStatus, - }; - - this.http.patch(apiUrl, [patchOperation]).subscribe( - () => { - console.log('Status updated successfully.'); - ldnService.enabled = newStatus; - this.cdRef.detectChanges(); - }, - (error) => { - console.error('Error updating status:', error); - } - ); - } - - fetchServiceData(serviceId: string): void { - const apiUrl = `http://localhost:8080/server/api/ldn/ldnservices/${serviceId}`; - - this.http.get(apiUrl).subscribe( - (data: any) => { - console.log(data); - }, - (error) => { - console.error('Error fetching service data:', error); - } - ); - } + ); + } } diff --git a/src/app/admin/admin-ldn-services/ldn-services-guard/ldn-services-guard.service.ts b/src/app/admin/admin-ldn-services/ldn-services-guard/ldn-services-guard.service.ts index 85235b4370..21123ede4a 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-guard/ldn-services-guard.service.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-guard/ldn-services-guard.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router'; import { Observable } from 'rxjs'; -import { map } from 'rxjs/operators'; + @Injectable({ providedIn: 'root' diff --git a/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service-constraint.model.ts b/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service-constraint.model.ts deleted file mode 100644 index 704a3e7d8c..0000000000 --- a/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service-constraint.model.ts +++ /dev/null @@ -1,26 +0,0 @@ - -/** - * A cosntrain that can be used when running a service - */ -export class LdnServiceConstraint { - /** - * The name of the constrain - */ - name: string; - - /** - * The value of the constrain - */ - value: string; -} - -export const EndorsmentConstrain = [ - { - name: 'Type 1 Item', - value: 'Type1' - }, - { - name: 'Type 2 Item', - value: 'Type2' - }, -]; diff --git a/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service-patterns.model.ts b/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service-patterns.model.ts new file mode 100644 index 0000000000..1e67ccfeee --- /dev/null +++ b/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service-patterns.model.ts @@ -0,0 +1,13 @@ +import { autoserialize } from 'cerialize'; + +/** + * notify service patterns + */ +export class NotifyServicePattern { + @autoserialize + pattern: string; + @autoserialize + constraint: string; + @autoserialize + automatic: string; +} diff --git a/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service.constrain.model.ts b/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service.constrain.model.ts new file mode 100644 index 0000000000..6eda194443 --- /dev/null +++ b/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service.constrain.model.ts @@ -0,0 +1,3 @@ +export class ldnServiceConstrain { + void: any; +} diff --git a/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service.resource-type.ts b/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service.resource-type.ts index e5684e8c34..859ecb739e 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service.resource-type.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service.resource-type.ts @@ -7,3 +7,4 @@ import { ResourceType } from '../../../core/shared/resource-type'; export const LDN_SERVICE = new ResourceType('ldnservice'); +export const LDN_SERVICE_CONSTRAINT = new ResourceType('ldnservice'); diff --git a/src/app/admin/admin-ldn-services/ldn-services-model/ldn-services.model.ts b/src/app/admin/admin-ldn-services/ldn-services-model/ldn-services.model.ts index ec28193a93..82731b3cb1 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-model/ldn-services.model.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-model/ldn-services.model.ts @@ -1,12 +1,16 @@ import { ResourceType } from '../../../core/shared/resource-type'; import { CacheableObject } from '../../../core/cache/cacheable-object.model'; -import { autoserialize, deserialize } from 'cerialize'; +import { autoserialize, deserialize, deserializeAs, inheritSerialization } from 'cerialize'; import { LDN_SERVICE } from './ldn-service.resource-type'; import { excludeFromEquals } from '../../../core/utilities/equals.decorators'; import { typedObject } from '../../../core/cache/builders/build-decorators'; +import { NotifyServicePattern } from './ldn-service-patterns.model'; + + @typedObject +@inheritSerialization(CacheableObject) export class LdnService extends CacheableObject { static type = LDN_SERVICE; @@ -15,7 +19,10 @@ export class LdnService extends CacheableObject { type: ResourceType; @autoserialize - id?: number; + id: number; + + @deserializeAs('id') + uuid: string; @autoserialize name: string; @@ -49,13 +56,3 @@ export class LdnService extends CacheableObject { return this._links.self.href; } } - - -class NotifyServicePattern { - @autoserialize - pattern: string; - @autoserialize - constraint?: string; - @autoserialize - automatic?: boolean; -} diff --git a/src/app/admin/admin-routing-paths.ts b/src/app/admin/admin-routing-paths.ts index df0459ff53..511680bfd8 100644 --- a/src/app/admin/admin-routing-paths.ts +++ b/src/app/admin/admin-routing-paths.ts @@ -4,6 +4,8 @@ import { getAdminModuleRoute } from '../app-routing-paths'; export const REGISTRIES_MODULE_PATH = 'registries'; export const NOTIFICATIONS_MODULE_PATH = 'notifications'; +export const LDN_PATH = 'ldn'; + export function getRegistriesModuleRoute() { return new URLCombiner(getAdminModuleRoute(), REGISTRIES_MODULE_PATH).toString(); } @@ -12,4 +14,8 @@ export function getNotificationsModuleRoute() { return new URLCombiner(getAdminModuleRoute(), NOTIFICATIONS_MODULE_PATH).toString(); } -export const LDN_PATH = 'ldn'; +export function getLdnServicesModuleRoute() { + return new URLCombiner(getAdminModuleRoute(), LDN_PATH).toString(); +} + +