1 import { Injectable } from 'angular2/core';
2 import { Http, Response } from 'angular2/http';
3 import { Observable } from 'rxjs/Rx';
5 import { Video } from '../models/video';
6 import { AuthService } from '../../users/services/auth.service';
9 export class VideosService {
10 private _baseVideoUrl = '/api/v1/videos/';
12 constructor (private http: Http, private _authService: AuthService) {}
15 return this.http.get(this._baseVideoUrl)
16 .map(res => <Video[]> res.json())
17 .catch(this.handleError);
20 getVideo(id: string) {
21 return this.http.get(this._baseVideoUrl + id)
22 .map(res => <Video> res.json())
23 .catch(this.handleError);
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);
35 searchVideos(search: string) {
36 return this.http.get(this._baseVideoUrl + 'search/' + search)
37 .map(res => <Video> res.json())
38 .catch(this.handleError);
41 private handleError (error: Response) {
43 return Observable.throw(error.json().error || 'Server error');