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 { AuthService } from '../../core';
10 import { AuthHttp, RestExtractor, RestPagination, RestService, ResultList } from '../../shared';
11 import { Video } from './video.model';
14 export class VideoService {
15 private static BASE_VIDEO_URL = '/api/v1/videos/';
18 private authService: AuthService,
19 private authHttp: AuthHttp,
21 private restExtractor: RestExtractor,
22 private restService: RestService
25 getVideo(id: string): Observable<Video> {
26 return this.http.get(VideoService.BASE_VIDEO_URL + id)
27 .map(this.restExtractor.extractDataGet)
28 .map(video_hash => new Video(video_hash))
29 .catch((res) => this.restExtractor.handleError(res));
32 getVideos(pagination: RestPagination, sort: SortField) {
33 const params = this.restService.buildRestGetParams(pagination, sort);
35 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
36 .map(res => res.json())
37 .map(this.extractVideos)
38 .catch((res) => this.restExtractor.handleError(res));
41 removeVideo(id: string) {
42 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
43 .map(this.restExtractor.extractDataBool)
44 .catch((res) => this.restExtractor.handleError(res));
47 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
48 const params = this.restService.buildRestGetParams(pagination, sort);
50 if (search.field) params.set('field', search.field);
52 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
53 .map(this.restExtractor.extractDataList)
54 .map(this.extractVideos)
55 .catch((res) => this.restExtractor.handleError(res));
58 reportVideo(id: string, reason: string) {
62 const url = VideoService.BASE_VIDEO_URL + id + '/abuse';
64 return this.authHttp.post(url, body)
65 .map(this.restExtractor.extractDataBool)
66 .catch((res) => this.restExtractor.handleError(res));
69 private extractVideos(result: ResultList) {
70 const videosJson = result.data;
71 const totalVideos = result.total;
73 for (const videoJson of videosJson) {
74 videos.push(new Video(videoJson));
77 return { videos, totalVideos };