diff --git a/projects/rero/ng-core/src/lib/pipe/get-record.pipe.spec.ts b/projects/rero/ng-core/src/lib/pipe/get-record.pipe.spec.ts new file mode 100644 index 00000000..75851608 --- /dev/null +++ b/projects/rero/ng-core/src/lib/pipe/get-record.pipe.spec.ts @@ -0,0 +1,53 @@ +/* + * Invenio angular core + * Copyright (C) 2019 RERO + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import { GetRecordPipe } from './get-record.pipe'; +import { Observable, of } from 'rxjs'; +import { RecordService } from '../record/record.service'; + +class RecordServiceMock { + getRecord(type: string, pid: string, resolve = 0): Observable { + return of({ metadata: { pid, name: 'foo' }}); + } +} + +describe('GetRecordPipe', () => { + it('create an instance', () => { + const pipe = new GetRecordPipe(new RecordServiceMock() as RecordService); + expect(pipe).toBeTruthy(); + }); + + it('transform with $ref return object', () => { + const pipe = new GetRecordPipe(new RecordServiceMock() as RecordService); + pipe.transform('http://foo/1', 'resource').subscribe((result: object) => { + expect(result).toEqual({metadata: { pid: '1', name: 'foo' }}); + }); + }); + + it('transform with id return name', () => { + const pipe = new GetRecordPipe(new RecordServiceMock() as RecordService); + pipe.transform('10', 'resource', 'field', 'name').subscribe((result: string) => { + expect(result).toEqual('foo'); + }); + }); + + it('transform return null', () => { + const pipe = new GetRecordPipe(new RecordServiceMock() as RecordService); + pipe.transform('12', 'resource', 'field', 'foo').subscribe((result: string) => { + expect(result).toBeNull(); + }); + }); +}); diff --git a/projects/rero/ng-core/src/lib/pipe/get-record.pipe.ts b/projects/rero/ng-core/src/lib/pipe/get-record.pipe.ts new file mode 100644 index 00000000..0d2c5fc8 --- /dev/null +++ b/projects/rero/ng-core/src/lib/pipe/get-record.pipe.ts @@ -0,0 +1,52 @@ +/* + * Invenio angular core + * Copyright (C) 2019 RERO + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import { Pipe, PipeTransform } from '@angular/core'; +import { RecordService } from '../record/record.service'; +import { map } from 'rxjs/operators'; +import { extractIdOnRef } from '../utils/utils'; + +@Pipe({ + name: 'getRecord' +}) +export class GetRecordPipe implements PipeTransform { + + constructor(private recordService: RecordService) {} + + transform(pid: any, type: string, returnType = 'object', field?: string): any { + // process $ref entrypoint + if (pid.startsWith('http')) { + pid = extractIdOnRef(pid); + } + + if ('object' !== returnType) { + returnType = 'field'; + } + + return this.recordService.getRecord(type, pid, 1).pipe(map(data => { + if (!data) { + return null; + } + if ('object' === returnType) { + return data; + } + if (field in data.metadata) { + return data.metadata[field]; + } + return null; + })); + } +} diff --git a/projects/rero/ng-core/src/lib/shared.module.ts b/projects/rero/ng-core/src/lib/shared.module.ts index c815c14d..d9e05c79 100644 --- a/projects/rero/ng-core/src/lib/shared.module.ts +++ b/projects/rero/ng-core/src/lib/shared.module.ts @@ -36,6 +36,7 @@ import { MenuComponent } from './widget/menu/menu.component'; import { RouterModule } from '@angular/router'; import { CallbackArrayFilterPipe } from './pipe/callback-array-filter.pipe'; import { DateTranslatePipe } from './translate/date-translate-pipe'; +import { GetRecordPipe } from './pipe/get-record.pipe'; @NgModule({ @@ -49,7 +50,8 @@ import { DateTranslatePipe } from './translate/date-translate-pipe'; UpperCaseFirstPipe, MenuComponent, CallbackArrayFilterPipe, - DateTranslatePipe + DateTranslatePipe, + GetRecordPipe ], imports: [ CommonModule, @@ -83,7 +85,8 @@ import { DateTranslatePipe } from './translate/date-translate-pipe'; TranslateLanguagePipe, UpperCaseFirstPipe, MenuComponent, - DateTranslatePipe + DateTranslatePipe, + GetRecordPipe ], entryComponents: [ DialogComponent