advancing well the new helpers

This commit is contained in:
Mathias Beaulieu-Duncan
2019-09-13 11:53:25 -05:00
parent c89cc92d33
commit 3de4294ef5
4 changed files with 75 additions and 38 deletions
+1 -1
View File
@@ -4,7 +4,7 @@
"peerDependencies": {
"@angular/common": "^8.2.4",
"@angular/core": "^8.2.4",
"@poweredsoft/data": "^0.0.14",
"@poweredsoft/data": "^0.0.25",
"rxjs": "^6.5.3"
}
}
@@ -1,8 +1,10 @@
import { Injectable } from "@angular/core";
import { HttpClient } from '@angular/common/http';
import { IDataSourceTransportOptions, IDataSourceCommandAdapterOptions } from '@poweredsoft/data';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { IDataSourceTransportOptions, IDataSourceCommandAdapterOptions, IDataSourceOptions, IResolveCommandModelEvent } from '@poweredsoft/data';
import { IQueryExecutionResult, IQueryExecutionGroupResult, IQueryCriteria } from '@poweredsoft/data';
import { IDataSourceQueryAdapterOptions } from '@poweredsoft/data';
import { catchError} from 'rxjs/operators';
import { throwError, Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
@@ -14,6 +16,34 @@ export class GenericRestDataSourceService
}
private _handleErrorPipe(err: HttpErrorResponse) : Observable<any> {
// is it a validation error
// is it something I can display nicely with a message
// unexpected message...
return throwError({
});
}
createDataSourceOptions<TModel, TKey>(route: string, keyResolver: (model: TModel) => TKey, defaultCriteria: IQueryCriteria, manageNotificationMessage: boolean = true) : IDataSourceOptions<TModel>
{
const dataSourceTransportOptions = this.createStandardRestTransportOptions<TModel, TKey>(route, keyResolver);
const dataSourceOptions: IDataSourceOptions<TModel> = {
defaultCriteria: defaultCriteria,
resolveIdField: keyResolver,
manageNotificationMessage: manageNotificationMessage,
transport: dataSourceTransportOptions
};
return dataSourceOptions;
}
setResolveCommand<TModel>(options: IDataSourceOptions<TModel>, name: string, resolveCommandModel: (event: IResolveCommandModelEvent<TModel>) => Observable<any>) {
options.transport.commands[name].resolveCommandModel = resolveCommandModel;
}
createStandardRestTransportOptions<TModel, TKey>(route: string, keyResolver: (model: TModel) => TKey) : IDataSourceTransportOptions<TModel> {
const query: IDataSourceQueryAdapterOptions<TModel> = {
@@ -28,7 +58,7 @@ export class GenericRestDataSourceService
const createCommand: IDataSourceCommandAdapterOptions<TModel> = {
adapter: {
handle: (command: TModel) => {
return this.http.post<TModel>(route, command);
return this.http.post<TModel>(route, command).pipe(catchError(this._handleErrorPipe));
}
}
};
@@ -38,7 +68,7 @@ export class GenericRestDataSourceService
handle: (command: TModel) => {
const key = keyResolver(command);
const updateRoute = `${route}/${encodeURIComponent(key as any)}`;
return this.http.put<TModel>(updateRoute, command);
return this.http.put<TModel>(updateRoute, command).pipe(catchError(this._handleErrorPipe));
}
}
};
@@ -48,7 +78,7 @@ export class GenericRestDataSourceService
handle: (command: TModel) => {
const key = keyResolver(command);
const updateRoute = `${route}/${encodeURIComponent(key as any)}`;
return this.http.delete<TModel>(updateRoute);
return this.http.delete<TModel>(updateRoute).pipe(catchError(this._handleErrorPipe));
}
}
};