]>
Commit | Line | Data |
---|---|---|
230809ef C |
1 | import { Injectable } from '@angular/core'; |
2 | import { Http, Response } from '@angular/http'; | |
1553e15d | 3 | import { Observable } from 'rxjs/Rx'; |
dc8bc31b | 4 | |
501bc6c2 C |
5 | import { Video } from './video'; |
6 | import { AuthService } from '../users/services/auth.service'; | |
dc8bc31b C |
7 | |
8 | @Injectable() | |
9 | export class VideosService { | |
10 | private _baseVideoUrl = '/api/v1/videos/'; | |
11 | ||
1553e15d | 12 | constructor (private http: Http, private _authService: AuthService) {} |
dc8bc31b C |
13 | |
14 | getVideos() { | |
15 | return this.http.get(this._baseVideoUrl) | |
501bc6c2 C |
16 | .map(res => res.json()) |
17 | .map(this.extractVideos) | |
dc8bc31b C |
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) { | |
501bc6c2 C |
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); | |
dc8bc31b C |
32 | } |
33 | ||
98b01bac C |
34 | searchVideos(search: string) { |
35 | return this.http.get(this._baseVideoUrl + 'search/' + search) | |
501bc6c2 C |
36 | .map(res => res.json()) |
37 | .map(this.extractVideos) | |
98b01bac C |
38 | .catch(this.handleError); |
39 | } | |
40 | ||
501bc6c2 C |
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 | ||
dc8bc31b C |
50 | private handleError (error: Response) { |
51 | console.error(error); | |
52 | return Observable.throw(error.json().error || 'Server error'); | |
53 | } | |
54 | } |