]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/shared/user.service.ts
Move to angular cli
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / shared / user.service.ts
1 import { HttpClient, HttpParams } from '@angular/common/http'
2 import { Injectable } from '@angular/core'
3 import { BytesPipe } from 'ngx-pipes'
4 import { SortMeta } from 'primeng/components/common/sortmeta'
5 import 'rxjs/add/operator/catch'
6 import 'rxjs/add/operator/map'
7 import { Observable } from 'rxjs/Observable'
8 import { ResultList, UserCreate, UserUpdate } from '../../../../../../shared'
9 import { environment } from '../../../../environments/environment'
10 import { RestExtractor, RestPagination, RestService, User } from '../../../shared'
11
12 @Injectable()
13 export class UserService {
14 private static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
15 private bytesPipe = new BytesPipe()
16
17 constructor (
18 private authHttp: HttpClient,
19 private restService: RestService,
20 private restExtractor: RestExtractor
21 ) {}
22
23 addUser (userCreate: UserCreate) {
24 return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
25 .map(this.restExtractor.extractDataBool)
26 .catch(err => this.restExtractor.handleError(err))
27 }
28
29 updateUser (userId: number, userUpdate: UserUpdate) {
30 return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
31 .map(this.restExtractor.extractDataBool)
32 .catch(err => this.restExtractor.handleError(err))
33 }
34
35 getUser (userId: number) {
36 return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId)
37 .catch(err => this.restExtractor.handleError(err))
38 }
39
40 getUsers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<User>> {
41 let params = new HttpParams()
42 params = this.restService.addRestGetParams(params, pagination, sort)
43
44 return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params })
45 .map(res => this.restExtractor.convertResultListDateToHuman(res))
46 .map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this)))
47 .catch(err => this.restExtractor.handleError(err))
48 }
49
50 removeUser (user: User) {
51 return this.authHttp.delete(UserService.BASE_USERS_URL + user.id)
52 }
53
54 private formatUser (user: User) {
55 let videoQuota
56 if (user.videoQuota === -1) {
57 videoQuota = 'Unlimited'
58 } else {
59 videoQuota = this.bytesPipe.transform(user.videoQuota)
60 }
61
62 return Object.assign(user, {
63 videoQuota
64 })
65 }
66 }