1 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
2 import { Injectable } from '@angular/core'
3 import 'rxjs/add/operator/catch'
4 import 'rxjs/add/operator/map'
5 import { Observable } from 'rxjs/Observable'
6 import { Video as VideoServerModel, VideoDetails as VideoDetailsServerModel } from '../../../../../shared'
7 import { ResultList } from '../../../../../shared/models/result-list.model'
8 import { UserVideoRateUpdate } from '../../../../../shared/models/videos/user-video-rate-update.model'
9 import { UserVideoRate } from '../../../../../shared/models/videos/user-video-rate.model'
10 import { VideoRateType } from '../../../../../shared/models/videos/video-rate.type'
11 import { VideoUpdate } from '../../../../../shared/models/videos/video-update.model'
12 import { environment } from '../../../environments/environment'
13 import { ComponentPagination } from '../rest/component-pagination.model'
14 import { RestExtractor } from '../rest/rest-extractor.service'
15 import { RestService } from '../rest/rest.service'
16 import { UserService } from '../users/user.service'
17 import { SortField } from './sort-field.type'
18 import { VideoDetails } from './video-details.model'
19 import { VideoEdit } from './video-edit.model'
20 import { Video } from './video.model'
23 export class VideoService {
24 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
27 private authHttp: HttpClient,
28 private restExtractor: RestExtractor,
29 private restService: RestService
32 getVideo (uuid: string): Observable<VideoDetails> {
33 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
34 .map(videoHash => new VideoDetails(videoHash))
35 .catch((res) => this.restExtractor.handleError(res))
38 viewVideo (uuid: string): Observable<VideoDetails> {
39 return this.authHttp.post(VideoService.BASE_VIDEO_URL + uuid + '/views', {})
40 .map(this.restExtractor.extractDataBool)
41 .catch(this.restExtractor.handleError)
44 updateVideo (video: VideoEdit) {
45 const language = video.language || undefined
46 const licence = video.licence || undefined
47 const category = video.category || undefined
48 const description = video.description || undefined
50 const body: VideoUpdate = {
56 privacy: video.privacy,
59 commentsEnabled: video.commentsEnabled
62 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, body)
63 .map(this.restExtractor.extractDataBool)
64 .catch(this.restExtractor.handleError)
67 uploadVideo (video: FormData) {
68 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
72 .catch(this.restExtractor.handleError)
75 getMyVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
76 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
78 let params = new HttpParams()
79 params = this.restService.addRestGetParams(params, pagination, sort)
81 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
82 .map(this.extractVideos)
83 .catch((res) => this.restExtractor.handleError(res))
86 getVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
87 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
89 let params = new HttpParams()
90 params = this.restService.addRestGetParams(params, pagination, sort)
93 .get(VideoService.BASE_VIDEO_URL, { params })
94 .map(this.extractVideos)
95 .catch((res) => this.restExtractor.handleError(res))
100 videoPagination: ComponentPagination,
102 ): Observable<{ videos: Video[], totalVideos: number}> {
103 const url = VideoService.BASE_VIDEO_URL + 'search'
105 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
107 let params = new HttpParams()
108 params = this.restService.addRestGetParams(params, pagination, sort)
109 params = params.append('search', search)
112 .get<ResultList<VideoServerModel>>(url, { params })
113 .map(this.extractVideos)
114 .catch((res) => this.restExtractor.handleError(res))
117 removeVideo (id: number) {
119 .delete(VideoService.BASE_VIDEO_URL + id)
120 .map(this.restExtractor.extractDataBool)
121 .catch((res) => this.restExtractor.handleError(res))
124 loadCompleteDescription (descriptionPath: string) {
126 .get(environment.apiUrl + descriptionPath)
127 .map(res => res['description'])
128 .catch((res) => this.restExtractor.handleError(res))
131 setVideoLike (id: number) {
132 return this.setVideoRate(id, 'like')
135 setVideoDislike (id: number) {
136 return this.setVideoRate(id, 'dislike')
139 unsetVideoLike (id: number) {
140 return this.setVideoRate(id, 'none')
143 getUserVideoRating (id: number): Observable<UserVideoRate> {
144 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
148 .catch(res => this.restExtractor.handleError(res))
151 private setVideoRate (id: number, rateType: VideoRateType) {
152 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
153 const body: UserVideoRateUpdate = {
159 .map(this.restExtractor.extractDataBool)
160 .catch(res => this.restExtractor.handleError(res))
163 private extractVideos (result: ResultList<VideoServerModel>) {
164 const videosJson = result.data
165 const totalVideos = result.total
168 for (const videoJson of videosJson) {
169 videos.push(new Video(videoJson))
172 return { videos, totalVideos }