]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/shared/video.service.ts
Client: add basic aot support
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / shared / video.service.ts
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';
6
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';
12
13 @Injectable()
14 export class VideoService {
15 private static BASE_VIDEO_URL = '/api/v1/videos/';
16
17 constructor(
18 private authService: AuthService,
19 private authHttp: AuthHttp,
20 private http: Http,
21 private restExtractor: RestExtractor,
22 private restService: RestService
23 ) {}
24
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));
30 }
31
32 getVideos(pagination: RestPagination, sort: SortField) {
33 const params = this.restService.buildRestGetParams(pagination, sort);
34
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));
39 }
40
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));
45 }
46
47 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
48 const params = this.restService.buildRestGetParams(pagination, sort);
49
50 if (search.field) params.set('field', search.field);
51
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));
56 }
57
58 private extractVideos(result: ResultList) {
59 const videosJson = result.data;
60 const totalVideos = result.total;
61 const videos = [];
62 for (const videoJson of videosJson) {
63 videos.push(new Video(videoJson));
64 }
65
66 return { videos, totalVideos };
67 }
68 }