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'
24 Video as VideoServerModel,
25 VideoDetails as VideoDetailsServerModel,
27 } from '../../../../../shared'
30 export class VideoService {
31 private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
34 private authHttp: HttpClient,
35 private restExtractor: RestExtractor,
36 private restService: RestService
39 getVideo (uuid: string) {
40 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
41 .map(videoHash => new VideoDetails(videoHash))
42 .catch((res) => this.restExtractor.handleError(res))
45 updateVideo (video: VideoEdit) {
46 const language = video.language ? video.language : null
48 const body: VideoUpdate = {
50 category: video.category,
51 licence: video.licence,
53 description: video.description,
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 })
66 return this.authHttp.request(req)
67 .catch(this.restExtractor.handleError)
70 getVideos (videoPagination: VideoPagination, sort: SortField) {
71 const pagination = this.videoPaginationToRestPagination(videoPagination)
73 let params = new HttpParams()
74 params = this.restService.addRestGetParams(params, pagination, sort)
76 return this.authHttp.get(VideoService.BASE_VIDEO_URL, { params })
77 .map(this.extractVideos)
78 .catch((res) => this.restExtractor.handleError(res))
81 searchVideos (search: Search, videoPagination: VideoPagination, sort: SortField) {
82 const url = VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value)
84 const pagination = this.videoPaginationToRestPagination(videoPagination)
86 let params = new HttpParams()
87 params = this.restService.addRestGetParams(params, pagination, sort)
89 if (search.field) params.set('field', search.field)
91 return this.authHttp.get<ResultList<VideoServerModel>>(url, { params })
92 .map(this.extractVideos)
93 .catch((res) => this.restExtractor.handleError(res))
96 removeVideo (id: number) {
97 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
98 .map(this.restExtractor.extractDataBool)
99 .catch((res) => this.restExtractor.handleError(res))
102 reportVideo (id: number, reason: string) {
103 const url = VideoService.BASE_VIDEO_URL + id + '/abuse'
104 const body: VideoAbuseCreate = {
108 return this.authHttp.post(url, body)
109 .map(this.restExtractor.extractDataBool)
110 .catch(res => this.restExtractor.handleError(res))
113 setVideoLike (id: number) {
114 return this.setVideoRate(id, 'like')
117 setVideoDislike (id: number) {
118 return this.setVideoRate(id, 'dislike')
121 getUserVideoRating (id: number): Observable<UserVideoRate> {
122 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
124 return this.authHttp.get(url)
125 .catch(res => this.restExtractor.handleError(res))
128 private videoPaginationToRestPagination (videoPagination: VideoPagination) {
129 const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage
130 const count: number = videoPagination.itemsPerPage
132 return { start, count }
135 private setVideoRate (id: number, rateType: VideoRateType) {
136 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
137 const body: UserVideoRateUpdate = {
141 return this.authHttp.put(url, body)
142 .map(this.restExtractor.extractDataBool)
143 .catch(res => this.restExtractor.handleError(res))
146 private extractVideos (result: ResultList<VideoServerModel>) {
147 const videosJson = result.data
148 const totalVideos = result.total
151 for (const videoJson of videosJson) {
152 videos.push(new Video(videoJson))
155 return { videos, totalVideos }