mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 18:14:17 +00:00
Addition of SearchService that mocks an array SearchResult objects
This commit is contained in:
75
src/app/search/search.service.ts
Normal file
75
src/app/search/search.service.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { RemoteData } from '../core/data/remote-data';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { SearchResult } from './searchresult.model';
|
||||
import { ItemDataService } from '../core/data/item-data.service';
|
||||
import { PageInfo } from '../core/shared/page-info.model';
|
||||
import { DSpaceObject } from '../core/shared/dspace-object.model';
|
||||
|
||||
@Injectable()
|
||||
export class SearchService {
|
||||
|
||||
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',
|
||||
);
|
||||
|
||||
constructor(private itemDataService: ItemDataService) {
|
||||
}
|
||||
|
||||
search(query: string, scopeId: string): RemoteData<SearchResult<DSpaceObject>[]> {
|
||||
const self = `https://dspace7.4science.it/dspace-spring-rest/api/search?query=${query}&scope=${scopeId}`;
|
||||
const requestPending = Observable.of(false);
|
||||
const responsePending = Observable.of(false);
|
||||
const isSuccessFul = Observable.of(true);
|
||||
const errorMessage = Observable.of(undefined);
|
||||
const statusCode = Observable.of('200');
|
||||
const pageInfo = Observable.of(new PageInfo());
|
||||
|
||||
let mockSearchResults: SearchResult<DSpaceObject>[] = [];
|
||||
let dsoObsArr = [];
|
||||
this.idsToMock = this.idsToMock
|
||||
|
||||
this.idsToMock.forEach(id => {
|
||||
let remoteObject: RemoteData<DSpaceObject> = this.itemDataService.findById(id);
|
||||
|
||||
let dsoObs = remoteObject.payload.take(1);
|
||||
dsoObsArr.push(dsoObs);
|
||||
dsoObs.subscribe((dso: DSpaceObject) => {
|
||||
let mockResult: SearchResult<DSpaceObject> = new SearchResult();
|
||||
mockResult.result = dso;
|
||||
// Just return the first metadatum as a "highlight"
|
||||
mockResult.hitHiglights = dso.metadata.slice(0, 1);
|
||||
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(
|
||||
self,
|
||||
requestPending,
|
||||
responsePending,
|
||||
isSuccessFul,
|
||||
errorMessage,
|
||||
statusCode,
|
||||
pageInfo,
|
||||
payload
|
||||
)
|
||||
}
|
||||
}
|
15
src/app/search/searchresult.model.ts
Normal file
15
src/app/search/searchresult.model.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { DSpaceObject } from '../core/shared/dspace-object.model';
|
||||
import { Metadatum } from '../core/shared/metadatum.model';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
|
||||
@NgModule({
|
||||
|
||||
})
|
||||
|
||||
export class SearchResult<T extends DSpaceObject>{
|
||||
|
||||
result: T;
|
||||
hitHiglights : Metadatum[];
|
||||
|
||||
}
|
Reference in New Issue
Block a user