1 import { Injectable } from '@angular/core';
2 import { Http } 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 }> = [];
28 private authService: AuthService,
29 private authHttp: AuthHttp,
31 private restExtractor: RestExtractor,
32 private restService: RestService
35 loadVideoCategories() {
36 return this.http.get(VideoService.BASE_VIDEO_URL + 'categories')
37 .map(this.restExtractor.extractDataGet)
39 Object.keys(data).forEach(categoryKey => {
40 this.videoCategories.push({
41 id: parseInt(categoryKey),
42 label: data[categoryKey]
48 getVideo(id: string): Observable<Video> {
49 return this.http.get(VideoService.BASE_VIDEO_URL + id)
50 .map(this.restExtractor.extractDataGet)
51 .map(video_hash => new Video(video_hash))
52 .catch((res) => this.restExtractor.handleError(res));
55 getVideos(pagination: RestPagination, sort: SortField) {
56 const params = this.restService.buildRestGetParams(pagination, sort);
58 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
59 .map(res => res.json())
60 .map(this.extractVideos)
61 .catch((res) => this.restExtractor.handleError(res));
64 removeVideo(id: string) {
65 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
66 .map(this.restExtractor.extractDataBool)
67 .catch((res) => this.restExtractor.handleError(res));
70 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
71 const params = this.restService.buildRestGetParams(pagination, sort);
73 if (search.field) params.set('field', search.field);
75 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
76 .map(this.restExtractor.extractDataList)
77 .map(this.extractVideos)
78 .catch((res) => this.restExtractor.handleError(res));
81 reportVideo(id: string, reason: string) {
82 const url = VideoService.BASE_VIDEO_URL + id + '/abuse';
87 return this.authHttp.post(url, body)
88 .map(this.restExtractor.extractDataBool)
89 .catch((res) => this.restExtractor.handleError(res));
92 setVideoLike(id: string) {
93 return this.setVideoRate(id, 'like');
96 setVideoDislike(id: string) {
97 return this.setVideoRate(id, 'dislike');
100 getUserVideoRating(id: string) {
101 const url = UserService.BASE_USERS_URL + '/me/videos/' + id + '/rating';
103 return this.authHttp.get(url)
104 .map(this.restExtractor.extractDataGet)
105 .catch((res) => this.restExtractor.handleError(res));
108 private setVideoRate(id: string, rateType: RateType) {
109 const url = VideoService.BASE_VIDEO_URL + id + '/rate';
114 return this.authHttp.put(url, body)
115 .map(this.restExtractor.extractDataBool)
116 .catch((res) => this.restExtractor.handleError(res));
119 private extractVideos(result: ResultList) {
120 const videosJson = result.data;
121 const totalVideos = result.total;
123 for (const videoJson of videosJson) {
124 videos.push(new Video(videoJson));
127 return { videos, totalVideos };