aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/angular/videos/videos.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/angular/videos/videos.service.ts')
-rw-r--r--client/angular/videos/videos.service.ts54
1 files changed, 54 insertions, 0 deletions
diff --git a/client/angular/videos/videos.service.ts b/client/angular/videos/videos.service.ts
new file mode 100644
index 000000000..f4790b511
--- /dev/null
+++ b/client/angular/videos/videos.service.ts
@@ -0,0 +1,54 @@
1import { Injectable } from '@angular/core';
2import { Http, Response } from '@angular/http';
3import { Observable } from 'rxjs/Rx';
4
5import { Video } from './video';
6import { AuthService } from '../users/services/auth.service';
7
8@Injectable()
9export 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 => res.json())
17 .map(this.extractVideos)
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) {
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 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);
39 }
40
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
50 private handleError (error: Response) {
51 console.error(error);
52 return Observable.throw(error.json().error || 'Server error');
53 }
54}