import {Injectable, Inject} from '@angular/core'; import {URLSearchParams, Response} from '@angular/http'; import {Observable} from 'rxjs/Observable'; import {SEARCH_CONFIG, ISearchConfig} from '../config/config.module'; import {ISearchFilter} from './search-filter'; import {AuthHttp} from 'angular2-jwt'; import _ from 'lodash'; import { OwsRecordTransformer } from '../records/transformers/ows-record-transformer.service'; import {RecordCollection} from '../records/record-collection'; @Injectable() export class OwsSearchService { constructor( private http: AuthHttp, @Inject(SEARCH_CONFIG) private config: ISearchConfig) {} get( docType: string, searchFilter: ISearchFilter) : Observable { return this.http.get( this.buildUrl(docType), {search: this.urlParams(searchFilter)} ) .map(response => this.parseResponse(response, docType)) .cache(); } private urlParams(searchFilter: ISearchFilter): URLSearchParams { const urlParams = new URLSearchParams(); _.forOwn( searchFilter, (val, key) => urlParams.set(_.kebabCase(key), val)); return urlParams; } private buildUrl(docType: string): string { return this.config.searchUrl.replace(/:docType/, docType); } private parseResponse( res: Response, docType: string) : RecordCollection { const transformer = new OwsRecordTransformer(); return transformer.transform( res.json(), { label: _.startCase(docType), name: docType } ); } }