]>
Commit | Line | Data |
---|---|---|
230809ef | 1 | import { Injectable } from '@angular/core'; |
de59c48f | 2 | import { Http } from '@angular/http'; |
5555f886 | 3 | import { Observable } from 'rxjs/Observable'; |
dc8bc31b | 4 | |
4a6995be | 5 | import { Search } from '../../shared'; |
41a2aee3 | 6 | import { SortField } from './sort-field.type'; |
de59c48f | 7 | import { AuthHttp, AuthService, RestExtractor, RestPagination, RestService, ResultList } from '../../shared'; |
41a2aee3 | 8 | import { Video } from './video.model'; |
dc8bc31b C |
9 | |
10 | @Injectable() | |
41a2aee3 | 11 | export class VideoService { |
ccf6ed16 | 12 | private static BASE_VIDEO_URL = '/api/v1/videos/'; |
dc8bc31b | 13 | |
4fd8aa32 C |
14 | constructor( |
15 | private authService: AuthService, | |
bd5c83a8 | 16 | private authHttp: AuthHttp, |
de59c48f C |
17 | private http: Http, |
18 | private restExtractor: RestExtractor, | |
19 | private restService: RestService | |
4fd8aa32 C |
20 | ) {} |
21 | ||
de59c48f | 22 | getVideo(id: string): Observable<Video> { |
4fd8aa32 | 23 | return this.http.get(VideoService.BASE_VIDEO_URL + id) |
de59c48f C |
24 | .map(this.restExtractor.extractDataGet) |
25 | .catch((res) => this.restExtractor.handleError(res)); | |
4fd8aa32 | 26 | } |
dc8bc31b | 27 | |
de59c48f C |
28 | getVideos(pagination: RestPagination, sort: SortField) { |
29 | const params = this.restService.buildRestGetParams(pagination, 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) | |
de59c48f | 34 | .catch((res) => this.restExtractor.handleError(res)); |
dc8bc31b C |
35 | } |
36 | ||
dc8bc31b | 37 | removeVideo(id: string) { |
bd5c83a8 | 38 | return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id) |
de59c48f C |
39 | .map(this.restExtractor.extractDataBool) |
40 | .catch((res) => this.restExtractor.handleError(res)); | |
dc8bc31b C |
41 | } |
42 | ||
de59c48f C |
43 | searchVideos(search: Search, pagination: RestPagination, sort: SortField) { |
44 | const params = this.restService.buildRestGetParams(pagination, sort); | |
cf20596c | 45 | |
471bc22f | 46 | if (search.field) params.set('field', search.field); |
cf20596c | 47 | |
ccf6ed16 | 48 | return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params }) |
de59c48f | 49 | .map(this.restExtractor.extractDataList) |
501bc6c2 | 50 | .map(this.extractVideos) |
de59c48f | 51 | .catch((res) => this.restExtractor.handleError(res)); |
4fd8aa32 C |
52 | } |
53 | ||
de59c48f C |
54 | private extractVideos(result: ResultList) { |
55 | const videosJson = result.data; | |
56 | const totalVideos = result.total; | |
501bc6c2 | 57 | const videos = []; |
de59c48f C |
58 | for (const videoJson of videosJson) { |
59 | videos.push(new Video(videoJson)); | |
501bc6c2 C |
60 | } |
61 | ||
32294074 | 62 | return { videos, totalVideos }; |
501bc6c2 | 63 | } |
dc8bc31b | 64 | } |