1 import { Injectable } from '@angular/core';
2 import { Response } from '@angular/http';
3 import { Observable } from 'rxjs/Observable';
5 import { AuthHttp, User } from '../../../shared';
8 export class UserService {
9 // TODO: merge this constant with account
10 private static BASE_USERS_URL = '/api/v1/users/';
12 constructor(private authHttp: AuthHttp) {}
14 addUser(username: string, password: string) {
20 return this.authHttp.post(UserService.BASE_USERS_URL, body);
24 return this.authHttp.get(UserService.BASE_USERS_URL)
25 .map(res => res.json())
26 .map(this.extractUsers)
27 .catch(this.handleError);
30 removeUser(user: User) {
31 return this.authHttp.delete(UserService.BASE_USERS_URL + user.id);
34 private extractUsers(body: any) {
35 const usersJson = body.data;
36 const totalUsers = body.total;
38 for (const userJson of usersJson) {
39 users.push(new User(userJson));
42 return { users, totalUsers };
45 private handleError(error: Response) {
47 return Observable.throw(error.json().error || 'Server error');