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