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.ts49
1 files changed, 49 insertions, 0 deletions
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..be433f0a1
--- /dev/null
+++ b/client/src/app/admin/users/shared/user.service.ts
@@ -0,0 +1,49 @@
1import { Injectable } from '@angular/core';
2import { Response } from '@angular/http';
3import { Observable } from 'rxjs/Observable';
4
5import { AuthHttp, User } from '../../../shared';
6
7@Injectable()
8export class UserService {
9 // TODO: merge this constant with account
10 private static BASE_USERS_URL = '/api/v1/users/';
11
12 constructor(private authHttp: AuthHttp) {}
13
14 addUser(username: string, password: string) {
15 const body = {
16 username,
17 password
18 };
19
20 return this.authHttp.post(UserService.BASE_USERS_URL, body);
21 }
22
23 getUsers() {
24 return this.authHttp.get(UserService.BASE_USERS_URL)
25 .map(res => res.json())
26 .map(this.extractUsers)
27 .catch(this.handleError);
28 }
29
30 removeUser(user: User) {
31 return this.authHttp.delete(UserService.BASE_USERS_URL + user.id);
32 }
33
34 private extractUsers(body: any) {
35 const usersJson = body.data;
36 const totalUsers = body.total;
37 const users = [];
38 for (const userJson of usersJson) {
39 users.push(new User(userJson));
40 }
41
42 return { users, totalUsers };
43 }
44
45 private handleError(error: Response) {
46 console.error(error);
47 return Observable.throw(error.json().error || 'Server error');
48 }
49}