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