1 import { Injectable } from '@angular/core';
2 import 'rxjs/add/operator/catch';
3 import 'rxjs/add/operator/map';
5 import { AuthHttp, RestExtractor, ResultList, User } from '../../../shared';
8 export class UserService {
9 // TODO: merge this constant with account
10 private static BASE_USERS_URL = '/api/v1/users/';
13 private authHttp: AuthHttp,
14 private restExtractor: RestExtractor
17 addUser(username: string, password: string) {
23 return this.authHttp.post(UserService.BASE_USERS_URL, body)
24 .map(this.restExtractor.extractDataBool)
25 .catch(this.restExtractor.handleError);
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));
35 removeUser(user: User) {
36 return this.authHttp.delete(UserService.BASE_USERS_URL + user.id);
39 private extractUsers(result: ResultList) {
40 const usersJson = result.data;
41 const totalUsers = result.total;
43 for (const userJson of usersJson) {
44 users.push(new User(userJson));
47 return { users, totalUsers };