aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos/shared/video.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/videos/shared/video.service.ts')
-rw-r--r--client/src/app/videos/shared/video.service.ts111
1 files changed, 58 insertions, 53 deletions
diff --git a/client/src/app/videos/shared/video.service.ts b/client/src/app/videos/shared/video.service.ts
index 67091a8d8..b6d2a0666 100644
--- a/client/src/app/videos/shared/video.service.ts
+++ b/client/src/app/videos/shared/video.service.ts
@@ -1,27 +1,26 @@
1import { Injectable } from '@angular/core' 1import { Injectable } from '@angular/core'
2import { Http, Headers, RequestOptions } from '@angular/http'
3import { Observable } from 'rxjs/Observable' 2import { Observable } from 'rxjs/Observable'
4import 'rxjs/add/operator/catch' 3import 'rxjs/add/operator/catch'
5import 'rxjs/add/operator/map' 4import 'rxjs/add/operator/map'
5import { HttpClient, HttpParams } from '@angular/common/http'
6 6
7import { Search } from '../../shared' 7import { Search } from '../../shared'
8import { SortField } from './sort-field.type' 8import { SortField } from './sort-field.type'
9import { AuthService } from '../../core'
10import { 9import {
11 AuthHttp,
12 RestExtractor, 10 RestExtractor,
13 RestPagination,
14 RestService, 11 RestService,
15 ResultList,
16 UserService 12 UserService
17} from '../../shared' 13} from '../../shared'
18import { Video } from './video.model' 14import { Video } from './video.model'
15import { VideoPagination } from './video-pagination.model'
19import { 16import {
20 UserVideoRate, 17UserVideoRate,
21 VideoRateType, 18VideoRateType,
22 VideoUpdate, 19VideoUpdate,
23 VideoAbuseCreate, 20VideoAbuseCreate,
24 UserVideoRateUpdate 21UserVideoRateUpdate,
22Video as VideoServerModel,
23ResultList
25} from '../../../../../shared' 24} from '../../../../../shared'
26 25
27@Injectable() 26@Injectable()
@@ -33,9 +32,7 @@ export class VideoService {
33 videoLanguages: Array<{ id: number, label: string }> = [] 32 videoLanguages: Array<{ id: number, label: string }> = []
34 33
35 constructor ( 34 constructor (
36 private authService: AuthService, 35 private authHttp: HttpClient,
37 private authHttp: AuthHttp,
38 private http: Http,
39 private restExtractor: RestExtractor, 36 private restExtractor: RestExtractor,
40 private restService: RestService 37 private restService: RestService
41 ) {} 38 ) {}
@@ -52,11 +49,10 @@ export class VideoService {
52 return this.loadVideoAttributeEnum('languages', this.videoLanguages) 49 return this.loadVideoAttributeEnum('languages', this.videoLanguages)
53 } 50 }
54 51
55 getVideo (uuid: string): Observable<Video> { 52 getVideo (uuid: string) {
56 return this.http.get(VideoService.BASE_VIDEO_URL + uuid) 53 return this.authHttp.get<VideoServerModel>(VideoService.BASE_VIDEO_URL + uuid)
57 .map(this.restExtractor.extractDataGet) 54 .map(videoHash => new Video(videoHash))
58 .map(videoHash => new Video(videoHash)) 55 .catch((res) => this.restExtractor.handleError(res))
59 .catch((res) => this.restExtractor.handleError(res))
60 } 56 }
61 57
62 updateVideo (video: Video) { 58 updateVideo (video: Video) {
@@ -72,38 +68,41 @@ export class VideoService {
72 nsfw: video.nsfw 68 nsfw: video.nsfw
73 } 69 }
74 70
75 const headers = new Headers({ 'Content-Type': 'application/json' }) 71 return this.authHttp.put(`${VideoService.BASE_VIDEO_URL}/${video.id}`, body)
76 const options = new RequestOptions({ headers: headers })
77
78 return this.authHttp.put(`${VideoService.BASE_VIDEO_URL}/${video.id}`, body, options)
79 .map(this.restExtractor.extractDataBool) 72 .map(this.restExtractor.extractDataBool)
80 .catch(this.restExtractor.handleError) 73 .catch(this.restExtractor.handleError)
81 } 74 }
82 75
83 getVideos (pagination: RestPagination, sort: SortField) { 76 getVideos (videoPagination: VideoPagination, sort: SortField) {
84 const params = this.restService.buildRestGetParams(pagination, sort) 77 const pagination = this.videoPaginationToRestPagination(videoPagination)
85 78
86 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params }) 79 let params = new HttpParams()
87 .map(res => res.json()) 80 params = this.restService.addRestGetParams(params, pagination, sort)
88 .map(this.extractVideos)
89 .catch((res) => this.restExtractor.handleError(res))
90 }
91 81
92 removeVideo (id: number) { 82 return this.authHttp.get(VideoService.BASE_VIDEO_URL, { params })
93 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id) 83 .map(this.extractVideos)
94 .map(this.restExtractor.extractDataBool)
95 .catch((res) => this.restExtractor.handleError(res)) 84 .catch((res) => this.restExtractor.handleError(res))
96 } 85 }
97 86
98 searchVideos (search: Search, pagination: RestPagination, sort: SortField) { 87 searchVideos (search: Search, videoPagination: VideoPagination, sort: SortField) {
99 const params = this.restService.buildRestGetParams(pagination, sort) 88 const url = VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value)
89
90 const pagination = this.videoPaginationToRestPagination(videoPagination)
91
92 let params = new HttpParams()
93 params = this.restService.addRestGetParams(params, pagination, sort)
100 94
101 if (search.field) params.set('field', search.field) 95 if (search.field) params.set('field', search.field)
102 96
103 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params }) 97 return this.authHttp.get<ResultList<VideoServerModel>>(url, { params })
104 .map(this.restExtractor.extractDataList) 98 .map(this.extractVideos)
105 .map(this.extractVideos) 99 .catch((res) => this.restExtractor.handleError(res))
106 .catch((res) => this.restExtractor.handleError(res)) 100 }
101
102 removeVideo (id: number) {
103 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
104 .map(this.restExtractor.extractDataBool)
105 .catch((res) => this.restExtractor.handleError(res))
107 } 106 }
108 107
109 reportVideo (id: number, reason: string) { 108 reportVideo (id: number, reason: string) {
@@ -114,7 +113,7 @@ export class VideoService {
114 113
115 return this.authHttp.post(url, body) 114 return this.authHttp.post(url, body)
116 .map(this.restExtractor.extractDataBool) 115 .map(this.restExtractor.extractDataBool)
117 .catch((res) => this.restExtractor.handleError(res)) 116 .catch(res => this.restExtractor.handleError(res))
118 } 117 }
119 118
120 setVideoLike (id: number) { 119 setVideoLike (id: number) {
@@ -129,14 +128,20 @@ export class VideoService {
129 const url = UserService.BASE_USERS_URL + '/me/videos/' + id + '/rating' 128 const url = UserService.BASE_USERS_URL + '/me/videos/' + id + '/rating'
130 129
131 return this.authHttp.get(url) 130 return this.authHttp.get(url)
132 .map(this.restExtractor.extractDataGet) 131 .catch(res => this.restExtractor.handleError(res))
133 .catch((res) => this.restExtractor.handleError(res))
134 } 132 }
135 133
136 blacklistVideo (id: number) { 134 blacklistVideo (id: number) {
137 return this.authHttp.post(VideoService.BASE_VIDEO_URL + id + '/blacklist', {}) 135 return this.authHttp.post(VideoService.BASE_VIDEO_URL + id + '/blacklist', {})
138 .map(this.restExtractor.extractDataBool) 136 .map(this.restExtractor.extractDataBool)
139 .catch((res) => this.restExtractor.handleError(res)) 137 .catch(res => this.restExtractor.handleError(res))
138 }
139
140 private videoPaginationToRestPagination (videoPagination: VideoPagination) {
141 const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage
142 const count: number = videoPagination.itemsPerPage
143
144 return { start, count }
140 } 145 }
141 146
142 private setVideoRate (id: number, rateType: VideoRateType) { 147 private setVideoRate (id: number, rateType: VideoRateType) {
@@ -147,13 +152,14 @@ export class VideoService {
147 152
148 return this.authHttp.put(url, body) 153 return this.authHttp.put(url, body)
149 .map(this.restExtractor.extractDataBool) 154 .map(this.restExtractor.extractDataBool)
150 .catch((res) => this.restExtractor.handleError(res)) 155 .catch(res => this.restExtractor.handleError(res))
151 } 156 }
152 157
153 private extractVideos (result: ResultList) { 158 private extractVideos (result: ResultList<VideoServerModel>) {
154 const videosJson = result.data 159 const videosJson = result.data
155 const totalVideos = result.total 160 const totalVideos = result.total
156 const videos = [] 161 const videos = []
162
157 for (const videoJson of videosJson) { 163 for (const videoJson of videosJson) {
158 videos.push(new Video(videoJson)) 164 videos.push(new Video(videoJson))
159 } 165 }
@@ -162,15 +168,14 @@ export class VideoService {
162 } 168 }
163 169
164 private loadVideoAttributeEnum (attributeName: 'categories' | 'licences' | 'languages', hashToPopulate: { id: number, label: string }[]) { 170 private loadVideoAttributeEnum (attributeName: 'categories' | 'licences' | 'languages', hashToPopulate: { id: number, label: string }[]) {
165 return this.http.get(VideoService.BASE_VIDEO_URL + attributeName) 171 return this.authHttp.get(VideoService.BASE_VIDEO_URL + attributeName)
166 .map(this.restExtractor.extractDataGet) 172 .subscribe(data => {
167 .subscribe(data => { 173 Object.keys(data).forEach(dataKey => {
168 Object.keys(data).forEach(dataKey => { 174 hashToPopulate.push({
169 hashToPopulate.push({ 175 id: parseInt(dataKey, 10),
170 id: parseInt(dataKey, 10), 176 label: data[dataKey]
171 label: data[dataKey] 177 })
178 })
172 }) 179 })
173 })
174 })
175 } 180 }
176} 181}