]>
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' | |
11 | ||
12 | export type SearchResult = { | |
13 | videosResult: { totalVideos: number, videos: Video[] } | |
14 | } | |
15 | ||
16 | @Injectable() | |
17 | export class SearchService { | |
18 | static BASE_SEARCH_URL = environment.apiUrl + '/api/v1/search/' | |
19 | ||
20 | constructor ( | |
21 | private authHttp: HttpClient, | |
22 | private restExtractor: RestExtractor, | |
23 | private restService: RestService, | |
24 | private videoService: VideoService | |
25 | ) {} | |
26 | ||
27 | searchVideos ( | |
28 | search: string, | |
29 | componentPagination: ComponentPagination | |
30 | ): Observable<{ videos: Video[], totalVideos: number }> { | |
31 | const url = SearchService.BASE_SEARCH_URL + 'videos' | |
32 | ||
33 | const pagination = this.restService.componentPaginationToRestPagination(componentPagination) | |
34 | ||
35 | let params = new HttpParams() | |
36 | params = this.restService.addRestGetParams(params, pagination) | |
37 | params = params.append('search', search) | |
38 | ||
39 | return this.authHttp | |
40 | .get<ResultList<VideoServerModel>>(url, { params }) | |
41 | .pipe( | |
42 | switchMap(res => this.videoService.extractVideos(res)), | |
43 | catchError(err => this.restExtractor.handleError(err)) | |
44 | ) | |
45 | } | |
46 | } |