]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/search/search.service.ts
Fix responsive on videos search
[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)
4278710d
C
39
40 if (search) params = params.append('search', search)
57c36b27 41
0b18f4aa
C
42 const advancedSearchObject = advancedSearch.toAPIObject()
43
44 for (const name of Object.keys(advancedSearchObject)) {
45 const value = advancedSearchObject[name]
46 if (!value) continue
47
7afea880 48 if (Array.isArray(value) && value.length !== 0) {
0b18f4aa
C
49 for (const v of value) params = params.append(name, v)
50 } else {
51 params = params.append(name, value)
52 }
53 }
54
57c36b27
C
55 return this.authHttp
56 .get<ResultList<VideoServerModel>>(url, { params })
57 .pipe(
58 switchMap(res => this.videoService.extractVideos(res)),
59 catchError(err => this.restExtractor.handleError(err))
60 )
61 }
62}