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