Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pipe: create a new getRecord pipe #50

Merged
merged 1 commit into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions projects/rero/ng-core/src/lib/pipe/get-record.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<any> {
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();
});
});
});
52 changes: 52 additions & 0 deletions projects/rero/ng-core/src/lib/pipe/get-record.pipe.ts
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}));
}
}
7 changes: 5 additions & 2 deletions projects/rero/ng-core/src/lib/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -49,7 +50,8 @@ import { DateTranslatePipe } from './translate/date-translate-pipe';
UpperCaseFirstPipe,
MenuComponent,
CallbackArrayFilterPipe,
DateTranslatePipe
DateTranslatePipe,
GetRecordPipe
],
imports: [
CommonModule,
Expand Down Expand Up @@ -83,7 +85,8 @@ import { DateTranslatePipe } from './translate/date-translate-pipe';
TranslateLanguagePipe,
UpperCaseFirstPipe,
MenuComponent,
DateTranslatePipe
DateTranslatePipe,
GetRecordPipe
],
entryComponents: [
DialogComponent
Expand Down