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/app/admin/users | |
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/app/admin/users')
-rw-r--r-- | client/src/app/admin/users/shared/user.service.ts | 28 |
1 files changed, 13 insertions, 15 deletions
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 | } |