]>
Commit | Line | Data |
---|---|---|
7da18e44 | 1 | import { Injectable } from '@angular/core'; |
c16ce1de C |
2 | import 'rxjs/add/operator/catch'; |
3 | import 'rxjs/add/operator/map'; | |
7da18e44 | 4 | |
de59c48f | 5 | import { AuthHttp, RestExtractor, ResultList, User } from '../../../shared'; |
7da18e44 C |
6 | |
7 | @Injectable() | |
8 | export class UserService { | |
9 | // TODO: merge this constant with account | |
10 | private static BASE_USERS_URL = '/api/v1/users/'; | |
11 | ||
de59c48f C |
12 | constructor( |
13 | private authHttp: AuthHttp, | |
14 | private restExtractor: RestExtractor | |
15 | ) {} | |
7da18e44 C |
16 | |
17 | addUser(username: string, password: string) { | |
18 | const body = { | |
19 | username, | |
20 | password | |
21 | }; | |
22 | ||
de59c48f C |
23 | return this.authHttp.post(UserService.BASE_USERS_URL, body) |
24 | .map(this.restExtractor.extractDataBool) | |
bf68dd75 | 25 | .catch(this.restExtractor.handleError); |
7da18e44 C |
26 | } |
27 | ||
28 | getUsers() { | |
29 | return this.authHttp.get(UserService.BASE_USERS_URL) | |
de59c48f | 30 | .map(this.restExtractor.extractDataList) |
7da18e44 | 31 | .map(this.extractUsers) |
de59c48f | 32 | .catch((res) => this.restExtractor.handleError(res)); |
7da18e44 C |
33 | } |
34 | ||
35 | removeUser(user: User) { | |
36 | return this.authHttp.delete(UserService.BASE_USERS_URL + user.id); | |
37 | } | |
38 | ||
de59c48f C |
39 | private extractUsers(result: ResultList) { |
40 | const usersJson = result.data; | |
41 | const totalUsers = result.total; | |
7da18e44 C |
42 | const users = []; |
43 | for (const userJson of usersJson) { | |
44 | users.push(new User(userJson)); | |
45 | } | |
46 | ||
47 | return { users, totalUsers }; | |
48 | } | |
7da18e44 | 49 | } |