]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/search/search.service.ts
Add ability to search video channels
[github/Chocobozzz/PeerTube.git] / client / src / app / search / search.service.ts
index c6106afd659f2c57fc7295eb8c7b8bd449b1d755..cd3bdad356443812c0e5429192c16bc80d39d58a 100644 (file)
@@ -1,4 +1,4 @@
-import { catchError, switchMap } from 'rxjs/operators'
+import { catchError, map, switchMap } from 'rxjs/operators'
 import { HttpClient, HttpParams } from '@angular/common/http'
 import { Injectable } from '@angular/core'
 import { Observable } from 'rxjs'
@@ -6,13 +6,11 @@ import { ComponentPagination } from '@app/shared/rest/component-pagination.model
 import { VideoService } from '@app/shared/video/video.service'
 import { RestExtractor, RestService } from '@app/shared'
 import { environment } from 'environments/environment'
-import { ResultList, Video } from '../../../../shared'
-import { Video as VideoServerModel } from '@app/shared/video/video.model'
+import { ResultList, Video as VideoServerModel, VideoChannel as VideoChannelServerModel } from '../../../../shared'
+import { Video } from '@app/shared/video/video.model'
 import { AdvancedSearch } from '@app/search/advanced-search.model'
-
-export type SearchResult = {
-  videosResult: { totalVideos: number, videos: Video[] }
-}
+import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
+import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
 
 @Injectable()
 export class SearchService {
@@ -36,20 +34,11 @@ export class SearchService {
 
     let params = new HttpParams()
     params = this.restService.addRestGetParams(params, pagination)
-    params = params.append('search', search)
-
-    const advancedSearchObject = advancedSearch.toAPIObject()
 
-    for (const name of Object.keys(advancedSearchObject)) {
-      const value = advancedSearchObject[name]
-      if (!value) continue
+    if (search) params = params.append('search', search)
 
-      if (Array.isArray(value)) {
-        for (const v of value) params = params.append(name, v)
-      } else {
-        params = params.append(name, value)
-      }
-    }
+    const advancedSearchObject = advancedSearch.toAPIObject()
+    params = this.restService.addObjectParams(params, advancedSearchObject)
 
     return this.authHttp
                .get<ResultList<VideoServerModel>>(url, { params })
@@ -58,4 +47,24 @@ export class SearchService {
                  catchError(err => this.restExtractor.handleError(err))
                )
   }
+
+  searchVideoChannels (
+    search: string,
+    componentPagination: ComponentPagination
+  ): Observable<{ data: VideoChannel[], total: number }> {
+    const url = SearchService.BASE_SEARCH_URL + 'video-channels'
+
+    const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
+
+    let params = new HttpParams()
+    params = this.restService.addRestGetParams(params, pagination)
+    params = params.append('search', search)
+
+    return this.authHttp
+               .get<ResultList<VideoChannelServerModel>>(url, { params })
+               .pipe(
+                 map(res => VideoChannelService.extractVideoChannels(res)),
+                 catchError(err => this.restExtractor.handleError(err))
+               )
+  }
 }