]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/angular/videos/videos.service.ts
Add the list of files inside tsconfig
[github/Chocobozzz/PeerTube.git] / client / angular / videos / videos.service.ts
1 import { Injectable } from '@angular/core';
2 import { Http, Response } from '@angular/http';
3 import { Observable } from 'rxjs/Rx';
4
5 import { Video } from './video';
6 import { AuthService } from '../users/services/auth.service';
7
8 @Injectable()
9 export class VideosService {
10 private _baseVideoUrl = '/api/v1/videos/';
11
12 constructor (private http: Http, private _authService: AuthService) {}
13
14 getVideos() {
15 return this.http.get(this._baseVideoUrl)
16 .map(res => res.json())
17 .map(this.extractVideos)
18 .catch(this.handleError);
19 }
20
21 getVideo(id: string) {
22 return this.http.get(this._baseVideoUrl + id)
23 .map(res => <Video> res.json())
24 .catch(this.handleError);
25 }
26
27 removeVideo(id: string) {
28 const options = this._authService.getAuthRequestOptions();
29 return this.http.delete(this._baseVideoUrl + id, options)
30 .map(res => <number> res.status)
31 .catch(this.handleError);
32 }
33
34 searchVideos(search: string) {
35 return this.http.get(this._baseVideoUrl + 'search/' + search)
36 .map(res => res.json())
37 .map(this.extractVideos)
38 .catch(this.handleError);
39 }
40
41 private extractVideos (body: any[]) {
42 const videos = [];
43 for (const video_json of body) {
44 videos.push(new Video(video_json));
45 }
46
47 return videos;
48 }
49
50 private handleError (error: Response) {
51 console.error(error);
52 return Observable.throw(error.json().error || 'Server error');
53 }
54 }