1 import { Injectable } from '@angular/core';
2 import { Http, Response } from '@angular/http';
3 import { Observable } from 'rxjs/Rx';
5 import { Video } from './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 => res.json())
17 .map(this.extractVideos)
18 .catch(this.handleError);
21 getVideo(id: string) {
22 return this.http.get(this._baseVideoUrl + id)
23 .map(res => <Video> res.json())
24 .catch(this.handleError);
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);
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);
41 private extractVideos (body: any[]) {
43 for (const video_json of body) {
44 videos.push(new Video(video_json));
50 private handleError (error: Response) {
52 return Observable.throw(error.json().error || 'Server error');