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 { RateType } from './rate-type.type';
10 import { AuthService } from '../../core';
18 } from '../../shared';
19 import { Video } from './video.model';
22 export class VideoService {
23 private static BASE_VIDEO_URL = '/api/v1/videos/';
25 videoCategories: Array<{ id: number, label: string }> = [];
26 videoLicences: Array<{ id: number, label: string }> = [];
27 videoLanguages: Array<{ id: number, label: string }> = [];
30 private authService: AuthService,
31 private authHttp: AuthHttp,
33 private restExtractor: RestExtractor,
34 private restService: RestService
37 loadVideoCategories() {
38 return this.http.get(VideoService.BASE_VIDEO_URL + 'categories')
39 .map(this.restExtractor.extractDataGet)
41 Object.keys(data).forEach(categoryKey => {
42 this.videoCategories.push({
43 id: parseInt(categoryKey),
44 label: data[categoryKey]
51 return this.http.get(VideoService.BASE_VIDEO_URL + 'licences')
52 .map(this.restExtractor.extractDataGet)
54 Object.keys(data).forEach(licenceKey => {
55 this.videoLicences.push({
56 id: parseInt(licenceKey),
57 label: data[licenceKey]
63 loadVideoLanguages() {
64 return this.http.get(VideoService.BASE_VIDEO_URL + 'languages')
65 .map(this.restExtractor.extractDataGet)
67 Object.keys(data).forEach(languageKey => {
68 this.videoLanguages.push({
69 id: parseInt(languageKey),
70 label: data[languageKey]
76 getVideo(id: string): Observable<Video> {
77 return this.http.get(VideoService.BASE_VIDEO_URL + id)
78 .map(this.restExtractor.extractDataGet)
79 .map(video_hash => new Video(video_hash))
80 .catch((res) => this.restExtractor.handleError(res));
83 updateVideo(video: Video) {
84 const language = video.language ? video.language : null;
88 category: video.category,
89 licence: video.licence,
91 description: video.description,
95 const headers = new Headers({ 'Content-Type': 'application/json' });
96 const options = new RequestOptions({ headers: headers });
98 return this.authHttp.put(`${VideoService.BASE_VIDEO_URL}/${video.id}`, body, options)
99 .map(this.restExtractor.extractDataBool)
100 .catch(this.restExtractor.handleError);
103 getVideos(pagination: RestPagination, sort: SortField) {
104 const params = this.restService.buildRestGetParams(pagination, sort);
106 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
107 .map(res => res.json())
108 .map(this.extractVideos)
109 .catch((res) => this.restExtractor.handleError(res));
112 removeVideo(id: string) {
113 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
114 .map(this.restExtractor.extractDataBool)
115 .catch((res) => this.restExtractor.handleError(res));
118 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
119 const params = this.restService.buildRestGetParams(pagination, sort);
121 if (search.field) params.set('field', search.field);
123 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
124 .map(this.restExtractor.extractDataList)
125 .map(this.extractVideos)
126 .catch((res) => this.restExtractor.handleError(res));
129 reportVideo(id: string, reason: string) {
130 const url = VideoService.BASE_VIDEO_URL + id + '/abuse';
135 return this.authHttp.post(url, body)
136 .map(this.restExtractor.extractDataBool)
137 .catch((res) => this.restExtractor.handleError(res));
140 setVideoLike(id: string) {
141 return this.setVideoRate(id, 'like');
144 setVideoDislike(id: string) {
145 return this.setVideoRate(id, 'dislike');
148 getUserVideoRating(id: string) {
149 const url = UserService.BASE_USERS_URL + '/me/videos/' + id + '/rating';
151 return this.authHttp.get(url)
152 .map(this.restExtractor.extractDataGet)
153 .catch((res) => this.restExtractor.handleError(res));
156 blacklistVideo(id: string) {
157 return this.authHttp.post(VideoService.BASE_VIDEO_URL + id + '/blacklist', {})
158 .map(this.restExtractor.extractDataBool)
159 .catch((res) => this.restExtractor.handleError(res));
162 private setVideoRate(id: string, rateType: RateType) {
163 const url = VideoService.BASE_VIDEO_URL + id + '/rate';
168 return this.authHttp.put(url, body)
169 .map(this.restExtractor.extractDataBool)
170 .catch((res) => this.restExtractor.handleError(res));
173 private extractVideos(result: ResultList) {
174 const videosJson = result.data;
175 const totalVideos = result.total;
177 for (const videoJson of videosJson) {
178 videos.push(new Video(videoJson));
181 return { videos, totalVideos };