]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/shared/video.service.ts
b1f6880954d95a8ce309bd53260b140dde231f69
[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
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';
9
10 @Injectable()
11 export class VideoService {
12 private static BASE_VIDEO_URL = '/api/v1/videos/';
13
14 constructor(
15 private authService: AuthService,
16 private authHttp: AuthHttp,
17 private http: Http,
18 private restExtractor: RestExtractor,
19 private restService: RestService
20 ) {}
21
22 getVideo(id: string): Observable<Video> {
23 return this.http.get(VideoService.BASE_VIDEO_URL + id)
24 .map(this.restExtractor.extractDataGet)
25 .map(video_hash => new Video(video_hash))
26 .catch((res) => this.restExtractor.handleError(res));
27 }
28
29 getVideos(pagination: RestPagination, sort: SortField) {
30 const params = this.restService.buildRestGetParams(pagination, sort);
31
32 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
33 .map(res => res.json())
34 .map(this.extractVideos)
35 .catch((res) => this.restExtractor.handleError(res));
36 }
37
38 removeVideo(id: string) {
39 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
40 .map(this.restExtractor.extractDataBool)
41 .catch((res) => this.restExtractor.handleError(res));
42 }
43
44 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
45 const params = this.restService.buildRestGetParams(pagination, sort);
46
47 if (search.field) params.set('field', search.field);
48
49 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
50 .map(this.restExtractor.extractDataList)
51 .map(this.extractVideos)
52 .catch((res) => this.restExtractor.handleError(res));
53 }
54
55 private extractVideos(result: ResultList) {
56 const videosJson = result.data;
57 const totalVideos = result.total;
58 const videos = [];
59 for (const videoJson of videosJson) {
60 videos.push(new Video(videoJson));
61 }
62
63 return { videos, totalVideos };
64 }
65 }