diff --git a/projects/poweredsoft/data/package.json b/projects/poweredsoft/data/package.json index c832b77..0b3815a 100644 --- a/projects/poweredsoft/data/package.json +++ b/projects/poweredsoft/data/package.json @@ -1,6 +1,6 @@ { "name": "@poweredsoft/data", - "version": "0.0.23", + "version": "0.0.24", "peerDependencies": { "rxjs": "^6.5.3" } diff --git a/src/app/app.component.ts b/src/app/app.component.ts index e4dbfaf..23c53d1 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,108 +1,130 @@ import { Component, OnInit, Pipe } from '@angular/core'; import { IDataSource } from 'projects/poweredsoft/data/src/lib/IDataSource'; import { IDataSourceNotifyMessage } from 'projects/poweredsoft/data/src/lib/IDataSourceNotifyMessage'; -import { DataSource, IDataSourceTransportOptions, IDataSourceQueryAdapterOptions, IQueryCriteria, IQueryExecutionResult, IQueryExecutionGroupResult, IDataSourceCommandAdapterOptions } from 'projects/poweredsoft/data/src/public-api'; +import { DataSource, IDataSourceTransportOptions, IDataSourceQueryAdapterOptions, IQueryCriteria, IQueryExecutionResult, IQueryExecutionGroupResult, IDataSourceCommandAdapterOptions, IDataSourceValidationError } from 'projects/poweredsoft/data/src/public-api'; import { HttpClient } from '@angular/common/http'; -import { of } from 'rxjs'; +import { of, throwError } from 'rxjs'; export interface MyModel { - id: number; - name: string; + id: number; + name: string; } @Component({ - selector: 'app-root', - templateUrl: './app.component.html', - styleUrls: ['./app.component.css'] + selector: 'app-root', + templateUrl: './app.component.html', + styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { - title = 'data'; - message: string = ''; - type: string = ''; + title = 'data'; + message: string = ''; + type: string = ''; - dataSource: IDataSource; - createCommand: IDataSourceCommandAdapterOptions; - - public constructor(private http: HttpClient) { + dataSource: IDataSource; + createCommand: IDataSourceCommandAdapterOptions; - } + public constructor(private http: HttpClient) { - ngOnInit(): void { - const keyResolver = (m: MyModel) => m.id; - let route = 'http://localhost:9999'; + } - const query: IDataSourceQueryAdapterOptions = { - adapter: { - handle: (criteria: IQueryCriteria) => { - const queryRoute = `${route}/read`; - return this.http.post & IQueryExecutionGroupResult>(queryRoute, criteria); - } - } - }; + ngOnInit(): void { + const keyResolver = (m: MyModel) => m.id; + let route = 'http://localhost:9999'; - this.createCommand = { - adapter: { - handle: (command: MyModel) => { - return of(command); - //return this.http.post(route, command); - } - } - }; + const query: IDataSourceQueryAdapterOptions = { + adapter: { + handle: (criteria: IQueryCriteria) => { + const queryRoute = `${route}/read`; + return this.http.post & IQueryExecutionGroupResult>(queryRoute, criteria); + } + } + }; - const updateCommand: IDataSourceCommandAdapterOptions = { - adapter: { - handle: (command: MyModel) => { - const key = keyResolver(command); - const updateRoute = `${route}/${encodeURIComponent(key as any)}`; - return of(command); - } - } - }; + this.createCommand = { + adapter: { + handle: (command: MyModel) => { + return of(command); + //return this.http.post(route, command); + } + } + }; - const deleteCommand: IDataSourceCommandAdapterOptions = { - adapter: { - handle: (command: MyModel) => { - const key = keyResolver(command); - const updateRoute = `${route}/${encodeURIComponent(key as any)}`; - return of(command); - } - } - }; + const updateCommand: IDataSourceCommandAdapterOptions = { + adapter: { + handle: (command: MyModel) => { + const key = keyResolver(command); + const updateRoute = `${route}/${encodeURIComponent(key as any)}`; + return throwError({ + type: 'validation', + errors: { + 'name': [ + 'David is not unique' + ] + } + }); + } + } + }; - const transportOptions : IDataSourceTransportOptions = { - query: query, - commands: { - 'create': this.createCommand, - 'update': updateCommand, - 'delete': deleteCommand - } - }; - this.dataSource = new DataSource({ - manageNotificationMessage: false, - transport: transportOptions, - defaultCriteria: { - page: 1, - pageSize: 5, - groups: [ - { path: 'name' } - ] - } - }); + const deleteCommand: IDataSourceCommandAdapterOptions = { + adapter: { + handle: (command: MyModel) => { + const key = keyResolver(command); + const updateRoute = `${route}/${encodeURIComponent(key as any)}`; + return of(command); + } + } + }; - this.dataSource.notifyMessage$.subscribe((notifyMessage: IDataSourceNotifyMessage) => { - this.handleNotification(notifyMessage); - }); - } + const transportOptions: IDataSourceTransportOptions = { + query: query, + commands: { + 'create': this.createCommand, + 'update': updateCommand, + 'delete': deleteCommand + } + }; + this.dataSource = new DataSource({ + manageNotificationMessage: false, + transport: transportOptions, + defaultCriteria: { + page: 1, + pageSize: 5, + groups: [ + { path: 'name' } + ] + } + }); - handleNotification(notification: IDataSourceNotifyMessage) { - this.message = notification.message; - } + this.dataSource.notifyMessage$.subscribe((notifyMessage) => { + this.handleNotification(notifyMessage); + }); - onDoSomething() { - this.dataSource.executeCommandByName('create', this.createCommand).subscribe(() => { - console.log('hey we did it!'); - }); - //this.dataSource.refresh(); - } + this.dataSource.validationError$.subscribe((t) => { + + let message = ''; + for (var key in t.errors) + t.errors[key].forEach(error => message += `\n${error}`); + + this.handleNotification({ + type: 'error', + message: message + }); + }); + } + + handleNotification(notification: IDataSourceNotifyMessage) { + this.message = notification.message; + } + + onDoSomething() { + this.dataSource.executeCommandByName('create', {}).subscribe(() => { + console.log('hey we did it!'); + }); + this.dataSource.executeCommandByName('update', {}).subscribe(() => { + + }); + //this.dataSource.refresh(); + } }