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 { AuthHttp, AuthService, RestExtractor, RestPagination, RestService, ResultList } from '../../shared';
8 import { Video } from './video.model';
11 export class VideoService {
12 private static BASE_VIDEO_URL = '/api/v1/videos/';
15 private authService: AuthService,
16 private authHttp: AuthHttp,
18 private restExtractor: RestExtractor,
19 private restService: RestService
22 getVideo(id: string): Observable<Video> {
23 return this.http.get(VideoService.BASE_VIDEO_URL + id)
24 .map(this.restExtractor.extractDataGet)
25 .catch((res) => this.restExtractor.handleError(res));
28 getVideos(pagination: RestPagination, sort: SortField) {
29 const params = this.restService.buildRestGetParams(pagination, sort);
31 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
32 .map(res => res.json())
33 .map(this.extractVideos)
34 .catch((res) => this.restExtractor.handleError(res));
37 removeVideo(id: string) {
38 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
39 .map(this.restExtractor.extractDataBool)
40 .catch((res) => this.restExtractor.handleError(res));
43 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
44 const params = this.restService.buildRestGetParams(pagination, sort);
46 if (search.field) params.set('field', search.field);
48 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
49 .map(this.restExtractor.extractDataList)
50 .map(this.extractVideos)
51 .catch((res) => this.restExtractor.handleError(res));
54 private extractVideos(result: ResultList) {
55 const videosJson = result.data;
56 const totalVideos = result.total;
58 for (const videoJson of videosJson) {
59 videos.push(new Video(videoJson));
62 return { videos, totalVideos };