diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-09-14 11:57:49 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-09-14 11:57:49 +0200 |
commit | d592e0a9b2931c7c9cbedb27fb8efc9aaacad9bb (patch) | |
tree | 549b14b842de296efed846a11b3681efe08cfa9e /client/src/app/videos | |
parent | 91f6f169b1110eeae6ebf5c387f4204b0d07703c (diff) | |
download | PeerTube-d592e0a9b2931c7c9cbedb27fb8efc9aaacad9bb.tar.gz PeerTube-d592e0a9b2931c7c9cbedb27fb8efc9aaacad9bb.tar.zst PeerTube-d592e0a9b2931c7c9cbedb27fb8efc9aaacad9bb.zip |
Move to HttpClient and PrimeNG data table
Diffstat (limited to 'client/src/app/videos')
-rw-r--r-- | client/src/app/videos/shared/index.ts | 1 | ||||
-rw-r--r-- | client/src/app/videos/shared/video-pagination.model.ts | 5 | ||||
-rw-r--r-- | client/src/app/videos/shared/video.model.ts | 4 | ||||
-rw-r--r-- | client/src/app/videos/shared/video.service.ts | 111 | ||||
-rw-r--r-- | client/src/app/videos/video-list/video-list.component.ts | 11 |
5 files changed, 72 insertions, 60 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 @@ | |||
1 | export * from './sort-field.type' | 1 | export * from './sort-field.type' |
2 | export * from './video.model' | 2 | export * from './video.model' |
3 | export * from './video.service' | 3 | export * from './video.service' |
4 | export * 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 @@ | |||
1 | export 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 @@ | |||
1 | import { Injectable } from '@angular/core' | 1 | import { Injectable } from '@angular/core' |
2 | import { Http, Headers, RequestOptions } from '@angular/http' | ||
3 | import { Observable } from 'rxjs/Observable' | 2 | import { Observable } from 'rxjs/Observable' |
4 | import 'rxjs/add/operator/catch' | 3 | import 'rxjs/add/operator/catch' |
5 | import 'rxjs/add/operator/map' | 4 | import 'rxjs/add/operator/map' |
5 | import { HttpClient, HttpParams } from '@angular/common/http' | ||
6 | 6 | ||
7 | import { Search } from '../../shared' | 7 | import { Search } from '../../shared' |
8 | import { SortField } from './sort-field.type' | 8 | import { SortField } from './sort-field.type' |
9 | import { AuthService } from '../../core' | ||
10 | import { | 9 | import { |
11 | AuthHttp, | ||
12 | RestExtractor, | 10 | RestExtractor, |
13 | RestPagination, | ||
14 | RestService, | 11 | RestService, |
15 | ResultList, | ||
16 | UserService | 12 | UserService |
17 | } from '../../shared' | 13 | } from '../../shared' |
18 | import { Video } from './video.model' | 14 | import { Video } from './video.model' |
15 | import { VideoPagination } from './video-pagination.model' | ||
19 | import { | 16 | import { |
20 | UserVideoRate, | 17 | UserVideoRate, |
21 | VideoRateType, | 18 | VideoRateType, |
22 | VideoUpdate, | 19 | VideoUpdate, |
23 | VideoAbuseCreate, | 20 | VideoAbuseCreate, |
24 | UserVideoRateUpdate | 21 | UserVideoRateUpdate, |
22 | Video as VideoServerModel, | ||
23 | ResultList | ||
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 | } |
diff --git a/client/src/app/videos/video-list/video-list.component.ts b/client/src/app/videos/video-list/video-list.component.ts index 4ac539960..590632063 100644 --- a/client/src/app/videos/video-list/video-list.component.ts +++ b/client/src/app/videos/video-list/video-list.component.ts | |||
@@ -8,11 +8,12 @@ import { NotificationsService } from 'angular2-notifications' | |||
8 | import { | 8 | import { |
9 | SortField, | 9 | SortField, |
10 | Video, | 10 | Video, |
11 | VideoService | 11 | VideoService, |
12 | VideoPagination | ||
12 | } from '../shared' | 13 | } from '../shared' |
13 | import { AuthService, AuthUser } from '../../core' | 14 | import { AuthService, AuthUser } from '../../core' |
14 | import { RestPagination, Search, SearchField } from '../../shared' | 15 | import { Search, SearchField, SearchService } from '../../shared' |
15 | import { SearchService } from '../../shared' | 16 | import { } from '../../shared' |
16 | 17 | ||
17 | @Component({ | 18 | @Component({ |
18 | selector: 'my-videos-list', | 19 | selector: 'my-videos-list', |
@@ -21,7 +22,7 @@ import { SearchService } from '../../shared' | |||
21 | }) | 22 | }) |
22 | export class VideoListComponent implements OnInit, OnDestroy { | 23 | export class VideoListComponent implements OnInit, OnDestroy { |
23 | loading: BehaviorSubject<boolean> = new BehaviorSubject(false) | 24 | loading: BehaviorSubject<boolean> = new BehaviorSubject(false) |
24 | pagination: RestPagination = { | 25 | pagination: VideoPagination = { |
25 | currentPage: 1, | 26 | currentPage: 1, |
26 | itemsPerPage: 25, | 27 | itemsPerPage: 25, |
27 | totalItems: null | 28 | totalItems: null |
@@ -152,6 +153,6 @@ export class VideoListComponent implements OnInit, OnDestroy { | |||
152 | 153 | ||
153 | private navigateToNewParams () { | 154 | private navigateToNewParams () { |
154 | const routeParams = this.buildRouteParams() | 155 | const routeParams = this.buildRouteParams() |
155 | this.router.navigate(['/videos/list', routeParams]) | 156 | this.router.navigate([ '/videos/list', routeParams ]) |
156 | } | 157 | } |
157 | } | 158 | } |