]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/angular/videos/services/videos.service.ts
Angular 2 : draft 2
[github/Chocobozzz/PeerTube.git] / client / angular / videos / services / videos.service.ts
1 import {Injectable} from 'angular2/core';
2 import {Http, Response} from 'angular2/http';
3 import {Observable} from 'rxjs/Rx';
4
5 import {Video} from '../models/video';
6
7 @Injectable()
8 export class VideosService {
9 private _baseVideoUrl = '/api/v1/videos/';
10
11 constructor (private http: Http) {}
12
13 getVideos() {
14 return this.http.get(this._baseVideoUrl)
15 .map(res => <Video[]> res.json())
16 .catch(this.handleError);
17 }
18
19 getVideo(id: string) {
20 return this.http.get(this._baseVideoUrl + id)
21 .map(res => <Video> res.json())
22 .catch(this.handleError);
23 }
24
25 removeVideo(id: string) {
26 if (confirm('Are you sure?')) {
27 return this.http.delete(this._baseVideoUrl + id)
28 .map(res => <number> res.status)
29 .catch(this.handleError);
30 }
31 }
32
33 searchVideos(search: string) {
34 return this.http.get(this._baseVideoUrl + 'search/' + search)
35 .map(res => <Video> res.json())
36 .catch(this.handleError);
37 }
38
39 private handleError (error: Response) {
40 console.error(error);
41 return Observable.throw(error.json().error || 'Server error');
42 }
43 }