1 import { Injectable } from '@angular/core'
2 import { Http, Headers, RequestOptions } from '@angular/http'
3 import { Observable } from 'rxjs/Observable'
4 import 'rxjs/add/operator/catch'
5 import 'rxjs/add/operator/map'
7 import { Search } from '../../shared'
8 import { SortField } from './sort-field.type'
9 import { AuthService } from '../../core'
18 import { Video } from './video.model'
25 } from '../../../../../shared'
28 export class VideoService {
29 private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
31 videoCategories: Array<{ id: number, label: string }> = []
32 videoLicences: Array<{ id: number, label: string }> = []
33 videoLanguages: Array<{ id: number, label: string }> = []
36 private authService: AuthService,
37 private authHttp: AuthHttp,
39 private restExtractor: RestExtractor,
40 private restService: RestService
43 loadVideoCategories () {
44 return this.loadVideoAttributeEnum('categories', this.videoCategories)
47 loadVideoLicences () {
48 return this.loadVideoAttributeEnum('licences', this.videoLicences)
51 loadVideoLanguages () {
52 return this.loadVideoAttributeEnum('languages', this.videoLanguages)
55 getVideo (uuid: string): Observable<Video> {
56 return this.http.get(VideoService.BASE_VIDEO_URL + uuid)
57 .map(this.restExtractor.extractDataGet)
58 .map(videoHash => new Video(videoHash))
59 .catch((res) => this.restExtractor.handleError(res))
62 updateVideo (video: Video) {
63 const language = video.language ? video.language : null
65 const body: VideoUpdate = {
67 category: video.category,
68 licence: video.licence,
70 description: video.description,
75 const headers = new Headers({ 'Content-Type': 'application/json' })
76 const options = new RequestOptions({ headers: headers })
78 return this.authHttp.put(`${VideoService.BASE_VIDEO_URL}/${video.id}`, body, options)
79 .map(this.restExtractor.extractDataBool)
80 .catch(this.restExtractor.handleError)
83 getVideos (pagination: RestPagination, sort: SortField) {
84 const params = this.restService.buildRestGetParams(pagination, sort)
86 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
87 .map(res => res.json())
88 .map(this.extractVideos)
89 .catch((res) => this.restExtractor.handleError(res))
92 removeVideo (id: number) {
93 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
94 .map(this.restExtractor.extractDataBool)
95 .catch((res) => this.restExtractor.handleError(res))
98 searchVideos (search: Search, pagination: RestPagination, sort: SortField) {
99 const params = this.restService.buildRestGetParams(pagination, sort)
101 if (search.field) params.set('field', search.field)
103 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
104 .map(this.restExtractor.extractDataList)
105 .map(this.extractVideos)
106 .catch((res) => this.restExtractor.handleError(res))
109 reportVideo (id: number, reason: string) {
110 const url = VideoService.BASE_VIDEO_URL + id + '/abuse'
111 const body: VideoAbuseCreate = {
115 return this.authHttp.post(url, body)
116 .map(this.restExtractor.extractDataBool)
117 .catch((res) => this.restExtractor.handleError(res))
120 setVideoLike (id: number) {
121 return this.setVideoRate(id, 'like')
124 setVideoDislike (id: number) {
125 return this.setVideoRate(id, 'dislike')
128 getUserVideoRating (id: number): Observable<UserVideoRate> {
129 const url = UserService.BASE_USERS_URL + '/me/videos/' + id + '/rating'
131 return this.authHttp.get(url)
132 .map(this.restExtractor.extractDataGet)
133 .catch((res) => this.restExtractor.handleError(res))
136 blacklistVideo (id: number) {
137 return this.authHttp.post(VideoService.BASE_VIDEO_URL + id + '/blacklist', {})
138 .map(this.restExtractor.extractDataBool)
139 .catch((res) => this.restExtractor.handleError(res))
142 private setVideoRate (id: number, rateType: VideoRateType) {
143 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
144 const body: UserVideoRateUpdate = {
148 return this.authHttp.put(url, body)
149 .map(this.restExtractor.extractDataBool)
150 .catch((res) => this.restExtractor.handleError(res))
153 private extractVideos (result: ResultList) {
154 const videosJson = result.data
155 const totalVideos = result.total
157 for (const videoJson of videosJson) {
158 videos.push(new Video(videoJson))
161 return { videos, totalVideos }
164 private loadVideoAttributeEnum (attributeName: 'categories' | 'licences' | 'languages', hashToPopulate: { id: number, label: string }[]) {
165 return this.http.get(VideoService.BASE_VIDEO_URL + attributeName)
166 .map(this.restExtractor.extractDataGet)
168 Object.keys(data).forEach(dataKey => {
169 hashToPopulate.push({
170 id: parseInt(dataKey, 10),