Fixed handling of unregistered resources

This commit is contained in:
FinnStutzenstein 2019-04-09 16:15:23 +02:00
parent fc5f6f4e54
commit dfaf8b552a
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)) {
if (this.modelMapper.isCollectionRegistered(collection)) {
await this.DS.add(this.mapObjectsToBaseModels(collection, autoupdate.changed[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 {
if (this.isCollectionRegistered(this.getCollectionString(obj))) {
return this.collectionStringMapping[this.getCollectionString(obj)][0] as ModelConstructor<M>; 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 {
if (this.isCollectionRegistered(this.getCollectionString(obj))) {
return this.collectionStringMapping[this.getCollectionString(obj)][1] as ViewModelConstructor<M>; 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>(
obj: TypeIdentifier
): BaseRepository<V, M> | null {
if (this.isCollectionRegistered(this.getCollectionString(obj))) {
return this.collectionStringMapping[this.getCollectionString(obj)][2] as BaseRepository<V, M>; return this.collectionStringMapping[this.getCollectionString(obj)][2] as BaseRepository<V, M>;
} }
} }
}