]>
Commit | Line | Data |
---|---|---|
230809ef | 1 | import { Injectable } from '@angular/core'; |
aff038cd | 2 | import { Http, Response, URLSearchParams } from '@angular/http'; |
1553e15d | 3 | import { Observable } from 'rxjs/Rx'; |
dc8bc31b | 4 | |
32294074 | 5 | import { Pagination } from './pagination'; |
501bc6c2 C |
6 | import { Video } from './video'; |
7 | import { AuthService } from '../users/services/auth.service'; | |
471bc22f | 8 | import { Search } from '../app/search'; |
cf20596c | 9 | import { SortField } from './components/list/sort'; |
dc8bc31b C |
10 | |
11 | @Injectable() | |
12 | export class VideosService { | |
13 | private _baseVideoUrl = '/api/v1/videos/'; | |
14 | ||
1553e15d | 15 | constructor (private http: Http, private _authService: AuthService) {} |
dc8bc31b | 16 | |
cf20596c | 17 | getVideos(pagination: Pagination, sort: SortField) { |
471bc22f | 18 | const params = this.createPaginationParams(pagination); |
cf20596c | 19 | |
aff038cd | 20 | if (sort) params.set('sort', sort); |
cf20596c | 21 | |
471bc22f | 22 | return this.http.get(this._baseVideoUrl, { search: params }) |
501bc6c2 C |
23 | .map(res => res.json()) |
24 | .map(this.extractVideos) | |
dc8bc31b C |
25 | .catch(this.handleError); |
26 | } | |
27 | ||
28 | getVideo(id: string) { | |
29 | return this.http.get(this._baseVideoUrl + id) | |
30 | .map(res => <Video> res.json()) | |
31 | .catch(this.handleError); | |
32 | } | |
33 | ||
34 | removeVideo(id: string) { | |
501bc6c2 C |
35 | const options = this._authService.getAuthRequestOptions(); |
36 | return this.http.delete(this._baseVideoUrl + id, options) | |
37 | .map(res => <number> res.status) | |
38 | .catch(this.handleError); | |
dc8bc31b C |
39 | } |
40 | ||
cf20596c | 41 | searchVideos(search: Search, pagination: Pagination, sort: SortField) { |
471bc22f | 42 | const params = this.createPaginationParams(pagination); |
cf20596c | 43 | |
471bc22f | 44 | if (search.field) params.set('field', search.field); |
aff038cd | 45 | if (sort) params.set('sort', sort); |
cf20596c | 46 | |
471bc22f | 47 | return this.http.get(this._baseVideoUrl + 'search/' + encodeURIComponent(search.value), { search: params }) |
501bc6c2 C |
48 | .map(res => res.json()) |
49 | .map(this.extractVideos) | |
98b01bac C |
50 | .catch(this.handleError); |
51 | } | |
52 | ||
32294074 C |
53 | private extractVideos (body: any) { |
54 | const videos_json = body.data; | |
55 | const totalVideos = body.total; | |
501bc6c2 | 56 | const videos = []; |
32294074 | 57 | for (const video_json of videos_json) { |
501bc6c2 C |
58 | videos.push(new Video(video_json)); |
59 | } | |
60 | ||
32294074 | 61 | return { videos, totalVideos }; |
501bc6c2 C |
62 | } |
63 | ||
dc8bc31b C |
64 | private handleError (error: Response) { |
65 | console.error(error); | |
66 | return Observable.throw(error.json().error || 'Server error'); | |
67 | } | |
32294074 C |
68 | |
69 | private createPaginationParams(pagination: Pagination) { | |
70 | const params = new URLSearchParams(); | |
71 | const start: number = (pagination.currentPage - 1) * pagination.itemsPerPage; | |
72 | const count: number = pagination.itemsPerPage; | |
73 | ||
74 | params.set('start', start.toString()); | |
75 | params.set('count', count.toString()); | |
76 | ||
77 | return params; | |
78 | } | |
dc8bc31b | 79 | } |