diff options
Diffstat (limited to 'client/src/app/+admin/users/shared')
-rw-r--r-- | client/src/app/+admin/users/shared/index.ts | 1 | ||||
-rw-r--r-- | client/src/app/+admin/users/shared/user.service.ts | 49 |
2 files changed, 50 insertions, 0 deletions
diff --git a/client/src/app/+admin/users/shared/index.ts b/client/src/app/+admin/users/shared/index.ts new file mode 100644 index 000000000..e17ee5c7a --- /dev/null +++ b/client/src/app/+admin/users/shared/index.ts | |||
@@ -0,0 +1 @@ | |||
export * from './user.service'; | |||
diff --git a/client/src/app/+admin/users/shared/user.service.ts b/client/src/app/+admin/users/shared/user.service.ts new file mode 100644 index 000000000..d9005b213 --- /dev/null +++ b/client/src/app/+admin/users/shared/user.service.ts | |||
@@ -0,0 +1,49 @@ | |||
1 | import { Injectable } from '@angular/core'; | ||
2 | import 'rxjs/add/operator/catch'; | ||
3 | import 'rxjs/add/operator/map'; | ||
4 | |||
5 | import { AuthHttp, RestExtractor, ResultList, User } from '../../../shared'; | ||
6 | |||
7 | @Injectable() | ||
8 | export class UserService { | ||
9 | // TODO: merge this constant with account | ||
10 | private static BASE_USERS_URL = '/api/v1/users/'; | ||
11 | |||
12 | constructor( | ||
13 | private authHttp: AuthHttp, | ||
14 | private restExtractor: RestExtractor | ||
15 | ) {} | ||
16 | |||
17 | addUser(username: string, password: string) { | ||
18 | const body = { | ||
19 | username, | ||
20 | password | ||
21 | }; | ||
22 | |||
23 | return this.authHttp.post(UserService.BASE_USERS_URL, body) | ||
24 | .map(this.restExtractor.extractDataBool) | ||
25 | .catch(this.restExtractor.handleError); | ||
26 | } | ||
27 | |||
28 | getUsers() { | ||
29 | return this.authHttp.get(UserService.BASE_USERS_URL) | ||
30 | .map(this.restExtractor.extractDataList) | ||
31 | .map(this.extractUsers) | ||
32 | .catch((res) => this.restExtractor.handleError(res)); | ||
33 | } | ||
34 | |||
35 | removeUser(user: User) { | ||
36 | return this.authHttp.delete(UserService.BASE_USERS_URL + user.id); | ||
37 | } | ||
38 | |||
39 | private extractUsers(result: ResultList) { | ||
40 | const usersJson = result.data; | ||
41 | const totalUsers = result.total; | ||
42 | const users = []; | ||
43 | for (const userJson of usersJson) { | ||
44 | users.push(new User(userJson)); | ||
45 | } | ||
46 | |||
47 | return { users, totalUsers }; | ||
48 | } | ||
49 | } | ||