]>
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 | 24 | .map(this.restExtractor.extractDataGet) |
d1992b93 | 25 | .map(video_hash => new Video(video_hash)) |
de59c48f | 26 | .catch((res) => this.restExtractor.handleError(res)); |
4fd8aa32 | 27 | } |
dc8bc31b | 28 | |
de59c48f C |
29 | getVideos(pagination: RestPagination, sort: SortField) { |
30 | const params = this.restService.buildRestGetParams(pagination, sort); | |
cf20596c | 31 | |
ccf6ed16 | 32 | return this.http.get(VideoService.BASE_VIDEO_URL, { search: params }) |
501bc6c2 C |
33 | .map(res => res.json()) |
34 | .map(this.extractVideos) | |
de59c48f | 35 | .catch((res) => this.restExtractor.handleError(res)); |
dc8bc31b C |
36 | } |
37 | ||
dc8bc31b | 38 | removeVideo(id: string) { |
bd5c83a8 | 39 | return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id) |
de59c48f C |
40 | .map(this.restExtractor.extractDataBool) |
41 | .catch((res) => this.restExtractor.handleError(res)); | |
dc8bc31b C |
42 | } |
43 | ||
de59c48f C |
44 | searchVideos(search: Search, pagination: RestPagination, sort: SortField) { |
45 | const params = this.restService.buildRestGetParams(pagination, sort); | |
cf20596c | 46 | |
471bc22f | 47 | if (search.field) params.set('field', search.field); |
cf20596c | 48 | |
ccf6ed16 | 49 | return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params }) |
de59c48f | 50 | .map(this.restExtractor.extractDataList) |
501bc6c2 | 51 | .map(this.extractVideos) |
de59c48f | 52 | .catch((res) => this.restExtractor.handleError(res)); |
4fd8aa32 C |
53 | } |
54 | ||
de59c48f C |
55 | private extractVideos(result: ResultList) { |
56 | const videosJson = result.data; | |
57 | const totalVideos = result.total; | |
501bc6c2 | 58 | const videos = []; |
de59c48f C |
59 | for (const videoJson of videosJson) { |
60 | videos.push(new Video(videoJson)); | |
501bc6c2 C |
61 | } |
62 | ||
32294074 | 63 | return { videos, totalVideos }; |
501bc6c2 | 64 | } |
dc8bc31b | 65 | } |