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