aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/angular/videos/services/videos.service.ts
blob: 74b6a1ddc46f5573d24e344671e4d87bc33c2de3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Injectable } from 'angular2/core';
import { Http, Response } from 'angular2/http';
import { Observable } from 'rxjs/Rx';

import { Video } from '../models/video';
import { AuthService } from '../../users/services/auth.service';

@Injectable()
export class VideosService {
  private _baseVideoUrl = '/api/v1/videos/';

  constructor (private http: Http, private _authService: AuthService) {}

  getVideos() {
    return this.http.get(this._baseVideoUrl)
                    .map(res => <Video[]> res.json())
                    .catch(this.handleError);
  }

  getVideo(id: string) {
    return this.http.get(this._baseVideoUrl + id)
                    .map(res => <Video> res.json())
                    .catch(this.handleError);
  }

  removeVideo(id: string) {
    if (confirm('Are you sure?')) {
      const options = this._authService.getAuthRequestOptions();
      return this.http.delete(this._baseVideoUrl + id, options)
                      .map(res => <number> res.status)
                      .catch(this.handleError);
    }
  }

  searchVideos(search: string) {
    return this.http.get(this._baseVideoUrl + 'search/' + search)
                    .map(res => <Video> res.json())
                    .catch(this.handleError);
  }

  private handleError (error: Response) {
    console.error(error);
    return Observable.throw(error.json().error || 'Server error');
  }
}