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-04-14 16:07:46 -04:00
|
|
|
import { Video } from '../models/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)
|
|
|
|
.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?')) {
|
2016-04-14 16:07:46 -04:00
|
|
|
const options = this._authService.getAuthRequestOptions();
|
|
|
|
return this.http.delete(this._baseVideoUrl + id, options)
|
2016-03-14 08:50:19 -04:00
|
|
|
.map(res => <number> res.status)
|
|
|
|
.catch(this.handleError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 17:16:43 -04:00
|
|
|
searchVideos(search: string) {
|
|
|
|
return this.http.get(this._baseVideoUrl + 'search/' + search)
|
|
|
|
.map(res => <Video> res.json())
|
|
|
|
.catch(this.handleError);
|
|
|
|
}
|
|
|
|
|
2016-03-14 08:50:19 -04:00
|
|
|
private handleError (error: Response) {
|
|
|
|
console.error(error);
|
|
|
|
return Observable.throw(error.json().error || 'Server error');
|
|
|
|
}
|
|
|
|
}
|