]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/shared/video.service.ts
Merge branch 'master' into webseed-merged
[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 .catch((res) => this.restExtractor.handleError(res));
26 }
27
28 getVideos(pagination: RestPagination, sort: SortField) {
29 const params = this.restService.buildRestGetParams(pagination, sort);
30
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));
35 }
36
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));
41 }
42
43 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
44 const params = this.restService.buildRestGetParams(pagination, sort);
45
46 if (search.field) params.set('field', search.field);
47
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));
52 }
53
54 private extractVideos(result: ResultList) {
55 const videosJson = result.data;
56 const totalVideos = result.total;
57 const videos = [];
58 for (const videoJson of videosJson) {
59 videos.push(new Video(videoJson));
60 }
61
62 return { videos, totalVideos };
63 }
64 }