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,
61 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, body)
62 .map(this.restExtractor.extractDataBool)
63 .catch(this.restExtractor.handleError)
66 uploadVideo (video: FormData) {
67 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
71 .catch(this.restExtractor.handleError)
74 getMyVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
75 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
77 let params = new HttpParams()
78 params = this.restService.addRestGetParams(params, pagination, sort)
80 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
81 .map(this.extractVideos)
82 .catch((res) => this.restExtractor.handleError(res))
85 getVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
86 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
88 let params = new HttpParams()
89 params = this.restService.addRestGetParams(params, pagination, sort)
92 .get(VideoService.BASE_VIDEO_URL, { params })
93 .map(this.extractVideos)
94 .catch((res) => this.restExtractor.handleError(res))
99 videoPagination: ComponentPagination,
101 ): Observable<{ videos: Video[], totalVideos: number}> {
102 const url = VideoService.BASE_VIDEO_URL + 'search'
104 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
106 let params = new HttpParams()
107 params = this.restService.addRestGetParams(params, pagination, sort)
108 params = params.append('search', search)
111 .get<ResultList<VideoServerModel>>(url, { params })
112 .map(this.extractVideos)
113 .catch((res) => this.restExtractor.handleError(res))
116 removeVideo (id: number) {
118 .delete(VideoService.BASE_VIDEO_URL + id)
119 .map(this.restExtractor.extractDataBool)
120 .catch((res) => this.restExtractor.handleError(res))
123 loadCompleteDescription (descriptionPath: string) {
125 .get(environment.apiUrl + descriptionPath)
126 .map(res => res['description'])
127 .catch((res) => this.restExtractor.handleError(res))
130 setVideoLike (id: number) {
131 return this.setVideoRate(id, 'like')
134 setVideoDislike (id: number) {
135 return this.setVideoRate(id, 'dislike')
138 getUserVideoRating (id: number): Observable<UserVideoRate> {
139 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
143 .catch(res => this.restExtractor.handleError(res))
146 private setVideoRate (id: number, rateType: VideoRateType) {
147 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
148 const body: UserVideoRateUpdate = {
154 .map(this.restExtractor.extractDataBool)
155 .catch(res => this.restExtractor.handleError(res))
158 private extractVideos (result: ResultList<VideoServerModel>) {
159 const videosJson = result.data
160 const totalVideos = result.total
163 for (const videoJson of videosJson) {
164 videos.push(new Video(videoJson))
167 return { videos, totalVideos }