]>
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 | |
1553e15d C |
5 | import { Video } from '../models/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) | |
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?')) { | |
1553e15d C |
28 | const options = this._authService.getAuthRequestOptions(); |
29 | return this.http.delete(this._baseVideoUrl + id, options) | |
dc8bc31b C |
30 | .map(res => <number> res.status) |
31 | .catch(this.handleError); | |
32 | } | |
33 | } | |
34 | ||
98b01bac C |
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 | ||
dc8bc31b C |
41 | private handleError (error: Response) { |
42 | console.error(error); | |
43 | return Observable.throw(error.json().error || 'Server error'); | |
44 | } | |
45 | } |