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