]>
Commit | Line | Data |
---|---|---|
df98563e | 1 | import { Injectable } from '@angular/core' |
df98563e | 2 | import { Observable } from 'rxjs/Observable' |
7a8032bb | 3 | import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http' |
df98563e C |
4 | import 'rxjs/add/operator/catch' |
5 | import 'rxjs/add/operator/map' | |
6 | ||
df98563e | 7 | import { SortField } from './sort-field.type' |
d38b8281 | 8 | import { |
d38b8281 | 9 | RestExtractor, |
d38b8281 | 10 | RestService, |
7a8032bb C |
11 | UserService, |
12 | Search | |
df98563e C |
13 | } from '../../shared' |
14 | import { Video } from './video.model' | |
404b54e1 C |
15 | import { VideoDetails } from './video-details.model' |
16 | import { VideoEdit } from './video-edit.model' | |
d592e0a9 | 17 | import { VideoPagination } from './video-pagination.model' |
4771e000 | 18 | import { |
bfb3a98f C |
19 | UserVideoRate, |
20 | VideoRateType, | |
21 | VideoUpdate, | |
bfb3a98f C |
22 | UserVideoRateUpdate, |
23 | Video as VideoServerModel, | |
404b54e1 | 24 | VideoDetails as VideoDetailsServerModel, |
bfb3a98f | 25 | ResultList |
4771e000 | 26 | } from '../../../../../shared' |
dc8bc31b C |
27 | |
28 | @Injectable() | |
41a2aee3 | 29 | export class VideoService { |
df98563e | 30 | private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/' |
dc8bc31b | 31 | |
df98563e | 32 | constructor ( |
d592e0a9 | 33 | private authHttp: HttpClient, |
de59c48f C |
34 | private restExtractor: RestExtractor, |
35 | private restService: RestService | |
4fd8aa32 C |
36 | ) {} |
37 | ||
9d9597df | 38 | getVideo (uuid: string): Observable<VideoDetails> { |
404b54e1 C |
39 | return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid) |
40 | .map(videoHash => new VideoDetails(videoHash)) | |
d592e0a9 | 41 | .catch((res) => this.restExtractor.handleError(res)) |
4fd8aa32 | 42 | } |
dc8bc31b | 43 | |
1f3e9fec C |
44 | viewVideo (uuid: string): Observable<VideoDetails> { |
45 | return this.authHttp.post(VideoService.BASE_VIDEO_URL + uuid + '/views', {}) | |
46 | .map(this.restExtractor.extractDataBool) | |
47 | .catch(this.restExtractor.handleError) | |
48 | } | |
49 | ||
404b54e1 | 50 | updateVideo (video: VideoEdit) { |
df98563e | 51 | const language = video.language ? video.language : null |
c24ac1c1 | 52 | |
4771e000 | 53 | const body: VideoUpdate = { |
d8e689b8 C |
54 | name: video.name, |
55 | category: video.category, | |
56 | licence: video.licence, | |
c24ac1c1 | 57 | language, |
d8e689b8 | 58 | description: video.description, |
fd45e8f4 | 59 | privacy: video.privacy, |
4771e000 C |
60 | tags: video.tags, |
61 | nsfw: video.nsfw | |
df98563e | 62 | } |
c24ac1c1 | 63 | |
334ddfa4 | 64 | return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, body) |
d8e689b8 | 65 | .map(this.restExtractor.extractDataBool) |
df98563e | 66 | .catch(this.restExtractor.handleError) |
d8e689b8 C |
67 | } |
68 | ||
db7af09b | 69 | uploadVideo (video: FormData) { |
334ddfa4 | 70 | const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true }) |
bfb3a98f | 71 | |
fd45e8f4 C |
72 | return this.authHttp |
73 | .request(req) | |
74 | .catch(this.restExtractor.handleError) | |
bfb3a98f C |
75 | } |
76 | ||
fd45e8f4 | 77 | getMyVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> { |
d592e0a9 | 78 | const pagination = this.videoPaginationToRestPagination(videoPagination) |
cf20596c | 79 | |
d592e0a9 C |
80 | let params = new HttpParams() |
81 | params = this.restService.addRestGetParams(params, pagination, sort) | |
dc8bc31b | 82 | |
fd45e8f4 C |
83 | return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params }) |
84 | .map(this.extractVideos) | |
85 | .catch((res) => this.restExtractor.handleError(res)) | |
dc8bc31b C |
86 | } |
87 | ||
fd45e8f4 C |
88 | getVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> { |
89 | const pagination = this.videoPaginationToRestPagination(videoPagination) | |
90 | ||
91 | let params = new HttpParams() | |
92 | params = this.restService.addRestGetParams(params, pagination, sort) | |
93 | ||
94 | return this.authHttp | |
95 | .get(VideoService.BASE_VIDEO_URL, { params }) | |
96 | .map(this.extractVideos) | |
97 | .catch((res) => this.restExtractor.handleError(res)) | |
98 | } | |
99 | ||
100 | searchVideos (search: Search, videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> { | |
d592e0a9 C |
101 | const url = VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value) |
102 | ||
103 | const pagination = this.videoPaginationToRestPagination(videoPagination) | |
104 | ||
105 | let params = new HttpParams() | |
106 | params = this.restService.addRestGetParams(params, pagination, sort) | |
cf20596c | 107 | |
df98563e | 108 | if (search.field) params.set('field', search.field) |
cf20596c | 109 | |
fd45e8f4 C |
110 | return this.authHttp |
111 | .get<ResultList<VideoServerModel>>(url, { params }) | |
112 | .map(this.extractVideos) | |
113 | .catch((res) => this.restExtractor.handleError(res)) | |
d592e0a9 C |
114 | } |
115 | ||
116 | removeVideo (id: number) { | |
fd45e8f4 C |
117 | return this.authHttp |
118 | .delete(VideoService.BASE_VIDEO_URL + id) | |
119 | .map(this.restExtractor.extractDataBool) | |
120 | .catch((res) => this.restExtractor.handleError(res)) | |
4fd8aa32 C |
121 | } |
122 | ||
2de96f4d C |
123 | loadCompleteDescription (descriptionPath: string) { |
124 | return this.authHttp | |
125 | .get(API_URL + descriptionPath) | |
126 | .map(res => res['description']) | |
127 | .catch((res) => this.restExtractor.handleError(res)) | |
d38b8281 C |
128 | } |
129 | ||
0a6658fd | 130 | setVideoLike (id: number) { |
df98563e | 131 | return this.setVideoRate(id, 'like') |
d38b8281 C |
132 | } |
133 | ||
0a6658fd | 134 | setVideoDislike (id: number) { |
df98563e | 135 | return this.setVideoRate(id, 'dislike') |
d38b8281 C |
136 | } |
137 | ||
0a6658fd | 138 | getUserVideoRating (id: number): Observable<UserVideoRate> { |
334ddfa4 | 139 | const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating' |
d38b8281 | 140 | |
fd45e8f4 C |
141 | return this.authHttp |
142 | .get(url) | |
143 | .catch(res => this.restExtractor.handleError(res)) | |
d38b8281 C |
144 | } |
145 | ||
d592e0a9 C |
146 | private videoPaginationToRestPagination (videoPagination: VideoPagination) { |
147 | const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage | |
148 | const count: number = videoPagination.itemsPerPage | |
149 | ||
150 | return { start, count } | |
198b205c GS |
151 | } |
152 | ||
0a6658fd | 153 | private setVideoRate (id: number, rateType: VideoRateType) { |
df98563e | 154 | const url = VideoService.BASE_VIDEO_URL + id + '/rate' |
4771e000 | 155 | const body: UserVideoRateUpdate = { |
d38b8281 | 156 | rating: rateType |
df98563e | 157 | } |
d38b8281 | 158 | |
fd45e8f4 C |
159 | return this.authHttp |
160 | .put(url, body) | |
161 | .map(this.restExtractor.extractDataBool) | |
162 | .catch(res => this.restExtractor.handleError(res)) | |
4f8c0eb0 C |
163 | } |
164 | ||
d592e0a9 | 165 | private extractVideos (result: ResultList<VideoServerModel>) { |
df98563e C |
166 | const videosJson = result.data |
167 | const totalVideos = result.total | |
168 | const videos = [] | |
d592e0a9 | 169 | |
de59c48f | 170 | for (const videoJson of videosJson) { |
df98563e | 171 | videos.push(new Video(videoJson)) |
501bc6c2 C |
172 | } |
173 | ||
df98563e | 174 | return { videos, totalVideos } |
501bc6c2 | 175 | } |
dc8bc31b | 176 | } |