Skip to content

Commit

Permalink
fix: fixed getvaluesforsubject
Browse files Browse the repository at this point in the history
when no values exist for subject
  • Loading branch information
lem-onade committed May 28, 2020
1 parent ec4ead8 commit 81dd0d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,13 @@ export class DGTMockDataService extends DGTDataService {
}

public getEntities<S extends DGTEntity>(entityType: string, query: DGTQuery): Observable<S[]> {
let res: Observable<S[]> = null;

this.logger.debug(DGTMockDataService.name, 'Getting multiple entities', { entityType, query });
const entities = query ?
this.queries.execute<S[]>(this.database.get(entityType) as S[], query) :
this.database.get(entityType);

if (query) {
res = of(this.database.get(entityType))
.pipe(
tap(entities => this.logger.debug(DGTMockDataService.name, 'Loaded entities', { entities })),
map(entities => this.queries.execute<S[]>(entities as S[], query)),
tap(entities => this.logger.debug(DGTMockDataService.name, 'Queried entities', { entities })),
map(entities => _.map(entities, data => {
const entity: S = this.convertTimestamp(data) as S;

return entity;
})
));
} else {
res = of(this.database.get(entityType))
.pipe(
map(entities => _.map(entities, data => {
const entity: S = this.convertTimestamp(data) as S;

return entity;
})
));
}

return res;
return entities ? of(entities.map(entity => this.convertTimestamp(entity) as S)) : of([]);
}

public getEntity<S extends DGTEntity>(entityType: string, entityId: string): Observable<S> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Observable, forkJoin, of, concat, zip, merge } from 'rxjs';
import { DGTSubject } from '../models/dgt-subject.model';
import { switchMap, map, tap, concatAll, filter, mergeMap } from 'rxjs/operators';
import { switchMap, map, tap, concatAll, filter, mergeMap, flatMap } from 'rxjs/operators';
import { DGTExchange } from '../models/dgt-subject-exchange.model';
import { DGTLDTriple } from '../../linked-data/models/dgt-ld-triple.model';
import * as _ from 'lodash';
Expand All @@ -26,13 +26,19 @@ export class DGTSubjectService {
*/
public getValuesForSubject(subject: DGTSubject): Observable<DGTLDTriple[]> {
this.logger.debug(DGTSubjectService.name, 'Getting subject values', { subject });

return this.data.getEntities<DGTExchange>('exchange', { conditions: [{ field: 'subject', operator: '==', value: subject.id }] })
.pipe(
filter(exchanges => exchanges && exchanges.length > 0),
tap(exchanges => this.logger.debug(DGTSubjectService.name, 'Retrieved exchanges for subject', {exchanges, subject})),
mergeMap(exchanges => forkJoin(exchanges.map(exc => this.getValuesForExchange(exc)))),
map(val => _.flatten(val))
mergeMap(exchanges => {
if (exchanges.length) {
return of(exchanges).pipe(
mergeMap(xchngs => forkJoin(xchngs.map(xchng => this.getValuesForExchange(xchng)))),
tap(val => this.logger.debug(DGTSubjectService.name, 'Retrieved values for exchanges', {val})),
map(val => _.flatten(val))
);
} else {
return of([]);
}
}),
);
}

Expand Down

0 comments on commit 81dd0d5

Please sign in to comment.