2016-05-13 08:18:37 -04:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Http, Response } from '@angular/http';
|
2016-04-14 16:07:46 -04:00
|
|
|
import { Observable } from 'rxjs/Rx';
|
2016-03-14 08:50:19 -04:00
|
|
|
|
2016-05-21 12:03:34 -04:00
|
|
|
import { Video } from './video';
|
|
|
|
import { AuthService } from '../users/services/auth.service';
|
2016-03-14 08:50:19 -04:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class VideosService {
|
|
|
|
private _baseVideoUrl = '/api/v1/videos/';
|
|
|
|
|
2016-04-14 16:07:46 -04:00
|
|
|
constructor (private http: Http, private _authService: AuthService) {}
|
2016-03-14 08:50:19 -04:00
|
|
|
|
|
|
|
getVideos() {
|
|
|
|
return this.http.get(this._baseVideoUrl)
|
2016-05-21 12:03:34 -04:00
|
|
|
.map(res => res.json())
|
|
|
|
.map(this.extractVideos)
|
2016-03-14 08:50:19 -04:00
|
|
|
.catch(this.handleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
getVideo(id: string) {
|
|
|
|
return this.http.get(this._baseVideoUrl + id)
|
|
|
|
.map(res => <Video> res.json())
|
|
|
|
.catch(this.handleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeVideo(id: string) {
|
2016-05-21 12:03:34 -04:00
|
|
|
const options = this._authService.getAuthRequestOptions();
|
|
|
|
return this.http.delete(this._baseVideoUrl + id, options)
|
|
|
|
.map(res => <number> res.status)
|
|
|
|
.catch(this.handleError);
|
2016-03-14 08:50:19 -04:00
|
|
|
}
|
|
|
|
|
2016-03-14 17:16:43 -04:00
|
|
|
searchVideos(search: string) {
|
|
|
|
return this.http.get(this._baseVideoUrl + 'search/' + search)
|
2016-05-21 12:03:34 -04:00
|
|
|
.map(res => res.json())
|
|
|
|
.map(this.extractVideos)
|
2016-03-14 17:16:43 -04:00
|
|
|
.catch(this.handleError);
|
|
|
|
}
|
|
|
|
|
2016-05-21 12:03:34 -04:00
|
|
|
private extractVideos (body: any[]) {
|
|
|
|
const videos = [];
|
|
|
|
for (const video_json of body) {
|
|
|
|
videos.push(new Video(video_json));
|
|
|
|
}
|
|
|
|
|
|
|
|
return videos;
|
|
|
|
}
|
|
|
|
|
2016-03-14 08:50:19 -04:00
|
|
|
private handleError (error: Response) {
|
|
|
|
console.error(error);
|
|
|
|
return Observable.throw(error.json().error || 'Server error');
|
|
|
|
}
|
|
|
|
}
|