]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/search/search.service.ts
Add advanced search in client
[github/Chocobozzz/PeerTube.git] / client / src / app / search / search.service.ts
CommitLineData
57c36b27
C
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'
0b18f4aa 11import { AdvancedSearch } from '@app/search/advanced-search.model'
57c36b27
C
12
13export type SearchResult = {
14 videosResult: { totalVideos: number, videos: Video[] }
15}
16
17@Injectable()
18export 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}