]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/admin/users/shared/user.service.ts
Client: add basic aot support
[github/Chocobozzz/PeerTube.git] / client / src / app / admin / users / shared / user.service.ts
1 import { Injectable } from '@angular/core';
2 import 'rxjs/add/operator/catch';
3 import 'rxjs/add/operator/map';
4
5 import { AuthHttp, RestExtractor, ResultList, 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(
13 private authHttp: AuthHttp,
14 private restExtractor: RestExtractor
15 ) {}
16
17 addUser(username: string, password: string) {
18 const body = {
19 username,
20 password
21 };
22
23 return this.authHttp.post(UserService.BASE_USERS_URL, body)
24 .map(this.restExtractor.extractDataBool)
25 .catch(this.restExtractor.handleError);
26 }
27
28 getUsers() {
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));
33 }
34
35 removeUser(user: User) {
36 return this.authHttp.delete(UserService.BASE_USERS_URL + user.id);
37 }
38
39 private extractUsers(result: ResultList) {
40 const usersJson = result.data;
41 const totalUsers = result.total;
42 const users = [];
43 for (const userJson of usersJson) {
44 users.push(new User(userJson));
45 }
46
47 return { users, totalUsers };
48 }
49 }