diff options
Diffstat (limited to 'client/app/videos/shared/video.service.ts')
-rw-r--r-- | client/app/videos/shared/video.service.ts | 82 |
1 files changed, 0 insertions, 82 deletions
diff --git a/client/app/videos/shared/video.service.ts b/client/app/videos/shared/video.service.ts deleted file mode 100644 index a786b2ab2..000000000 --- a/client/app/videos/shared/video.service.ts +++ /dev/null | |||
@@ -1,82 +0,0 @@ | |||
1 | import { Injectable } from '@angular/core'; | ||
2 | import { Http, Response, URLSearchParams } from '@angular/http'; | ||
3 | import { Observable } from 'rxjs/Rx'; | ||
4 | |||
5 | import { Pagination } from './pagination.model'; | ||
6 | import { Search } from '../../shared/index'; | ||
7 | import { SortField } from './sort-field.type'; | ||
8 | import { AuthService } from '../../shared/index'; | ||
9 | import { Video } from './video.model'; | ||
10 | |||
11 | @Injectable() | ||
12 | export class VideoService { | ||
13 | private static BASE_VIDEO_URL = '/api/v1/videos/'; | ||
14 | |||
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 | } | ||
25 | |||
26 | getVideos(pagination: Pagination, sort: SortField) { | ||
27 | const params = this.createPaginationParams(pagination); | ||
28 | |||
29 | if (sort) params.set('sort', sort); | ||
30 | |||
31 | return this.http.get(VideoService.BASE_VIDEO_URL, { search: params }) | ||
32 | .map(res => res.json()) | ||
33 | .map(this.extractVideos) | ||
34 | .catch(this.handleError); | ||
35 | } | ||
36 | |||
37 | removeVideo(id: string) { | ||
38 | const options = this.authService.getAuthRequestOptions(); | ||
39 | return this.http.delete(VideoService.BASE_VIDEO_URL + id, options) | ||
40 | .map(res => <number> res.status) | ||
41 | .catch(this.handleError); | ||
42 | } | ||
43 | |||
44 | searchVideos(search: Search, pagination: Pagination, sort: SortField) { | ||
45 | const params = this.createPaginationParams(pagination); | ||
46 | |||
47 | if (search.field) params.set('field', search.field); | ||
48 | if (sort) params.set('sort', sort); | ||
49 | |||
50 | return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params }) | ||
51 | .map(res => res.json()) | ||
52 | .map(this.extractVideos) | ||
53 | .catch(this.handleError); | ||
54 | } | ||
55 | |||
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 | |||
67 | private extractVideos(body: any) { | ||
68 | const videos_json = body.data; | ||
69 | const totalVideos = body.total; | ||
70 | const videos = []; | ||
71 | for (const video_json of videos_json) { | ||
72 | videos.push(new Video(video_json)); | ||
73 | } | ||
74 | |||
75 | return { videos, totalVideos }; | ||
76 | } | ||
77 | |||
78 | private handleError(error: Response) { | ||
79 | console.error(error); | ||
80 | return Observable.throw(error.json().error || 'Server error'); | ||
81 | } | ||
82 | } | ||