Merge pull request #4582 from FinnStutzenstein/fixUnregisteredCollections

Fixed handling of unregistered resources
This commit is contained in:
Emanuel Schütze 2019-04-09 16:38:30 +02:00 committed by GitHub
commit f54360290f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 10 deletions

View File

@ -112,7 +112,11 @@ export class AutoupdateService {
// Add the objects to the DataStore. // Add the objects to the DataStore.
for (const collection of Object.keys(autoupdate.changed)) { for (const collection of Object.keys(autoupdate.changed)) {
await this.DS.add(this.mapObjectsToBaseModels(collection, autoupdate.changed[collection])); if (this.modelMapper.isCollectionRegistered(collection)) {
await this.DS.add(this.mapObjectsToBaseModels(collection, autoupdate.changed[collection]));
} else {
console.error(`Unregistered collection "${collection}". Ignore it.`);
}
} }
await this.DS.flushToStorage(autoupdate.to_change_id); await this.DS.flushToStorage(autoupdate.to_change_id);
@ -130,9 +134,6 @@ export class AutoupdateService {
*/ */
private mapObjectsToBaseModels(collection: string, models: object[]): BaseModel[] { private mapObjectsToBaseModels(collection: string, models: object[]): BaseModel[] {
const targetClass = this.modelMapper.getModelConstructor(collection); const targetClass = this.modelMapper.getModelConstructor(collection);
if (!targetClass) {
throw new Error(`Unregistered resource ${collection}`);
}
return models.map(model => new targetClass(model)); return models.map(model => new targetClass(model));
} }

View File

@ -65,27 +65,39 @@ export class CollectionStringMapperService {
} }
} }
public isCollectionRegistered(collectionString: string): boolean {
return !!this.collectionStringMapping[collectionString];
}
/** /**
* @param obj The object to get the model constructor from. * @param obj The object to get the model constructor from.
* @returns the model constructor * @returns the model constructor
*/ */
public getModelConstructor<M extends BaseModel>(obj: TypeIdentifier): ModelConstructor<M> { public getModelConstructor<M extends BaseModel>(obj: TypeIdentifier): ModelConstructor<M> | null {
return this.collectionStringMapping[this.getCollectionString(obj)][0] as ModelConstructor<M>; if (this.isCollectionRegistered(this.getCollectionString(obj))) {
return this.collectionStringMapping[this.getCollectionString(obj)][0] as ModelConstructor<M>;
}
} }
/** /**
* @param obj The object to get the view model constructor from. * @param obj The object to get the view model constructor from.
* @returns the view model constructor * @returns the view model constructor
*/ */
public getViewModelConstructor<M extends BaseViewModel>(obj: TypeIdentifier): ViewModelConstructor<M> { public getViewModelConstructor<M extends BaseViewModel>(obj: TypeIdentifier): ViewModelConstructor<M> | null {
return this.collectionStringMapping[this.getCollectionString(obj)][1] as ViewModelConstructor<M>; if (this.isCollectionRegistered(this.getCollectionString(obj))) {
return this.collectionStringMapping[this.getCollectionString(obj)][1] as ViewModelConstructor<M>;
}
} }
/** /**
* @param obj The object to get the repository from. * @param obj The object to get the repository from.
* @returns the repository * @returns the repository
*/ */
public getRepository<V extends BaseViewModel, M extends BaseModel>(obj: TypeIdentifier): BaseRepository<V, M> { public getRepository<V extends BaseViewModel, M extends BaseModel>(
return this.collectionStringMapping[this.getCollectionString(obj)][2] as BaseRepository<V, M>; obj: TypeIdentifier
): BaseRepository<V, M> | null {
if (this.isCollectionRegistered(this.getCollectionString(obj))) {
return this.collectionStringMapping[this.getCollectionString(obj)][2] as BaseRepository<V, M>;
}
} }
} }