2016-05-13 08:18:37 -04:00
|
|
|
import { Injectable } from '@angular/core';
|
2016-05-22 04:43:06 -04:00
|
|
|
import { Http, Response, RequestOptions, URLSearchParams } 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-22 04:43:06 -04:00
|
|
|
import { Pagination } from './pagination';
|
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
|
|
|
|
2016-05-22 04:43:06 -04:00
|
|
|
getVideos(pagination: Pagination) {
|
|
|
|
const params = { search: this.createPaginationParams(pagination) };
|
|
|
|
return this.http.get(this._baseVideoUrl, params)
|
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-05-22 04:43:06 -04:00
|
|
|
searchVideos(search: string, pagination: Pagination) {
|
|
|
|
const params = { search: this.createPaginationParams(pagination) };
|
|
|
|
return this.http.get(this._baseVideoUrl + 'search/' + encodeURIComponent(search), params)
|
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-22 04:43:06 -04:00
|
|
|
private extractVideos (body: any) {
|
|
|
|
const videos_json = body.data;
|
|
|
|
const totalVideos = body.total;
|
2016-05-21 12:03:34 -04:00
|
|
|
const videos = [];
|
2016-05-22 04:43:06 -04:00
|
|
|
for (const video_json of videos_json) {
|
2016-05-21 12:03:34 -04:00
|
|
|
videos.push(new Video(video_json));
|
|
|
|
}
|
|
|
|
|
2016-05-22 04:43:06 -04:00
|
|
|
return { videos, totalVideos };
|
2016-05-21 12:03:34 -04:00
|
|
|
}
|
|
|
|
|
2016-03-14 08:50:19 -04:00
|
|
|
private handleError (error: Response) {
|
|
|
|
console.error(error);
|
|
|
|
return Observable.throw(error.json().error || 'Server error');
|
|
|
|
}
|
2016-05-22 04:43:06 -04:00
|
|
|
|
|
|
|
private createPaginationParams(pagination: Pagination) {
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
const start: number = (pagination.currentPage - 1) * pagination.itemsPerPage;
|
|
|
|
const count: number = pagination.itemsPerPage;
|
|
|
|
|
|
|
|
params.set('start', start.toString());
|
|
|
|
params.set('count', count.toString());
|
|
|
|
|
|
|
|
return params;
|
|
|
|
}
|
2016-03-14 08:50:19 -04:00
|
|
|
}
|