aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/admin/users/shared/user.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/admin/users/shared/user.service.ts')
-rw-r--r--client/src/app/admin/users/shared/user.service.ts28
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 @@
1import { Injectable } from '@angular/core'; 1import { Injectable } from '@angular/core';
2import { Response } from '@angular/http';
3import { Observable } from 'rxjs/Observable';
4 2
5import { AuthHttp, User } from '../../../shared'; 3import { AuthHttp, RestExtractor, ResultList, User } from '../../../shared';
6 4
7@Injectable() 5@Injectable()
8export class UserService { 6export 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}