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