aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos/shared
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-09-14 11:57:49 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-09-14 11:57:49 +0200
commitd592e0a9b2931c7c9cbedb27fb8efc9aaacad9bb (patch)
tree549b14b842de296efed846a11b3681efe08cfa9e /client/src/app/videos/shared
parent91f6f169b1110eeae6ebf5c387f4204b0d07703c (diff)
downloadPeerTube-d592e0a9b2931c7c9cbedb27fb8efc9aaacad9bb.tar.gz
PeerTube-d592e0a9b2931c7c9cbedb27fb8efc9aaacad9bb.tar.zst
PeerTube-d592e0a9b2931c7c9cbedb27fb8efc9aaacad9bb.zip
Move to HttpClient and PrimeNG data table
Diffstat (limited to 'client/src/app/videos/shared')
-rw-r--r--client/src/app/videos/shared/index.ts1
-rw-r--r--client/src/app/videos/shared/video-pagination.model.ts5
-rw-r--r--client/src/app/videos/shared/video.model.ts4
-rw-r--r--client/src/app/videos/shared/video.service.ts111
4 files changed, 66 insertions, 55 deletions
diff --git a/client/src/app/videos/shared/index.ts b/client/src/app/videos/shared/index.ts
index 97d795321..8168e3bfd 100644
--- a/client/src/app/videos/shared/index.ts
+++ b/client/src/app/videos/shared/index.ts
@@ -1,3 +1,4 @@
1export * from './sort-field.type' 1export * from './sort-field.type'
2export * from './video.model' 2export * from './video.model'
3export * from './video.service' 3export * from './video.service'
4export * from './video-pagination.model'
diff --git a/client/src/app/videos/shared/video-pagination.model.ts b/client/src/app/videos/shared/video-pagination.model.ts
new file mode 100644
index 000000000..9e71769cb
--- /dev/null
+++ b/client/src/app/videos/shared/video-pagination.model.ts
@@ -0,0 +1,5 @@
1export interface VideoPagination {
2 currentPage: number
3 itemsPerPage: number
4 totalItems: number
5}
diff --git a/client/src/app/videos/shared/video.model.ts b/client/src/app/videos/shared/video.model.ts
index 1a413db9d..17f41059d 100644
--- a/client/src/app/videos/shared/video.model.ts
+++ b/client/src/app/videos/shared/video.model.ts
@@ -46,7 +46,7 @@ export class Video implements VideoServerModel {
46 46
47 constructor (hash: { 47 constructor (hash: {
48 author: string, 48 author: string,
49 createdAt: string, 49 createdAt: Date | string,
50 categoryLabel: string, 50 categoryLabel: string,
51 category: number, 51 category: number,
52 licenceLabel: string, 52 licenceLabel: string,
@@ -70,7 +70,7 @@ export class Video implements VideoServerModel {
70 files: VideoFile[] 70 files: VideoFile[]
71 }) { 71 }) {
72 this.author = hash.author 72 this.author = hash.author
73 this.createdAt = new Date(hash.createdAt) 73 this.createdAt = new Date(hash.createdAt.toString())
74 this.categoryLabel = hash.categoryLabel 74 this.categoryLabel = hash.categoryLabel
75 this.category = hash.category 75 this.category = hash.category
76 this.licenceLabel = hash.licenceLabel 76 this.licenceLabel = hash.licenceLabel
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}