]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/admin/users/shared/user.service.ts
Client: add user management
[github/Chocobozzz/PeerTube.git] / client / src / app / admin / users / shared / user.service.ts
1 import { Injectable } from '@angular/core';
2 import { Response } from '@angular/http';
3 import { Observable } from 'rxjs/Observable';
4
5 import { AuthHttp, 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(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 }