1 import { Injectable } from '@angular/core';
2 import { Http } from '@angular/http';
3 import { Observable } from 'rxjs/Observable';
5 import { Search } from '../../shared';
6 import { SortField } from './sort-field.type';
7 import { AuthService } from '../../core';
8 import { AuthHttp, RestExtractor, RestPagination, RestService, ResultList } from '../../shared';
9 import { Video } from './video.model';
12 export class VideoService {
13 private static BASE_VIDEO_URL = '/api/v1/videos/';
16 private authService: AuthService,
17 private authHttp: AuthHttp,
19 private restExtractor: RestExtractor,
20 private restService: RestService
23 getVideo(id: string): Observable<Video> {
24 return this.http.get(VideoService.BASE_VIDEO_URL + id)
25 .map(this.restExtractor.extractDataGet)
26 .map(video_hash => new Video(video_hash))
27 .catch((res) => this.restExtractor.handleError(res));
30 getVideos(pagination: RestPagination, sort: SortField) {
31 const params = this.restService.buildRestGetParams(pagination, sort);
33 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
34 .map(res => res.json())
35 .map(this.extractVideos)
36 .catch((res) => this.restExtractor.handleError(res));
39 removeVideo(id: string) {
40 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
41 .map(this.restExtractor.extractDataBool)
42 .catch((res) => this.restExtractor.handleError(res));
45 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
46 const params = this.restService.buildRestGetParams(pagination, sort);
48 if (search.field) params.set('field', search.field);
50 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
51 .map(this.restExtractor.extractDataList)
52 .map(this.extractVideos)
53 .catch((res) => this.restExtractor.handleError(res));
56 private extractVideos(result: ResultList) {
57 const videosJson = result.data;
58 const totalVideos = result.total;
60 for (const videoJson of videosJson) {
61 videos.push(new Video(videoJson));
64 return { videos, totalVideos };