]>
Commit | Line | Data |
---|---|---|
df98563e C |
1 | import { Injectable } from '@angular/core' |
2 | import { Http, Headers, RequestOptions } from '@angular/http' | |
3 | import { Observable } from 'rxjs/Observable' | |
4 | import 'rxjs/add/operator/catch' | |
5 | import 'rxjs/add/operator/map' | |
6 | ||
7 | import { Search } from '../../shared' | |
8 | import { SortField } from './sort-field.type' | |
df98563e | 9 | import { AuthService } from '../../core' |
d38b8281 C |
10 | import { |
11 | AuthHttp, | |
12 | RestExtractor, | |
13 | RestPagination, | |
14 | RestService, | |
15 | ResultList, | |
16 | UserService | |
df98563e C |
17 | } from '../../shared' |
18 | import { Video } from './video.model' | |
154898b0 | 19 | import { VideoRateType } from '../../../../../shared' |
dc8bc31b C |
20 | |
21 | @Injectable() | |
41a2aee3 | 22 | export class VideoService { |
df98563e | 23 | private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/' |
dc8bc31b | 24 | |
df98563e C |
25 | videoCategories: Array<{ id: number, label: string }> = [] |
26 | videoLicences: Array<{ id: number, label: string }> = [] | |
27 | videoLanguages: Array<{ id: number, label: string }> = [] | |
6e07c3de | 28 | |
df98563e | 29 | constructor ( |
4fd8aa32 | 30 | private authService: AuthService, |
bd5c83a8 | 31 | private authHttp: AuthHttp, |
de59c48f C |
32 | private http: Http, |
33 | private restExtractor: RestExtractor, | |
34 | private restService: RestService | |
4fd8aa32 C |
35 | ) {} |
36 | ||
df98563e | 37 | loadVideoCategories () { |
6e07c3de C |
38 | return this.http.get(VideoService.BASE_VIDEO_URL + 'categories') |
39 | .map(this.restExtractor.extractDataGet) | |
40 | .subscribe(data => { | |
41 | Object.keys(data).forEach(categoryKey => { | |
42 | this.videoCategories.push({ | |
df98563e | 43 | id: parseInt(categoryKey, 10), |
6e07c3de | 44 | label: data[categoryKey] |
df98563e C |
45 | }) |
46 | }) | |
47 | }) | |
6e07c3de C |
48 | } |
49 | ||
df98563e | 50 | loadVideoLicences () { |
d07137b9 C |
51 | return this.http.get(VideoService.BASE_VIDEO_URL + 'licences') |
52 | .map(this.restExtractor.extractDataGet) | |
53 | .subscribe(data => { | |
54 | Object.keys(data).forEach(licenceKey => { | |
55 | this.videoLicences.push({ | |
df98563e | 56 | id: parseInt(licenceKey, 10), |
d07137b9 | 57 | label: data[licenceKey] |
df98563e C |
58 | }) |
59 | }) | |
60 | }) | |
d07137b9 C |
61 | } |
62 | ||
df98563e | 63 | loadVideoLanguages () { |
db216afd C |
64 | return this.http.get(VideoService.BASE_VIDEO_URL + 'languages') |
65 | .map(this.restExtractor.extractDataGet) | |
66 | .subscribe(data => { | |
67 | Object.keys(data).forEach(languageKey => { | |
68 | this.videoLanguages.push({ | |
df98563e | 69 | id: parseInt(languageKey, 10), |
db216afd | 70 | label: data[languageKey] |
df98563e C |
71 | }) |
72 | }) | |
73 | }) | |
db216afd C |
74 | } |
75 | ||
df98563e | 76 | getVideo (id: string): Observable<Video> { |
4fd8aa32 | 77 | return this.http.get(VideoService.BASE_VIDEO_URL + id) |
de59c48f | 78 | .map(this.restExtractor.extractDataGet) |
df98563e C |
79 | .map(videoHash => new Video(videoHash)) |
80 | .catch((res) => this.restExtractor.handleError(res)) | |
4fd8aa32 | 81 | } |
dc8bc31b | 82 | |
df98563e C |
83 | updateVideo (video: Video) { |
84 | const language = video.language ? video.language : null | |
c24ac1c1 | 85 | |
d8e689b8 C |
86 | const body = { |
87 | name: video.name, | |
88 | category: video.category, | |
89 | licence: video.licence, | |
c24ac1c1 | 90 | language, |
d8e689b8 C |
91 | description: video.description, |
92 | tags: video.tags | |
df98563e | 93 | } |
c24ac1c1 | 94 | |
df98563e C |
95 | const headers = new Headers({ 'Content-Type': 'application/json' }) |
96 | const options = new RequestOptions({ headers: headers }) | |
d8e689b8 C |
97 | |
98 | return this.authHttp.put(`${VideoService.BASE_VIDEO_URL}/${video.id}`, body, options) | |
99 | .map(this.restExtractor.extractDataBool) | |
df98563e | 100 | .catch(this.restExtractor.handleError) |
d8e689b8 C |
101 | } |
102 | ||
df98563e C |
103 | getVideos (pagination: RestPagination, sort: SortField) { |
104 | const params = this.restService.buildRestGetParams(pagination, sort) | |
cf20596c | 105 | |
ccf6ed16 | 106 | return this.http.get(VideoService.BASE_VIDEO_URL, { search: params }) |
501bc6c2 C |
107 | .map(res => res.json()) |
108 | .map(this.extractVideos) | |
df98563e | 109 | .catch((res) => this.restExtractor.handleError(res)) |
dc8bc31b C |
110 | } |
111 | ||
df98563e | 112 | removeVideo (id: string) { |
bd5c83a8 | 113 | return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id) |
de59c48f | 114 | .map(this.restExtractor.extractDataBool) |
df98563e | 115 | .catch((res) => this.restExtractor.handleError(res)) |
dc8bc31b C |
116 | } |
117 | ||
df98563e C |
118 | searchVideos (search: Search, pagination: RestPagination, sort: SortField) { |
119 | const params = this.restService.buildRestGetParams(pagination, sort) | |
cf20596c | 120 | |
df98563e | 121 | if (search.field) params.set('field', search.field) |
cf20596c | 122 | |
ccf6ed16 | 123 | return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params }) |
de59c48f | 124 | .map(this.restExtractor.extractDataList) |
501bc6c2 | 125 | .map(this.extractVideos) |
df98563e | 126 | .catch((res) => this.restExtractor.handleError(res)) |
4fd8aa32 C |
127 | } |
128 | ||
df98563e C |
129 | reportVideo (id: string, reason: string) { |
130 | const url = VideoService.BASE_VIDEO_URL + id + '/abuse' | |
4f8c0eb0 C |
131 | const body = { |
132 | reason | |
df98563e | 133 | } |
4f8c0eb0 C |
134 | |
135 | return this.authHttp.post(url, body) | |
d38b8281 | 136 | .map(this.restExtractor.extractDataBool) |
df98563e | 137 | .catch((res) => this.restExtractor.handleError(res)) |
d38b8281 C |
138 | } |
139 | ||
df98563e C |
140 | setVideoLike (id: string) { |
141 | return this.setVideoRate(id, 'like') | |
d38b8281 C |
142 | } |
143 | ||
df98563e C |
144 | setVideoDislike (id: string) { |
145 | return this.setVideoRate(id, 'dislike') | |
d38b8281 C |
146 | } |
147 | ||
154898b0 | 148 | getUserVideoRating (id: string): Observable<VideoRateType> { |
df98563e | 149 | const url = UserService.BASE_USERS_URL + '/me/videos/' + id + '/rating' |
d38b8281 C |
150 | |
151 | return this.authHttp.get(url) | |
152 | .map(this.restExtractor.extractDataGet) | |
df98563e | 153 | .catch((res) => this.restExtractor.handleError(res)) |
d38b8281 C |
154 | } |
155 | ||
df98563e | 156 | blacklistVideo (id: string) { |
198b205c | 157 | return this.authHttp.post(VideoService.BASE_VIDEO_URL + id + '/blacklist', {}) |
ab683a8e | 158 | .map(this.restExtractor.extractDataBool) |
df98563e | 159 | .catch((res) => this.restExtractor.handleError(res)) |
198b205c GS |
160 | } |
161 | ||
154898b0 | 162 | private setVideoRate (id: string, rateType: VideoRateType) { |
df98563e | 163 | const url = VideoService.BASE_VIDEO_URL + id + '/rate' |
d38b8281 C |
164 | const body = { |
165 | rating: rateType | |
df98563e | 166 | } |
d38b8281 C |
167 | |
168 | return this.authHttp.put(url, body) | |
169 | .map(this.restExtractor.extractDataBool) | |
df98563e | 170 | .catch((res) => this.restExtractor.handleError(res)) |
4f8c0eb0 C |
171 | } |
172 | ||
df98563e C |
173 | private extractVideos (result: ResultList) { |
174 | const videosJson = result.data | |
175 | const totalVideos = result.total | |
176 | const videos = [] | |
de59c48f | 177 | for (const videoJson of videosJson) { |
df98563e | 178 | videos.push(new Video(videoJson)) |
501bc6c2 C |
179 | } |
180 | ||
df98563e | 181 | return { videos, totalVideos } |
501bc6c2 | 182 | } |
dc8bc31b | 183 | } |