aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-search/search.service.ts
blob: ad2de0f37f54dcb5741121a2d46dccddbe4e2d8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { Observable, of } from 'rxjs'
import { catchError, map, switchMap } from 'rxjs/operators'
import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { ComponentPaginationLight, RestExtractor, RestPagination, RestService } from '@app/core'
import { Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main'
import {
  ResultList,
  Video as VideoServerModel,
  VideoChannel as VideoChannelServerModel,
  VideoPlaylist as VideoPlaylistServerModel
} from '@shared/models'
import { environment } from '../../../environments/environment'
import { VideoPlaylist, VideoPlaylistService } from '../shared-video-playlist'
import { AdvancedSearch } from './advanced-search.model'

@Injectable()
export class SearchService {
  static BASE_SEARCH_URL = environment.apiUrl + '/api/v1/search/'

  constructor (
    private authHttp: HttpClient,
    private restExtractor: RestExtractor,
    private restService: RestService,
    private videoService: VideoService,
    private playlistService: VideoPlaylistService
  ) { }

  searchVideos (parameters: {
    search?: string
    componentPagination?: ComponentPaginationLight
    advancedSearch?: AdvancedSearch
    uuids?: string[]
  }): Observable<ResultList<Video>> {
    const { search, uuids, componentPagination, advancedSearch } = parameters

    if (advancedSearch?.resultType !== undefined && advancedSearch.resultType !== 'videos') {
      return of({ total: 0, data: [] })
    }

    const url = SearchService.BASE_SEARCH_URL + 'videos'
    let pagination: RestPagination

    if (componentPagination) {
      pagination = this.restService.componentToRestPagination(componentPagination)
    }

    let params = new HttpParams()
    params = this.restService.addRestGetParams(params, pagination)

    if (search) params = params.append('search', search)
    if (uuids) params = this.restService.addArrayParams(params, 'uuids', uuids)

    if (advancedSearch) {
      const advancedSearchObject = advancedSearch.toVideosAPIObject()
      params = this.restService.addObjectParams(params, advancedSearchObject)
    }

    return this.authHttp
               .get<ResultList<VideoServerModel>>(url, { params })
               .pipe(
                 switchMap(res => this.videoService.extractVideos(res)),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  searchVideoChannels (parameters: {
    search?: string
    advancedSearch?: AdvancedSearch
    componentPagination?: ComponentPaginationLight
    handles?: string[]
  }): Observable<ResultList<VideoChannel>> {
    const { search, advancedSearch, componentPagination, handles } = parameters

    if (advancedSearch?.resultType !== undefined && advancedSearch.resultType !== 'channels') {
      return of({ total: 0, data: [] })
    }

    const url = SearchService.BASE_SEARCH_URL + 'video-channels'

    let pagination: RestPagination
    if (componentPagination) {
      pagination = this.restService.componentToRestPagination(componentPagination)
    }

    let params = new HttpParams()
    params = this.restService.addRestGetParams(params, pagination)

    if (search) params = params.append('search', search)
    if (handles) params = this.restService.addArrayParams(params, 'handles', handles)

    if (advancedSearch) {
      const advancedSearchObject = advancedSearch.toChannelAPIObject()
      params = this.restService.addObjectParams(params, advancedSearchObject)
    }

    return this.authHttp
               .get<ResultList<VideoChannelServerModel>>(url, { params })
               .pipe(
                 map(res => VideoChannelService.extractVideoChannels(res)),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  searchVideoPlaylists (parameters: {
    search?: string
    advancedSearch?: AdvancedSearch
    componentPagination?: ComponentPaginationLight
    uuids?: string[]
  }): Observable<ResultList<VideoPlaylist>> {
    const { search, advancedSearch, componentPagination, uuids } = parameters

    if (advancedSearch?.resultType !== undefined && advancedSearch.resultType !== 'playlists') {
      return of({ total: 0, data: [] })
    }

    const url = SearchService.BASE_SEARCH_URL + 'video-playlists'

    let pagination: RestPagination
    if (componentPagination) {
      pagination = this.restService.componentToRestPagination(componentPagination)
    }

    let params = new HttpParams()
    params = this.restService.addRestGetParams(params, pagination)

    if (search) params = params.append('search', search)
    if (uuids) params = this.restService.addArrayParams(params, 'uuids', uuids)

    if (advancedSearch) {
      const advancedSearchObject = advancedSearch.toPlaylistAPIObject()
      params = this.restService.addObjectParams(params, advancedSearchObject)
    }

    return this.authHttp
               .get<ResultList<VideoPlaylistServerModel>>(url, { params })
               .pipe(
                 switchMap(res => this.playlistService.extractPlaylists(res)),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }
}