]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/angular/videos/services/videos.service.ts
Update to Angular RC 1
[github/Chocobozzz/PeerTube.git] / client / angular / videos / services / 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 '../models/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 => <Video[]> res.json())
17 .catch(this.handleError);
18 }
19
20 getVideo(id: string) {
21 return this.http.get(this._baseVideoUrl + id)
22 .map(res => <Video> res.json())
23 .catch(this.handleError);
24 }
25
26 removeVideo(id: string) {
27 if (confirm('Are you sure?')) {
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
35 searchVideos(search: string) {
36 return this.http.get(this._baseVideoUrl + 'search/' + search)
37 .map(res => <Video> res.json())
38 .catch(this.handleError);
39 }
40
41 private handleError (error: Response) {
42 console.error(error);
43 return Observable.throw(error.json().error || 'Server error');
44 }
45 }