1 import { catchError, map, switchMap } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { Observable } from 'rxjs'
5 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
6 import { VideoService } from '@app/shared/video/video.service'
7 import { RestExtractor, RestService } from '@app/shared'
8 import { environment } from '../../environments/environment'
9 import { ResultList, Video as VideoServerModel, VideoChannel as VideoChannelServerModel } from '../../../../shared'
10 import { Video } from '@app/shared/video/video.model'
11 import { AdvancedSearch } from '@app/search/advanced-search.model'
12 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
13 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
16 export class SearchService {
17 static BASE_SEARCH_URL = environment.apiUrl + '/api/v1/search/'
20 private authHttp: HttpClient,
21 private restExtractor: RestExtractor,
22 private restService: RestService,
23 private videoService: VideoService
26 searchVideos (parameters: {
28 componentPagination: ComponentPagination,
29 advancedSearch: AdvancedSearch
30 }): Observable<ResultList<Video>> {
31 const { search, componentPagination, advancedSearch } = parameters
33 const url = SearchService.BASE_SEARCH_URL + 'videos'
34 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
36 let params = new HttpParams()
37 params = this.restService.addRestGetParams(params, pagination)
39 if (search) params = params.append('search', search)
41 const advancedSearchObject = advancedSearch.toAPIObject()
42 params = this.restService.addObjectParams(params, advancedSearchObject)
45 .get<ResultList<VideoServerModel>>(url, { params })
47 switchMap(res => this.videoService.extractVideos(res)),
48 catchError(err => this.restExtractor.handleError(err))
52 searchVideoChannels (parameters: {
54 componentPagination: ComponentPagination
55 }): Observable<ResultList<VideoChannel>> {
56 const { search, componentPagination } = parameters
58 const url = SearchService.BASE_SEARCH_URL + 'video-channels'
59 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
61 let params = new HttpParams()
62 params = this.restService.addRestGetParams(params, pagination)
63 params = params.append('search', search)
66 .get<ResultList<VideoChannelServerModel>>(url, { params })
68 map(res => VideoChannelService.extractVideoChannels(res)),
69 catchError(err => this.restExtractor.handleError(err))