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 { VideoPagination } from './video-pagination.model'
22 Video as VideoServerModel,
24 } from '../../../../../shared'
27 export class VideoService {
28 private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
31 private authHttp: HttpClient,
32 private restExtractor: RestExtractor,
33 private restService: RestService
36 getVideo (uuid: string) {
37 return this.authHttp.get<VideoServerModel>(VideoService.BASE_VIDEO_URL + uuid)
38 .map(videoHash => new Video(videoHash))
39 .catch((res) => this.restExtractor.handleError(res))
42 updateVideo (video: Video) {
43 const language = video.language ? video.language : null
45 const body: VideoUpdate = {
47 category: video.category,
48 licence: video.licence,
50 description: video.description,
55 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, body)
56 .map(this.restExtractor.extractDataBool)
57 .catch(this.restExtractor.handleError)
60 uploadVideo (video: FormData) {
61 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
63 return this.authHttp.request(req)
64 .catch(this.restExtractor.handleError)
67 getVideos (videoPagination: VideoPagination, sort: SortField) {
68 const pagination = this.videoPaginationToRestPagination(videoPagination)
70 let params = new HttpParams()
71 params = this.restService.addRestGetParams(params, pagination, sort)
73 return this.authHttp.get(VideoService.BASE_VIDEO_URL, { params })
74 .map(this.extractVideos)
75 .catch((res) => this.restExtractor.handleError(res))
78 searchVideos (search: Search, videoPagination: VideoPagination, sort: SortField) {
79 const url = VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value)
81 const pagination = this.videoPaginationToRestPagination(videoPagination)
83 let params = new HttpParams()
84 params = this.restService.addRestGetParams(params, pagination, sort)
86 if (search.field) params.set('field', search.field)
88 return this.authHttp.get<ResultList<VideoServerModel>>(url, { params })
89 .map(this.extractVideos)
90 .catch((res) => this.restExtractor.handleError(res))
93 removeVideo (id: number) {
94 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
95 .map(this.restExtractor.extractDataBool)
96 .catch((res) => this.restExtractor.handleError(res))
99 reportVideo (id: number, reason: string) {
100 const url = VideoService.BASE_VIDEO_URL + id + '/abuse'
101 const body: VideoAbuseCreate = {
105 return this.authHttp.post(url, body)
106 .map(this.restExtractor.extractDataBool)
107 .catch(res => this.restExtractor.handleError(res))
110 setVideoLike (id: number) {
111 return this.setVideoRate(id, 'like')
114 setVideoDislike (id: number) {
115 return this.setVideoRate(id, 'dislike')
118 getUserVideoRating (id: number): Observable<UserVideoRate> {
119 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
121 return this.authHttp.get(url)
122 .catch(res => this.restExtractor.handleError(res))
125 private videoPaginationToRestPagination (videoPagination: VideoPagination) {
126 const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage
127 const count: number = videoPagination.itemsPerPage
129 return { start, count }
132 private setVideoRate (id: number, rateType: VideoRateType) {
133 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
134 const body: UserVideoRateUpdate = {
138 return this.authHttp.put(url, body)
139 .map(this.restExtractor.extractDataBool)
140 .catch(res => this.restExtractor.handleError(res))
143 private extractVideos (result: ResultList<VideoServerModel>) {
144 const videosJson = result.data
145 const totalVideos = result.total
148 for (const videoJson of videosJson) {
149 videos.push(new Video(videoJson))
152 return { videos, totalVideos }