1
0
Fork 0
peertube/client/app/videos/shared/video.service.ts

80 lines
2.6 KiB
TypeScript
Raw Normal View History

2016-05-13 08:18:37 -04:00
import { Injectable } from '@angular/core';
2016-05-23 06:15:03 -04:00
import { Http, Response, URLSearchParams } from '@angular/http';
import { Observable } from 'rxjs/Rx';
2016-03-14 08:50:19 -04:00
import { Pagination } from './pagination.model';
import { Search } from '../../shared/index';
import { SortField } from './sort-field.type';
import { AuthService } from '../../users/index';
import { Video } from './video.model';
2016-03-14 08:50:19 -04:00
@Injectable()
export class VideoService {
2016-05-27 11:25:52 -04:00
private static BASE_VIDEO_URL = '/api/v1/videos/';
2016-03-14 08:50:19 -04:00
2016-05-27 11:25:52 -04:00
constructor(private http: Http, private authService: AuthService) {}
2016-03-14 08:50:19 -04:00
2016-05-23 05:07:42 -04:00
getVideos(pagination: Pagination, sort: SortField) {
const params = this.createPaginationParams(pagination);
2016-05-23 05:07:42 -04:00
2016-05-23 06:15:03 -04:00
if (sort) params.set('sort', sort);
2016-05-23 05:07:42 -04:00
2016-05-27 11:25:52 -04:00
return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
.map(res => res.json())
.map(this.extractVideos)
2016-03-14 08:50:19 -04:00
.catch(this.handleError);
}
getVideo(id: string) {
2016-05-27 11:25:52 -04:00
return this.http.get(VideoService.BASE_VIDEO_URL + id)
2016-03-14 08:50:19 -04:00
.map(res => <Video> res.json())
.catch(this.handleError);
}
removeVideo(id: string) {
2016-05-27 11:25:52 -04:00
const options = this.authService.getAuthRequestOptions();
return this.http.delete(VideoService.BASE_VIDEO_URL + id, options)
.map(res => <number> res.status)
.catch(this.handleError);
2016-03-14 08:50:19 -04:00
}
2016-05-23 05:07:42 -04:00
searchVideos(search: Search, pagination: Pagination, sort: SortField) {
const params = this.createPaginationParams(pagination);
2016-05-23 05:07:42 -04:00
if (search.field) params.set('field', search.field);
2016-05-23 06:15:03 -04:00
if (sort) params.set('sort', sort);
2016-05-23 05:07:42 -04:00
2016-05-27 11:25:52 -04:00
return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
.map(res => res.json())
.map(this.extractVideos)
2016-03-14 17:16:43 -04:00
.catch(this.handleError);
}
2016-05-27 11:25:52 -04:00
private extractVideos(body: any) {
2016-05-22 04:43:06 -04:00
const videos_json = body.data;
const totalVideos = body.total;
const videos = [];
2016-05-22 04:43:06 -04:00
for (const video_json of videos_json) {
videos.push(new Video(video_json));
}
2016-05-22 04:43:06 -04:00
return { videos, totalVideos };
}
2016-05-27 11:25:52 -04:00
private handleError(error: Response) {
2016-03-14 08:50:19 -04:00
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
}