import { Injectable } from '@angular/core' ; import { ApiCallService , ApiService , httpObservePostOptions } from '../services/api-call.service' ; import { ValidatorService } from '../services/validator.service' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { HttpParams } from '@angular/common/http' ; import { NumberSearchResults , SearchResultList } from './models/search-result' ; export const COOCKIENAME = 'onlyActiveObjCookie' ; @ Injectable ( { providedIn : 'root' } ) export class SearchService < T = SearchResultList > implements ApiService { public servicePrefix : string = 'search' ; public constructor ( private api ) { } public getEstimateValueResults ( regex ) { const httpOptions = httpObservePostOptions ; let params = new HttpParams ( ) ; params = params . set ( 'searchValue' , ValidatorService . validateRegex ( regex ) . trim ( ) ) ; httpOptions . params = params ; return this . api . callGet < NumberSearchResults > ( this . servicePrefix + '/quick/count/' , httpOptions ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } public getSearch ( query , params ? ) { const httpOptions = httpObservePostOptions ; params = params . set ( 'onlyActiveObjCookie' , this . api . readCookies ( COOCKIENAME ) ) ; if ( params ) { httpOptions . params = params ; } return this . api . callGet < T > ( `${ this . servicePrefix }/` , httpOptions ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . statusCode === 204 ) { return [ ] ; } return apiResponse . body ; } ) ) ; } public postSearch ( query , params ? ) { const httpOptions = httpObservePostOptions ; params = params . set ( 'onlyActiveObjCookie' , this . api . readCookies ( COOCKIENAME ) ) ; if ( params ) { httpOptions . params = params ; } return this . api . callPost < T > ( `${ this . servicePrefix }/` , query , httpOptions ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . statusCode === 204 ) { return [ ] ; } return apiResponse . body ; } ) ) ; } }