1
0
Fork 0
peertube/client/angular/videos/videos.service.ts

55 lines
1.5 KiB
TypeScript
Raw Normal View History

2016-05-13 08:18:37 -04:00
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';
2016-03-14 08:50:19 -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/';
constructor (private http: Http, private _authService: AuthService) {}
2016-03-14 08:50:19 -04:00
getVideos() {
return this.http.get(this._baseVideoUrl)
.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) {
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)
.map(res => res.json())
.map(this.extractVideos)
2016-03-14 17:16:43 -04:00
.catch(this.handleError);
}
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');
}
}