aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/search/search.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/search/search.service.ts')
-rw-r--r--client/src/app/search/search.service.ts46
1 files changed, 46 insertions, 0 deletions
diff --git a/client/src/app/search/search.service.ts b/client/src/app/search/search.service.ts
new file mode 100644
index 000000000..02d5f5915
--- /dev/null
+++ b/client/src/app/search/search.service.ts
@@ -0,0 +1,46 @@
1import { catchError, switchMap } from 'rxjs/operators'
2import { HttpClient, HttpParams } from '@angular/common/http'
3import { Injectable } from '@angular/core'
4import { Observable } from 'rxjs'
5import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
6import { VideoService } from '@app/shared/video/video.service'
7import { RestExtractor, RestService } from '@app/shared'
8import { environment } from 'environments/environment'
9import { ResultList, Video } from '../../../../shared'
10import { Video as VideoServerModel } from '@app/shared/video/video.model'
11
12export type SearchResult = {
13 videosResult: { totalVideos: number, videos: Video[] }
14}
15
16@Injectable()
17export 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}