1 import { Injectable } from '@angular/core'
2 import { Observable } from 'rxjs/Observable'
3 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
4 import 'rxjs/add/operator/catch'
5 import 'rxjs/add/operator/map'
7 import { SortField } from './sort-field.type'
14 import { Video } from './video.model'
15 import { VideoDetails } from './video-details.model'
16 import { VideoEdit } from './video-edit.model'
17 import { VideoPagination } from './video-pagination.model'
23 Video as VideoServerModel,
24 VideoDetails as VideoDetailsServerModel,
26 } from '../../../../../shared'
29 export class VideoService {
30 private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
33 private authHttp: HttpClient,
34 private restExtractor: RestExtractor,
35 private restService: RestService
38 getVideo (uuid: string): Observable<VideoDetails> {
39 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
40 .map(videoHash => new VideoDetails(videoHash))
41 .catch((res) => this.restExtractor.handleError(res))
44 viewVideo (uuid: string): Observable<VideoDetails> {
45 return this.authHttp.post(VideoService.BASE_VIDEO_URL + uuid + '/views', {})
46 .map(this.restExtractor.extractDataBool)
47 .catch(this.restExtractor.handleError)
50 updateVideo (video: VideoEdit) {
51 const language = video.language ? video.language : null
53 const body: VideoUpdate = {
55 category: video.category,
56 licence: video.licence,
58 description: video.description,
59 privacy: video.privacy,
64 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, body)
65 .map(this.restExtractor.extractDataBool)
66 .catch(this.restExtractor.handleError)
69 uploadVideo (video: FormData) {
70 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
74 .catch(this.restExtractor.handleError)
77 getMyVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
78 const pagination = this.videoPaginationToRestPagination(videoPagination)
80 let params = new HttpParams()
81 params = this.restService.addRestGetParams(params, pagination, sort)
83 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
84 .map(this.extractVideos)
85 .catch((res) => this.restExtractor.handleError(res))
88 getVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
89 const pagination = this.videoPaginationToRestPagination(videoPagination)
91 let params = new HttpParams()
92 params = this.restService.addRestGetParams(params, pagination, sort)
95 .get(VideoService.BASE_VIDEO_URL, { params })
96 .map(this.extractVideos)
97 .catch((res) => this.restExtractor.handleError(res))
100 searchVideos (search: Search, videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
101 const url = VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value)
103 const pagination = this.videoPaginationToRestPagination(videoPagination)
105 let params = new HttpParams()
106 params = this.restService.addRestGetParams(params, pagination, sort)
108 if (search.field) params.set('field', search.field)
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(API_URL + 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 videoPaginationToRestPagination (videoPagination: VideoPagination) {
147 const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage
148 const count: number = videoPagination.itemsPerPage
150 return { start, count }
153 private setVideoRate (id: number, rateType: VideoRateType) {
154 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
155 const body: UserVideoRateUpdate = {
161 .map(this.restExtractor.extractDataBool)
162 .catch(res => this.restExtractor.handleError(res))
165 private extractVideos (result: ResultList<VideoServerModel>) {
166 const videosJson = result.data
167 const totalVideos = result.total
170 for (const videoJson of videosJson) {
171 videos.push(new Video(videoJson))
174 return { videos, totalVideos }