Separate module for searchservice + different retrieval of mock items

This commit is contained in:
Jonas Van Goolen
2017-08-10 14:21:01 +02:00
parent 09920419de
commit 88a6f8220f
3 changed files with 52 additions and 61 deletions

View File

@@ -1,15 +1,9 @@
import { NgModule } from '@angular/core';
import { DSpaceObject } from '../core/shared/dspace-object.model'; import { DSpaceObject } from '../core/shared/dspace-object.model';
import { Metadatum } from '../core/shared/metadatum.model'; import { Metadatum } from '../core/shared/metadatum.model';
import { Observable } from 'rxjs/Observable';
@NgModule({
})
export class SearchResult<T extends DSpaceObject>{ export class SearchResult<T extends DSpaceObject>{
result: T; dspaceObject: T;
hitHiglights : Metadatum[]; hitHighlights: Metadatum[];
} }

View File

@@ -0,0 +1,17 @@
import { NgModule } from '@angular/core';
import { CoreModule } from '../core/core.module';
import { SearchService } from './search.service';
@NgModule({
imports: [
CoreModule
],
declarations: [
],
exports: [
],
providers: [
SearchService
]
})
export class SearchModule {}

View File

@@ -1,31 +1,19 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { RemoteData } from '../core/data/remote-data'; import { RemoteData } from '../core/data/remote-data';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import { SearchResult } from './searchresult.model'; import { SearchResult } from './search-result.model';
import { ItemDataService } from '../core/data/item-data.service'; import { ItemDataService } from '../core/data/item-data.service';
import { PageInfo } from '../core/shared/page-info.model'; import { PageInfo } from '../core/shared/page-info.model';
import { DSpaceObject } from '../core/shared/dspace-object.model'; import { DSpaceObject } from '../core/shared/dspace-object.model';
import { SearchOptions } from './search.models'; import { SearchOptions } from './search.models';
import { hasValue } from '../shared/empty.util'; import { hasValue, isNotEmpty } from '../shared/empty.util';
import { Metadatum } from '../core/shared/metadatum.model'; import { Metadatum } from '../core/shared/metadatum.model';
import { Item } from '../core/shared/item.model';
@Injectable() @Injectable()
export class SearchService { export class SearchService {
totalPages : number = 5; totalPages = 5;
idsToMock: string[] = new Array(
'ed5d5f21-1ce4-4b06-b7c2-a7272835ade0',
'0ec7ff22-f211-40ab-a69e-c819b0b1f357',
'9f3288b2-f2ad-454f-9f4c-70325646dcee',
'fdf0175b-6b0c-421f-9266-28b04341b940',
'dbe26193-2fa0-4d6c-9f52-edd3572b65a0',
'6212604b-c778-42b3-88e4-cc3b1262ad28',
'a352a28f-fd5f-4c7b-81bb-06a28b4ea780',
'55a24a8a-1a2f-4fee-b5e8-ca07826d9ff3',
'75664e4e-0000-48e5-b2b6-fbe51ad05f92',
'848e4058-d7b2-482a-b481-e681e7c4016b',
);
mockedHighlights: string[] = new Array( mockedHighlights: string[] = new Array(
'This is a <em>sample abstract</em>.', 'This is a <em>sample abstract</em>.',
'This is a sample abstract. But, to fill up some space, here\'s <em>"Hello"</em> in several different languages : ', 'This is a sample abstract. But, to fill up some space, here\'s <em>"Hello"</em> in several different languages : ',
@@ -39,60 +27,52 @@ export class SearchService {
'<em>The QSAR DataBank (QsarDB) repository</em>', '<em>The QSAR DataBank (QsarDB) repository</em>',
); );
constructor(private itemDataService: ItemDataService) { constructor(private itemDataService: ItemDataService) {
} }
search(query: string, scopeId: string, searchOptions: SearchOptions): RemoteData<SearchResult<DSpaceObject>[]> { search(query: string, scopeId?: string, searchOptions?: SearchOptions): RemoteData<Array<SearchResult<DSpaceObject>>> {
let self = `https://dspace7.4science.it/dspace-spring-rest/api/search?query=${query}&scope=${scopeId}`; let self = `https://dspace7.4science.it/dspace-spring-rest/api/search?query=${query}`;
if(hasValue(searchOptions.currentPage)){ if (hasValue(scopeId)) {
self+=`&currentPage=${searchOptions.currentPage}`; self += `&scope=${scopeId}`;
}
if (isNotEmpty(searchOptions) && hasValue(searchOptions.currentPage)) {
self += `&page=${searchOptions.currentPage}`;
} }
const requestPending = Observable.of(false); const requestPending = Observable.of(false);
const responsePending = Observable.of(false); const responsePending = Observable.of(false);
const isSuccessFul = Observable.of(true); const isSuccessFul = Observable.of(true);
const errorMessage = Observable.of(undefined); const errorMessage = Observable.of(undefined);
const statusCode = Observable.of('200'); const statusCode = Observable.of('200');
let returningPageInfo = new PageInfo(); const returningPageInfo = new PageInfo();
returningPageInfo.elementsPerPage = searchOptions.elementsPerPage; if (isNotEmpty(searchOptions)) {
returningPageInfo.currentPage = searchOptions.currentPage; returningPageInfo.elementsPerPage = searchOptions.elementsPerPage;
returningPageInfo.currentPage = searchOptions.currentPage;
} else {
returningPageInfo.elementsPerPage = 10;
returningPageInfo.currentPage = 1;
}
returningPageInfo.totalPages = this.totalPages; returningPageInfo.totalPages = this.totalPages;
returningPageInfo.totalElements= this.idsToMock.length*this.totalPages; returningPageInfo.totalElements = 10 * this.totalPages;
const pageInfo = Observable.of(returningPageInfo); const pageInfo = Observable.of(returningPageInfo);
let mockSearchResults: SearchResult<DSpaceObject>[] = []; const itemsRD = this.itemDataService.findAll({
let dsoObsArr = []; scopeID: '8e0928a0-047a-4369-8883-12669f32dd64',
this.idsToMock = this.idsToMock currentPage: returningPageInfo.currentPage,
elementsPerPage: returningPageInfo.elementsPerPage
this.idsToMock.forEach((id,index) => { });
let remoteObject: RemoteData<DSpaceObject> = this.itemDataService.findById(id); const payload = itemsRD.payload.map((items: Item[]) => {
return items.map((item: Item, index: number) => {
let dsoObs = remoteObject.payload.take(1); const mockResult: SearchResult<DSpaceObject> = new SearchResult();
dsoObsArr.push(dsoObs); mockResult.dspaceObject = item;
dsoObs.subscribe((dso: DSpaceObject) => { const highlight = new Metadatum();
let mockResult: SearchResult<DSpaceObject> = new SearchResult();
mockResult.result = dso;
// Use a mocked highlight based on the index of the loop
let highlight = new Metadatum();
highlight.key = 'dc.description.abstract'; highlight.key = 'dc.description.abstract';
highlight.value = this.mockedHighlights[index]; highlight.value = this.mockedHighlights[index % this.mockedHighlights.length];
mockResult.hitHiglights = new Array(highlight); mockResult.hitHighlights = new Array(highlight);
return mockResult;
mockSearchResults.push(mockResult);
}); });
}); });
// combineLatest ->Merges observables. When this is done, put the "mockSearchResults" as a payload
const payload = Observable.combineLatest(...dsoObsArr
, () => {
// Shuffle the searchresult to mimick a changed in the query
let randomization: number[] = new Array(-1, 0, 1);
let number = randomization[ Math.floor(Math.random() * randomization.length) ];
return mockSearchResults.sort(() => number);
});
return new RemoteData( return new RemoteData(
self, self,
requestPending, requestPending,