зеркало из https://github.com/microsoft/paris.git
Improved entity removal
This commit is contained in:
Родитель
6d65a00bd4
Коммит
ca0bb70b9d
|
@ -3,6 +3,7 @@ import {ParisConfig} from "../config/paris-config";
|
|||
import {DataEntityConstructor} from "./data-entity.base";
|
||||
import {DataQuery} from "../dataset/data-query";
|
||||
import {RequestMethod} from "../services/http.service";
|
||||
import {ModelBase} from "../models/model.base";
|
||||
|
||||
export class ModelEntity extends EntityConfigBase implements EntityConfig{
|
||||
endpoint:EntityConfigFunctionOrValue;
|
||||
|
@ -15,7 +16,9 @@ export class ModelEntity extends EntityConfigBase implements EntityConfig{
|
|||
parseDataQuery?:(dataQuery:DataQuery) => { [index:string]:any };
|
||||
parseItemQuery?:(itemId:string|number, entity?:IEntityConfigBase, config?:ParisConfig, params?:{ [index:string]:any }) => string;
|
||||
parseSaveQuery?:(item:any, entity?:IEntityConfigBase, config?:ParisConfig) => string;
|
||||
parseRemoveQuery?:(items:Array<ModelBase>, entity?:IEntityConfigBase, config?:ParisConfig) => string;
|
||||
serializeItem?:(item:any, serializedItem?:any, entity?:IEntityConfigBase, config?:ParisConfig) => any;
|
||||
getRemoveData?:(items:Array<ModelBase>) => any;
|
||||
|
||||
constructor(config:EntityConfig, entityConstructor:DataEntityConstructor<any>){
|
||||
super(config, entityConstructor);
|
||||
|
@ -38,9 +41,11 @@ export interface EntityBackendConfig{
|
|||
allItemsEndpoint?:string,
|
||||
allItemsEndpointTrailingSlash?:boolean,
|
||||
fixedData?: { [index:string]:any },
|
||||
getRemoveData?:(items:Array<ModelBase>) => any,
|
||||
parseDataQuery?:(dataQuery:DataQuery) => { [index:string]:any },
|
||||
parseItemQuery?:(itemId:string|number, entity?:IEntityConfigBase, config?:ParisConfig, params?:{ [index:string]:any }) => string,
|
||||
parseSaveQuery?:(item:any, entity?:IEntityConfigBase, config?:ParisConfig) => string,
|
||||
parseRemoveQuery?:(items:Array<ModelBase>, entity?:IEntityConfigBase, config?:ParisConfig) => string,
|
||||
serializeItem?:(item:any, serializedItem?:any, entity?:IEntityConfigBase, config?:ParisConfig) => any,
|
||||
separateArrayParams?:boolean,
|
||||
saveMethod?:((item:any, config?:ParisConfig) => RequestMethod) | RequestMethod
|
||||
|
|
|
@ -127,6 +127,45 @@ export class Repository<T extends ModelBase> extends ReadonlyRepository<T> imple
|
|||
});
|
||||
}
|
||||
|
||||
removeItem(item:T, options?:HttpOptions):Observable<T>{
|
||||
if (!item)
|
||||
return Observable.of(null);
|
||||
|
||||
if (!this.entityBackendConfig.endpoint)
|
||||
throw new Error(`Entity ${this.entity.singularName} can't be deleted - it doesn't specify an endpoint.`);
|
||||
|
||||
try {
|
||||
let httpOptions:HttpOptions = options || { data: {}};
|
||||
if (!httpOptions.data)
|
||||
httpOptions.data = {};
|
||||
|
||||
if (this.entityBackendConfig.getRemoveData)
|
||||
Object.assign(httpOptions.data, this.entityBackendConfig.getRemoveData([item]));
|
||||
|
||||
let endpoint:string = this.entityBackendConfig.parseRemoveQuery ? this.entityBackendConfig.parseRemoveQuery([item], this.entity, this.config) : `${this.endpointName}/${item.id || ''}`;
|
||||
|
||||
return this.dataStore.delete(endpoint, httpOptions, this.baseUrl)
|
||||
.catch((err: AjaxError) => {
|
||||
this.emitEntityHttpErrorEvent(err);
|
||||
throw err;
|
||||
})
|
||||
.do(() => {
|
||||
if (this._allValues) {
|
||||
let itemIndex:number = _.findIndex(this._allValues, (_item:T) => _item.id === item.id);
|
||||
if (~itemIndex)
|
||||
this._allValues.splice(itemIndex, 1);
|
||||
|
||||
this._allItemsSubject$.next(this._allValues);
|
||||
}
|
||||
|
||||
this._removeSubject$.next({ entity: this.entityConstructor, items: [item] });
|
||||
}).map(() => item);
|
||||
}
|
||||
catch(e){
|
||||
return Observable.throw(e);
|
||||
}
|
||||
}
|
||||
|
||||
remove(items:Array<T>, options?:HttpOptions):Observable<Array<T>>{
|
||||
if (!items)
|
||||
throw new Error(`No ${this.entity.pluralName.toLowerCase()} specified for removing.`);
|
||||
|
@ -145,9 +184,14 @@ export class Repository<T extends ModelBase> extends ReadonlyRepository<T> imple
|
|||
if (!httpOptions.data)
|
||||
httpOptions.data = {};
|
||||
|
||||
httpOptions.data.ids = items.map(item => item.id);
|
||||
Object.assign(httpOptions.data, this.entityBackendConfig.getRemoveData
|
||||
? this.entityBackendConfig.getRemoveData(items)
|
||||
: { ids: items.map(item => item.id) }
|
||||
);
|
||||
|
||||
return this.dataStore.delete(this.endpointName, httpOptions, this.baseUrl)
|
||||
let endpoint:string = this.entityBackendConfig.parseRemoveQuery ? this.entityBackendConfig.parseRemoveQuery(items, this.entity, this.config) : this.endpointName;
|
||||
|
||||
return this.dataStore.delete(endpoint, httpOptions, this.baseUrl)
|
||||
.catch((err: AjaxError) => {
|
||||
this.emitEntityHttpErrorEvent(err);
|
||||
throw err;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "wcdportal.paris",
|
||||
"version": "0.6.7",
|
||||
"version": "0.6.8",
|
||||
"description": "Library for the implementation of Domain Driven Design in Angular/TypeScript apps",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
|
Загрузка…
Ссылка в новой задаче