]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/admin/users/shared/user.service.ts
Client: fix error display for component
[github/Chocobozzz/PeerTube.git] / client / src / app / admin / users / shared / user.service.ts
CommitLineData
7da18e44 1import { Injectable } from '@angular/core';
7da18e44 2
de59c48f 3import { AuthHttp, RestExtractor, ResultList, User } from '../../../shared';
7da18e44
C
4
5@Injectable()
6export class UserService {
7 // TODO: merge this constant with account
8 private static BASE_USERS_URL = '/api/v1/users/';
9
de59c48f
C
10 constructor(
11 private authHttp: AuthHttp,
12 private restExtractor: RestExtractor
13 ) {}
7da18e44
C
14
15 addUser(username: string, password: string) {
16 const body = {
17 username,
18 password
19 };
20
de59c48f
C
21 return this.authHttp.post(UserService.BASE_USERS_URL, body)
22 .map(this.restExtractor.extractDataBool)
bf68dd75 23 .catch(this.restExtractor.handleError);
7da18e44
C
24 }
25
26 getUsers() {
27 return this.authHttp.get(UserService.BASE_USERS_URL)
de59c48f 28 .map(this.restExtractor.extractDataList)
7da18e44 29 .map(this.extractUsers)
de59c48f 30 .catch((res) => this.restExtractor.handleError(res));
7da18e44
C
31 }
32
33 removeUser(user: User) {
34 return this.authHttp.delete(UserService.BASE_USERS_URL + user.id);
35 }
36
de59c48f
C
37 private extractUsers(result: ResultList) {
38 const usersJson = result.data;
39 const totalUsers = result.total;
7da18e44
C
40 const users = [];
41 for (const userJson of usersJson) {
42 users.push(new User(userJson));
43 }
44
45 return { users, totalUsers };
46 }
7da18e44 47}