diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-08-23 16:54:21 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-08-23 16:54:21 +0200 |
commit | de59c48f5f317018e3f746bbe4a7b7efe00109f2 (patch) | |
tree | bc3d007c5aaed8dc72119763f3b1731c5777f218 /client/src | |
parent | def16d33d19153c6583fa8a30634760b3d64d34c (diff) | |
download | PeerTube-de59c48f5f317018e3f746bbe4a7b7efe00109f2.tar.gz PeerTube-de59c48f5f317018e3f746bbe4a7b7efe00109f2.tar.zst PeerTube-de59c48f5f317018e3f746bbe4a7b7efe00109f2.zip |
Client: centralize http res extraction in a service
Diffstat (limited to 'client/src')
-rw-r--r-- | client/src/app/account/account.service.ts | 12 | ||||
-rw-r--r-- | client/src/app/admin/friends/shared/friend.service.ts | 22 | ||||
-rw-r--r-- | client/src/app/admin/users/shared/user.service.ts | 28 | ||||
-rw-r--r-- | client/src/app/app.component.ts | 4 | ||||
-rw-r--r-- | client/src/app/login/login.component.ts | 6 | ||||
-rw-r--r-- | client/src/app/shared/auth/auth.service.ts | 22 | ||||
-rw-r--r-- | client/src/app/shared/index.ts | 1 | ||||
-rw-r--r-- | client/src/app/shared/rest/index.ts | 3 | ||||
-rw-r--r-- | client/src/app/shared/rest/rest-extractor.service.ts | 46 | ||||
-rw-r--r-- | client/src/app/shared/rest/rest-pagination.ts (renamed from client/src/app/videos/shared/pagination.model.ts) | 4 | ||||
-rw-r--r-- | client/src/app/shared/rest/rest.service.ts | 27 | ||||
-rw-r--r-- | client/src/app/videos/shared/index.ts | 1 | ||||
-rw-r--r-- | client/src/app/videos/shared/video.service.ts | 62 | ||||
-rw-r--r-- | client/src/app/videos/video-list/video-list.component.ts | 5 | ||||
-rw-r--r-- | client/src/main.ts | 3 |
15 files changed, 150 insertions, 96 deletions
diff --git a/client/src/app/account/account.service.ts b/client/src/app/account/account.service.ts index 19b4e0624..355bcef74 100644 --- a/client/src/app/account/account.service.ts +++ b/client/src/app/account/account.service.ts | |||
@@ -1,12 +1,16 @@ | |||
1 | import { Injectable } from '@angular/core'; | 1 | import { Injectable } from '@angular/core'; |
2 | 2 | ||
3 | import { AuthHttp, AuthService } from '../shared'; | 3 | import { AuthHttp, AuthService, RestExtractor } from '../shared'; |
4 | 4 | ||
5 | @Injectable() | 5 | @Injectable() |
6 | export class AccountService { | 6 | export class AccountService { |
7 | private static BASE_USERS_URL = '/api/v1/users/'; | 7 | private static BASE_USERS_URL = '/api/v1/users/'; |
8 | 8 | ||
9 | constructor(private authHttp: AuthHttp, private authService: AuthService) { } | 9 | constructor( |
10 | private authHttp: AuthHttp, | ||
11 | private authService: AuthService, | ||
12 | private restExtractor: RestExtractor | ||
13 | ) {} | ||
10 | 14 | ||
11 | changePassword(newPassword: string) { | 15 | changePassword(newPassword: string) { |
12 | const url = AccountService.BASE_USERS_URL + this.authService.getUser().id; | 16 | const url = AccountService.BASE_USERS_URL + this.authService.getUser().id; |
@@ -14,6 +18,8 @@ export class AccountService { | |||
14 | password: newPassword | 18 | password: newPassword |
15 | }; | 19 | }; |
16 | 20 | ||
17 | return this.authHttp.put(url, body); | 21 | return this.authHttp.put(url, body) |
22 | .map(this.restExtractor.extractDataBool) | ||
23 | .catch((res) => this.restExtractor.handleError(res)); | ||
18 | } | 24 | } |
19 | } | 25 | } |
diff --git a/client/src/app/admin/friends/shared/friend.service.ts b/client/src/app/admin/friends/shared/friend.service.ts index e4e680c29..75826fc17 100644 --- a/client/src/app/admin/friends/shared/friend.service.ts +++ b/client/src/app/admin/friends/shared/friend.service.ts | |||
@@ -1,9 +1,8 @@ | |||
1 | import { Injectable } from '@angular/core'; | 1 | import { Injectable } from '@angular/core'; |
2 | import { Response } from '@angular/http'; | ||
3 | import { Observable } from 'rxjs/Observable'; | 2 | import { Observable } from 'rxjs/Observable'; |
4 | 3 | ||
5 | import { Friend } from './friend.model'; | 4 | import { Friend } from './friend.model'; |
6 | import { AuthHttp, AuthService } from '../../../shared'; | 5 | import { AuthHttp, RestExtractor } from '../../../shared'; |
7 | 6 | ||
8 | @Injectable() | 7 | @Injectable() |
9 | export class FriendService { | 8 | export class FriendService { |
@@ -11,13 +10,15 @@ export class FriendService { | |||
11 | 10 | ||
12 | constructor ( | 11 | constructor ( |
13 | private authHttp: AuthHttp, | 12 | private authHttp: AuthHttp, |
14 | private authService: AuthService | 13 | private restExtractor: RestExtractor |
15 | ) {} | 14 | ) {} |
16 | 15 | ||
17 | getFriends(): Observable<Friend[]> { | 16 | getFriends(): Observable<Friend[]> { |
18 | return this.authHttp.get(FriendService.BASE_FRIEND_URL) | 17 | return this.authHttp.get(FriendService.BASE_FRIEND_URL) |
19 | .map(res => <Friend[]>res.json()) | 18 | // Not implemented as a data list by the server yet |
20 | .catch(this.handleError); | 19 | // .map(this.restExtractor.extractDataList) |
20 | .map((res) => res.json()) | ||
21 | .catch((res) => this.restExtractor.handleError(res)); | ||
21 | } | 22 | } |
22 | 23 | ||
23 | makeFriends(notEmptyUrls) { | 24 | makeFriends(notEmptyUrls) { |
@@ -26,18 +27,13 @@ export class FriendService { | |||
26 | }; | 27 | }; |
27 | 28 | ||
28 | return this.authHttp.post(FriendService.BASE_FRIEND_URL + 'makefriends', body) | 29 | return this.authHttp.post(FriendService.BASE_FRIEND_URL + 'makefriends', body) |
29 | .map(res => res.status) | 30 | .map(this.restExtractor.extractDataBool) |
30 | .catch(this.handleError); | 31 | .catch((res) => this.restExtractor.handleError(res)); |
31 | } | 32 | } |
32 | 33 | ||
33 | quitFriends() { | 34 | quitFriends() { |
34 | return this.authHttp.get(FriendService.BASE_FRIEND_URL + 'quitfriends') | 35 | return this.authHttp.get(FriendService.BASE_FRIEND_URL + 'quitfriends') |
35 | .map(res => res.status) | 36 | .map(res => res.status) |
36 | .catch(this.handleError); | 37 | .catch((res) => this.restExtractor.handleError(res)); |
37 | } | ||
38 | |||
39 | private handleError (error: Response) { | ||
40 | console.error(error); | ||
41 | return Observable.throw(error.json().error || 'Server error'); | ||
42 | } | 38 | } |
43 | } | 39 | } |
diff --git a/client/src/app/admin/users/shared/user.service.ts b/client/src/app/admin/users/shared/user.service.ts index be433f0a1..d96db4575 100644 --- a/client/src/app/admin/users/shared/user.service.ts +++ b/client/src/app/admin/users/shared/user.service.ts | |||
@@ -1,15 +1,16 @@ | |||
1 | import { Injectable } from '@angular/core'; | 1 | import { Injectable } from '@angular/core'; |
2 | import { Response } from '@angular/http'; | ||
3 | import { Observable } from 'rxjs/Observable'; | ||
4 | 2 | ||
5 | import { AuthHttp, User } from '../../../shared'; | 3 | import { AuthHttp, RestExtractor, ResultList, User } from '../../../shared'; |
6 | 4 | ||
7 | @Injectable() | 5 | @Injectable() |
8 | export class UserService { | 6 | export class UserService { |
9 | // TODO: merge this constant with account | 7 | // TODO: merge this constant with account |
10 | private static BASE_USERS_URL = '/api/v1/users/'; | 8 | private static BASE_USERS_URL = '/api/v1/users/'; |
11 | 9 | ||
12 | constructor(private authHttp: AuthHttp) {} | 10 | constructor( |
11 | private authHttp: AuthHttp, | ||
12 | private restExtractor: RestExtractor | ||
13 | ) {} | ||
13 | 14 | ||
14 | addUser(username: string, password: string) { | 15 | addUser(username: string, password: string) { |
15 | const body = { | 16 | const body = { |
@@ -17,23 +18,25 @@ export class UserService { | |||
17 | password | 18 | password |
18 | }; | 19 | }; |
19 | 20 | ||
20 | return this.authHttp.post(UserService.BASE_USERS_URL, body); | 21 | return this.authHttp.post(UserService.BASE_USERS_URL, body) |
22 | .map(this.restExtractor.extractDataBool) | ||
23 | .catch((res) => this.restExtractor.handleError(res)); | ||
21 | } | 24 | } |
22 | 25 | ||
23 | getUsers() { | 26 | getUsers() { |
24 | return this.authHttp.get(UserService.BASE_USERS_URL) | 27 | return this.authHttp.get(UserService.BASE_USERS_URL) |
25 | .map(res => res.json()) | 28 | .map(this.restExtractor.extractDataList) |
26 | .map(this.extractUsers) | 29 | .map(this.extractUsers) |
27 | .catch(this.handleError); | 30 | .catch((res) => this.restExtractor.handleError(res)); |
28 | } | 31 | } |
29 | 32 | ||
30 | removeUser(user: User) { | 33 | removeUser(user: User) { |
31 | return this.authHttp.delete(UserService.BASE_USERS_URL + user.id); | 34 | return this.authHttp.delete(UserService.BASE_USERS_URL + user.id); |
32 | } | 35 | } |
33 | 36 | ||
34 | private extractUsers(body: any) { | 37 | private extractUsers(result: ResultList) { |
35 | const usersJson = body.data; | 38 | const usersJson = result.data; |
36 | const totalUsers = body.total; | 39 | const totalUsers = result.total; |
37 | const users = []; | 40 | const users = []; |
38 | for (const userJson of usersJson) { | 41 | for (const userJson of usersJson) { |
39 | users.push(new User(userJson)); | 42 | users.push(new User(userJson)); |
@@ -41,9 +44,4 @@ export class UserService { | |||
41 | 44 | ||
42 | return { users, totalUsers }; | 45 | return { users, totalUsers }; |
43 | } | 46 | } |
44 | |||
45 | private handleError(error: Response) { | ||
46 | console.error(error); | ||
47 | return Observable.throw(error.json().error || 'Server error'); | ||
48 | } | ||
49 | } | 47 | } |
diff --git a/client/src/app/app.component.ts b/client/src/app/app.component.ts index 2e0fd13f1..9d05c272f 100644 --- a/client/src/app/app.component.ts +++ b/client/src/app/app.component.ts | |||
@@ -3,7 +3,7 @@ import { Router, ROUTER_DIRECTIVES } from '@angular/router'; | |||
3 | 3 | ||
4 | import { MenuAdminComponent } from './admin'; | 4 | import { MenuAdminComponent } from './admin'; |
5 | import { MenuComponent } from './menu.component'; | 5 | import { MenuComponent } from './menu.component'; |
6 | import { SearchComponent, SearchService } from './shared'; | 6 | import { RestExtractor, RestService, SearchComponent, SearchService } from './shared'; |
7 | import { VideoService } from './videos'; | 7 | import { VideoService } from './videos'; |
8 | 8 | ||
9 | @Component({ | 9 | @Component({ |
@@ -11,7 +11,7 @@ import { VideoService } from './videos'; | |||
11 | template: require('./app.component.html'), | 11 | template: require('./app.component.html'), |
12 | styles: [ require('./app.component.scss') ], | 12 | styles: [ require('./app.component.scss') ], |
13 | directives: [ MenuAdminComponent, MenuComponent, ROUTER_DIRECTIVES, SearchComponent ], | 13 | directives: [ MenuAdminComponent, MenuComponent, ROUTER_DIRECTIVES, SearchComponent ], |
14 | providers: [ VideoService, SearchService ] | 14 | providers: [ RestExtractor, RestService, VideoService, SearchService ] |
15 | }) | 15 | }) |
16 | 16 | ||
17 | export class AppComponent { | 17 | export class AppComponent { |
diff --git a/client/src/app/login/login.component.ts b/client/src/app/login/login.component.ts index fe867b7b4..1e0ba0fe8 100644 --- a/client/src/app/login/login.component.ts +++ b/client/src/app/login/login.component.ts | |||
@@ -37,12 +37,12 @@ export class LoginComponent implements OnInit { | |||
37 | this.router.navigate(['/videos/list']); | 37 | this.router.navigate(['/videos/list']); |
38 | }, | 38 | }, |
39 | error => { | 39 | error => { |
40 | console.error(error); | 40 | console.error(error.json); |
41 | 41 | ||
42 | if (error.error === 'invalid_grant') { | 42 | if (error.json.error === 'invalid_grant') { |
43 | this.error = 'Credentials are invalid.'; | 43 | this.error = 'Credentials are invalid.'; |
44 | } else { | 44 | } else { |
45 | this.error = `${error.error}: ${error.error_description}`; | 45 | this.error = `${error.json.error}: ${error.json.error_description}`; |
46 | } | 46 | } |
47 | } | 47 | } |
48 | ); | 48 | ); |
diff --git a/client/src/app/shared/auth/auth.service.ts b/client/src/app/shared/auth/auth.service.ts index 8eea0c4bf..2273048c8 100644 --- a/client/src/app/shared/auth/auth.service.ts +++ b/client/src/app/shared/auth/auth.service.ts | |||
@@ -1,10 +1,11 @@ | |||
1 | import { Injectable } from '@angular/core'; | 1 | import { Injectable } from '@angular/core'; |
2 | import { Headers, Http, Response, URLSearchParams } from '@angular/http'; | 2 | import { Headers, Http, URLSearchParams } from '@angular/http'; |
3 | import { Observable } from 'rxjs/Observable'; | 3 | import { Observable } from 'rxjs/Observable'; |
4 | import { Subject } from 'rxjs/Subject'; | 4 | import { Subject } from 'rxjs/Subject'; |
5 | 5 | ||
6 | import { AuthStatus } from './auth-status.model'; | 6 | import { AuthStatus } from './auth-status.model'; |
7 | import { AuthUser } from './auth-user.model'; | 7 | import { AuthUser } from './auth-user.model'; |
8 | import { RestExtractor } from '../rest'; | ||
8 | 9 | ||
9 | @Injectable() | 10 | @Injectable() |
10 | export class AuthService { | 11 | export class AuthService { |
@@ -19,15 +20,15 @@ export class AuthService { | |||
19 | private loginChanged: Subject<AuthStatus>; | 20 | private loginChanged: Subject<AuthStatus>; |
20 | private user: AuthUser = null; | 21 | private user: AuthUser = null; |
21 | 22 | ||
22 | constructor(private http: Http) { | 23 | constructor(private http: Http, private restExtractor: RestExtractor) { |
23 | this.loginChanged = new Subject<AuthStatus>(); | 24 | this.loginChanged = new Subject<AuthStatus>(); |
24 | this.loginChangedSource = this.loginChanged.asObservable(); | 25 | this.loginChangedSource = this.loginChanged.asObservable(); |
25 | 26 | ||
26 | // Fetch the client_id/client_secret | 27 | // Fetch the client_id/client_secret |
27 | // FIXME: save in local storage? | 28 | // FIXME: save in local storage? |
28 | this.http.get(AuthService.BASE_CLIENT_URL) | 29 | this.http.get(AuthService.BASE_CLIENT_URL) |
29 | .map(res => res.json()) | 30 | .map(this.restExtractor.extractDataGet) |
30 | .catch(this.handleError) | 31 | .catch((res) => this.restExtractor.handleError(res)) |
31 | .subscribe( | 32 | .subscribe( |
32 | result => { | 33 | result => { |
33 | this.clientId = result.client_id; | 34 | this.clientId = result.client_id; |
@@ -101,14 +102,14 @@ export class AuthService { | |||
101 | }; | 102 | }; |
102 | 103 | ||
103 | return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) | 104 | return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) |
104 | .map(res => res.json()) | 105 | .map(this.restExtractor.extractDataGet) |
105 | .map(res => { | 106 | .map(res => { |
106 | res.username = username; | 107 | res.username = username; |
107 | return res; | 108 | return res; |
108 | }) | 109 | }) |
109 | .flatMap(res => this.fetchUserInformations(res)) | 110 | .flatMap(res => this.fetchUserInformations(res)) |
110 | .map(res => this.handleLogin(res)) | 111 | .map(res => this.handleLogin(res)) |
111 | .catch(this.handleError); | 112 | .catch((res) => this.restExtractor.handleError(res)); |
112 | } | 113 | } |
113 | 114 | ||
114 | logout() { | 115 | logout() { |
@@ -139,9 +140,9 @@ export class AuthService { | |||
139 | }; | 140 | }; |
140 | 141 | ||
141 | return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) | 142 | return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) |
142 | .map(res => res.json()) | 143 | .map(this.restExtractor.extractDataGet) |
143 | .map(res => this.handleRefreshToken(res)) | 144 | .map(res => this.handleRefreshToken(res)) |
144 | .catch(this.handleError); | 145 | .catch((res) => this.restExtractor.handleError(res)); |
145 | } | 146 | } |
146 | 147 | ||
147 | private fetchUserInformations (obj: any) { | 148 | private fetchUserInformations (obj: any) { |
@@ -160,11 +161,6 @@ export class AuthService { | |||
160 | ); | 161 | ); |
161 | } | 162 | } |
162 | 163 | ||
163 | private handleError (error: Response) { | ||
164 | console.error(error); | ||
165 | return Observable.throw(error.json() || { error: 'Server error' }); | ||
166 | } | ||
167 | |||
168 | private handleLogin (obj: any) { | 164 | private handleLogin (obj: any) { |
169 | const id = obj.id; | 165 | const id = obj.id; |
170 | const username = obj.username; | 166 | const username = obj.username; |
diff --git a/client/src/app/shared/index.ts b/client/src/app/shared/index.ts index 9edf9b4a0..c362a0e4a 100644 --- a/client/src/app/shared/index.ts +++ b/client/src/app/shared/index.ts | |||
@@ -1,4 +1,5 @@ | |||
1 | export * from './auth'; | 1 | export * from './auth'; |
2 | export * from './form-validators'; | 2 | export * from './form-validators'; |
3 | export * from './rest'; | ||
3 | export * from './search'; | 4 | export * from './search'; |
4 | export * from './users'; | 5 | export * from './users'; |
diff --git a/client/src/app/shared/rest/index.ts b/client/src/app/shared/rest/index.ts new file mode 100644 index 000000000..3c9509dc7 --- /dev/null +++ b/client/src/app/shared/rest/index.ts | |||
@@ -0,0 +1,3 @@ | |||
1 | export * from './rest-extractor.service'; | ||
2 | export * from './rest-pagination'; | ||
3 | export * from './rest.service'; | ||
diff --git a/client/src/app/shared/rest/rest-extractor.service.ts b/client/src/app/shared/rest/rest-extractor.service.ts new file mode 100644 index 000000000..aa44799af --- /dev/null +++ b/client/src/app/shared/rest/rest-extractor.service.ts | |||
@@ -0,0 +1,46 @@ | |||
1 | import { Injectable } from '@angular/core'; | ||
2 | import { Response } from '@angular/http'; | ||
3 | import { Observable } from 'rxjs/Observable'; | ||
4 | |||
5 | export interface ResultList { | ||
6 | data: any[]; | ||
7 | total: number; | ||
8 | } | ||
9 | |||
10 | @Injectable() | ||
11 | export class RestExtractor { | ||
12 | |||
13 | constructor () { ; } | ||
14 | |||
15 | extractDataBool(res: Response) { | ||
16 | return true; | ||
17 | } | ||
18 | |||
19 | extractDataList(res: Response) { | ||
20 | const body = res.json(); | ||
21 | |||
22 | const ret: ResultList = { | ||
23 | data: body.data, | ||
24 | total: body.total | ||
25 | }; | ||
26 | |||
27 | return ret; | ||
28 | } | ||
29 | |||
30 | extractDataGet(res: Response) { | ||
31 | return res.json(); | ||
32 | } | ||
33 | |||
34 | handleError(res: Response) { | ||
35 | let text = 'Server error: '; | ||
36 | text += res.text(); | ||
37 | let json = res.json(); | ||
38 | |||
39 | const error = { | ||
40 | json, | ||
41 | text | ||
42 | }; | ||
43 | |||
44 | return Observable.throw(error); | ||
45 | } | ||
46 | } | ||
diff --git a/client/src/app/videos/shared/pagination.model.ts b/client/src/app/shared/rest/rest-pagination.ts index eda44ebfb..0cfa4f468 100644 --- a/client/src/app/videos/shared/pagination.model.ts +++ b/client/src/app/shared/rest/rest-pagination.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | export interface Pagination { | 1 | export interface RestPagination { |
2 | currentPage: number; | 2 | currentPage: number; |
3 | itemsPerPage: number; | 3 | itemsPerPage: number; |
4 | totalItems: number; | 4 | totalItems: number; |
5 | } | 5 | }; |
diff --git a/client/src/app/shared/rest/rest.service.ts b/client/src/app/shared/rest/rest.service.ts new file mode 100644 index 000000000..16b47e957 --- /dev/null +++ b/client/src/app/shared/rest/rest.service.ts | |||
@@ -0,0 +1,27 @@ | |||
1 | import { Injectable } from '@angular/core'; | ||
2 | import { URLSearchParams } from '@angular/http'; | ||
3 | |||
4 | import { RestPagination } from './rest-pagination'; | ||
5 | |||
6 | @Injectable() | ||
7 | export class RestService { | ||
8 | |||
9 | buildRestGetParams(pagination?: RestPagination, sort?: string) { | ||
10 | const params = new URLSearchParams(); | ||
11 | |||
12 | if (pagination) { | ||
13 | const start: number = (pagination.currentPage - 1) * pagination.itemsPerPage; | ||
14 | const count: number = pagination.itemsPerPage; | ||
15 | |||
16 | params.set('start', start.toString()); | ||
17 | params.set('count', count.toString()); | ||
18 | } | ||
19 | |||
20 | if (sort) { | ||
21 | params.set('sort', sort); | ||
22 | } | ||
23 | |||
24 | return params; | ||
25 | } | ||
26 | |||
27 | } | ||
diff --git a/client/src/app/videos/shared/index.ts b/client/src/app/videos/shared/index.ts index a54120f5d..67d16ead1 100644 --- a/client/src/app/videos/shared/index.ts +++ b/client/src/app/videos/shared/index.ts | |||
@@ -1,5 +1,4 @@ | |||
1 | export * from './loader'; | 1 | export * from './loader'; |
2 | export * from './pagination.model'; | ||
3 | export * from './sort-field.type'; | 2 | export * from './sort-field.type'; |
4 | export * from './video.model'; | 3 | export * from './video.model'; |
5 | export * from './video.service'; | 4 | export * from './video.service'; |
diff --git a/client/src/app/videos/shared/video.service.ts b/client/src/app/videos/shared/video.service.ts index b4396f767..ad8557533 100644 --- a/client/src/app/videos/shared/video.service.ts +++ b/client/src/app/videos/shared/video.service.ts | |||
@@ -1,11 +1,10 @@ | |||
1 | import { Injectable } from '@angular/core'; | 1 | import { Injectable } from '@angular/core'; |
2 | import { Http, Response, URLSearchParams } from '@angular/http'; | 2 | import { Http } from '@angular/http'; |
3 | import { Observable } from 'rxjs/Observable'; | 3 | import { Observable } from 'rxjs/Observable'; |
4 | 4 | ||
5 | import { Pagination } from './pagination.model'; | ||
6 | import { Search } from '../../shared'; | 5 | import { Search } from '../../shared'; |
7 | import { SortField } from './sort-field.type'; | 6 | import { SortField } from './sort-field.type'; |
8 | import { AuthHttp, AuthService } from '../../shared'; | 7 | import { AuthHttp, AuthService, RestExtractor, RestPagination, RestService, ResultList } from '../../shared'; |
9 | import { Video } from './video.model'; | 8 | import { Video } from './video.model'; |
10 | 9 | ||
11 | @Injectable() | 10 | @Injectable() |
@@ -15,68 +14,51 @@ export class VideoService { | |||
15 | constructor( | 14 | constructor( |
16 | private authService: AuthService, | 15 | private authService: AuthService, |
17 | private authHttp: AuthHttp, | 16 | private authHttp: AuthHttp, |
18 | private http: Http | 17 | private http: Http, |
18 | private restExtractor: RestExtractor, | ||
19 | private restService: RestService | ||
19 | ) {} | 20 | ) {} |
20 | 21 | ||
21 | getVideo(id: string) { | 22 | getVideo(id: string): Observable<Video> { |
22 | return this.http.get(VideoService.BASE_VIDEO_URL + id) | 23 | return this.http.get(VideoService.BASE_VIDEO_URL + id) |
23 | .map(res => <Video> res.json()) | 24 | .map(this.restExtractor.extractDataGet) |
24 | .catch(this.handleError); | 25 | .catch((res) => this.restExtractor.handleError(res)); |
25 | } | 26 | } |
26 | 27 | ||
27 | getVideos(pagination: Pagination, sort: SortField) { | 28 | getVideos(pagination: RestPagination, sort: SortField) { |
28 | const params = this.createPaginationParams(pagination); | 29 | const params = this.restService.buildRestGetParams(pagination, sort); |
29 | |||
30 | if (sort) params.set('sort', sort); | ||
31 | 30 | ||
32 | return this.http.get(VideoService.BASE_VIDEO_URL, { search: params }) | 31 | return this.http.get(VideoService.BASE_VIDEO_URL, { search: params }) |
33 | .map(res => res.json()) | 32 | .map(res => res.json()) |
34 | .map(this.extractVideos) | 33 | .map(this.extractVideos) |
35 | .catch(this.handleError); | 34 | .catch((res) => this.restExtractor.handleError(res)); |
36 | } | 35 | } |
37 | 36 | ||
38 | removeVideo(id: string) { | 37 | removeVideo(id: string) { |
39 | return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id) | 38 | return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id) |
40 | .map(res => <number> res.status) | 39 | .map(this.restExtractor.extractDataBool) |
41 | .catch(this.handleError); | 40 | .catch((res) => this.restExtractor.handleError(res)); |
42 | } | 41 | } |
43 | 42 | ||
44 | searchVideos(search: Search, pagination: Pagination, sort: SortField) { | 43 | searchVideos(search: Search, pagination: RestPagination, sort: SortField) { |
45 | const params = this.createPaginationParams(pagination); | 44 | const params = this.restService.buildRestGetParams(pagination, sort); |
46 | 45 | ||
47 | if (search.field) params.set('field', search.field); | 46 | if (search.field) params.set('field', search.field); |
48 | if (sort) params.set('sort', sort); | ||
49 | 47 | ||
50 | return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params }) | 48 | return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params }) |
51 | .map(res => res.json()) | 49 | .map(this.restExtractor.extractDataList) |
52 | .map(this.extractVideos) | 50 | .map(this.extractVideos) |
53 | .catch(this.handleError); | 51 | .catch((res) => this.restExtractor.handleError(res)); |
54 | } | ||
55 | |||
56 | private createPaginationParams(pagination: Pagination) { | ||
57 | const params = new URLSearchParams(); | ||
58 | const start: number = (pagination.currentPage - 1) * pagination.itemsPerPage; | ||
59 | const count: number = pagination.itemsPerPage; | ||
60 | |||
61 | params.set('start', start.toString()); | ||
62 | params.set('count', count.toString()); | ||
63 | |||
64 | return params; | ||
65 | } | 52 | } |
66 | 53 | ||
67 | private extractVideos(body: any) { | 54 | private extractVideos(result: ResultList) { |
68 | const videos_json = body.data; | 55 | const videosJson = result.data; |
69 | const totalVideos = body.total; | 56 | const totalVideos = result.total; |
70 | const videos = []; | 57 | const videos = []; |
71 | for (const video_json of videos_json) { | 58 | for (const videoJson of videosJson) { |
72 | videos.push(new Video(video_json)); | 59 | videos.push(new Video(videoJson)); |
73 | } | 60 | } |
74 | 61 | ||
75 | return { videos, totalVideos }; | 62 | return { videos, totalVideos }; |
76 | } | 63 | } |
77 | |||
78 | private handleError(error: Response) { | ||
79 | console.error(error); | ||
80 | return Observable.throw(error.json().error || 'Server error'); | ||
81 | } | ||
82 | } | 64 | } |
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 7c6d4b992..1324a6214 100644 --- a/client/src/app/videos/video-list/video-list.component.ts +++ b/client/src/app/videos/video-list/video-list.component.ts | |||
@@ -7,12 +7,11 @@ import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination'; | |||
7 | 7 | ||
8 | import { | 8 | import { |
9 | LoaderComponent, | 9 | LoaderComponent, |
10 | Pagination, | ||
11 | SortField, | 10 | SortField, |
12 | Video, | 11 | Video, |
13 | VideoService | 12 | VideoService |
14 | } from '../shared'; | 13 | } from '../shared'; |
15 | import { AuthService, AuthUser, Search, SearchField } from '../../shared'; | 14 | import { AuthService, AuthUser, RestPagination, Search, SearchField } from '../../shared'; |
16 | import { VideoMiniatureComponent } from './video-miniature.component'; | 15 | import { VideoMiniatureComponent } from './video-miniature.component'; |
17 | import { VideoSortComponent } from './video-sort.component'; | 16 | import { VideoSortComponent } from './video-sort.component'; |
18 | import { SearchService } from '../../shared'; | 17 | import { SearchService } from '../../shared'; |
@@ -27,7 +26,7 @@ import { SearchService } from '../../shared'; | |||
27 | 26 | ||
28 | export class VideoListComponent implements OnInit, OnDestroy { | 27 | export class VideoListComponent implements OnInit, OnDestroy { |
29 | loading: BehaviorSubject<boolean> = new BehaviorSubject(false); | 28 | loading: BehaviorSubject<boolean> = new BehaviorSubject(false); |
30 | pagination: Pagination = { | 29 | pagination: RestPagination = { |
31 | currentPage: 1, | 30 | currentPage: 1, |
32 | itemsPerPage: 9, | 31 | itemsPerPage: 9, |
33 | totalItems: null | 32 | totalItems: null |
diff --git a/client/src/main.ts b/client/src/main.ts index 7c058e12f..7caabe914 100644 --- a/client/src/main.ts +++ b/client/src/main.ts | |||
@@ -9,7 +9,7 @@ import { bootstrap } from '@angular/platform-browser-dynamic'; | |||
9 | import { provideRouter } from '@angular/router'; | 9 | import { provideRouter } from '@angular/router'; |
10 | 10 | ||
11 | import { routes } from './app/app.routes'; | 11 | import { routes } from './app/app.routes'; |
12 | import { AuthHttp, AuthService } from './app/shared'; | 12 | import { AuthHttp, AuthService, RestExtractor } from './app/shared'; |
13 | import { AppComponent } from './app/app.component'; | 13 | import { AppComponent } from './app/app.component'; |
14 | 14 | ||
15 | if (process.env.ENV === 'production') { | 15 | if (process.env.ENV === 'production') { |
@@ -26,6 +26,7 @@ bootstrap(AppComponent, [ | |||
26 | }), | 26 | }), |
27 | 27 | ||
28 | AuthService, | 28 | AuthService, |
29 | RestExtractor, | ||
29 | 30 | ||
30 | provideRouter(routes), | 31 | provideRouter(routes), |
31 | 32 | ||