]>
Commit | Line | Data |
---|---|---|
57c36b27 C |
1 | import { catchError, 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 } from '../../../../shared' | |
10 | import { Video as VideoServerModel } from '@app/shared/video/video.model' | |
0b18f4aa | 11 | import { AdvancedSearch } from '@app/search/advanced-search.model' |
57c36b27 C |
12 | |
13 | export type SearchResult = { | |
14 | videosResult: { totalVideos: number, videos: Video[] } | |
15 | } | |
16 | ||
17 | @Injectable() | |
18 | export class SearchService { | |
19 | static BASE_SEARCH_URL = environment.apiUrl + '/api/v1/search/' | |
20 | ||
21 | constructor ( | |
22 | private authHttp: HttpClient, | |
23 | private restExtractor: RestExtractor, | |
24 | private restService: RestService, | |
25 | private videoService: VideoService | |
26 | ) {} | |
27 | ||
28 | searchVideos ( | |
29 | search: string, | |
0b18f4aa C |
30 | componentPagination: ComponentPagination, |
31 | advancedSearch: AdvancedSearch | |
57c36b27 C |
32 | ): Observable<{ videos: Video[], totalVideos: number }> { |
33 | const url = SearchService.BASE_SEARCH_URL + 'videos' | |
34 | ||
35 | const pagination = this.restService.componentPaginationToRestPagination(componentPagination) | |
36 | ||
37 | let params = new HttpParams() | |
38 | params = this.restService.addRestGetParams(params, pagination) | |
39 | params = params.append('search', search) | |
40 | ||
0b18f4aa C |
41 | const advancedSearchObject = advancedSearch.toAPIObject() |
42 | ||
43 | for (const name of Object.keys(advancedSearchObject)) { | |
44 | const value = advancedSearchObject[name] | |
45 | if (!value) continue | |
46 | ||
47 | if (Array.isArray(value)) { | |
48 | for (const v of value) params = params.append(name, v) | |
49 | } else { | |
50 | params = params.append(name, value) | |
51 | } | |
52 | } | |
53 | ||
57c36b27 C |
54 | return this.authHttp |
55 | .get<ResultList<VideoServerModel>>(url, { params }) | |
56 | .pipe( | |
57 | switchMap(res => this.videoService.extractVideos(res)), | |
58 | catchError(err => this.restExtractor.handleError(err)) | |
59 | ) | |
60 | } | |
61 | } |