1 import { Injectable } from '@angular/core'
2 import { Observable } from 'rxjs/Observable'
3 import 'rxjs/add/operator/catch'
4 import 'rxjs/add/operator/map'
5 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
7 import { Search } from '../../shared'
8 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 updateVideo (video: VideoEdit) {
45 const language = video.language ? video.language : null
47 const body: VideoUpdate = {
49 category: video.category,
50 licence: video.licence,
52 description: video.description,
53 privacy: video.privacy,
58 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, body)
59 .map(this.restExtractor.extractDataBool)
60 .catch(this.restExtractor.handleError)
63 uploadVideo (video: FormData) {
64 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
68 .catch(this.restExtractor.handleError)
71 getMyVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
72 const pagination = this.videoPaginationToRestPagination(videoPagination)
74 let params = new HttpParams()
75 params = this.restService.addRestGetParams(params, pagination, sort)
77 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
78 .map(this.extractVideos)
79 .catch((res) => this.restExtractor.handleError(res))
82 getVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
83 const pagination = this.videoPaginationToRestPagination(videoPagination)
85 let params = new HttpParams()
86 params = this.restService.addRestGetParams(params, pagination, sort)
89 .get(VideoService.BASE_VIDEO_URL, { params })
90 .map(this.extractVideos)
91 .catch((res) => this.restExtractor.handleError(res))
94 searchVideos (search: Search, videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
95 const url = VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value)
97 const pagination = this.videoPaginationToRestPagination(videoPagination)
99 let params = new HttpParams()
100 params = this.restService.addRestGetParams(params, pagination, sort)
102 if (search.field) params.set('field', search.field)
105 .get<ResultList<VideoServerModel>>(url, { params })
106 .map(this.extractVideos)
107 .catch((res) => this.restExtractor.handleError(res))
110 removeVideo (id: number) {
112 .delete(VideoService.BASE_VIDEO_URL + id)
113 .map(this.restExtractor.extractDataBool)
114 .catch((res) => this.restExtractor.handleError(res))
117 loadCompleteDescription (descriptionPath: string) {
119 .get(API_URL + descriptionPath)
120 .map(res => res['description'])
121 .catch((res) => this.restExtractor.handleError(res))
124 setVideoLike (id: number) {
125 return this.setVideoRate(id, 'like')
128 setVideoDislike (id: number) {
129 return this.setVideoRate(id, 'dislike')
132 getUserVideoRating (id: number): Observable<UserVideoRate> {
133 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
137 .catch(res => this.restExtractor.handleError(res))
140 private videoPaginationToRestPagination (videoPagination: VideoPagination) {
141 const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage
142 const count: number = videoPagination.itemsPerPage
144 return { start, count }
147 private setVideoRate (id: number, rateType: VideoRateType) {
148 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
149 const body: UserVideoRateUpdate = {
155 .map(this.restExtractor.extractDataBool)
156 .catch(res => this.restExtractor.handleError(res))
159 private extractVideos (result: ResultList<VideoServerModel>) {
160 const videosJson = result.data
161 const totalVideos = result.total
164 for (const videoJson of videosJson) {
165 videos.push(new Video(videoJson))
168 return { videos, totalVideos }