1 import { catchError, map } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { BytesPipe } from 'ngx-pipes'
5 import { SortMeta } from 'primeng/components/common/sortmeta'
6 import { Observable } from 'rxjs'
7 import { ResultList, UserCreate, UserUpdate } from '../../../../../../shared'
8 import { environment } from '../../../../environments/environment'
9 import { RestExtractor, RestPagination, RestService, User } from '../../../shared'
12 export class UserService {
13 private static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
14 private bytesPipe = new BytesPipe()
17 private authHttp: HttpClient,
18 private restService: RestService,
19 private restExtractor: RestExtractor
23 addUser (userCreate: UserCreate) {
24 return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
26 map(this.restExtractor.extractDataBool),
27 catchError(err => this.restExtractor.handleError(err))
31 updateUser (userId: number, userUpdate: UserUpdate) {
32 return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
34 map(this.restExtractor.extractDataBool),
35 catchError(err => this.restExtractor.handleError(err))
39 getUser (userId: number) {
40 return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId)
41 .pipe(catchError(err => this.restExtractor.handleError(err)))
44 getUsers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<User>> {
45 let params = new HttpParams()
46 params = this.restService.addRestGetParams(params, pagination, sort)
48 return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params })
50 map(res => this.restExtractor.convertResultListDateToHuman(res)),
51 map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))),
52 catchError(err => this.restExtractor.handleError(err))
56 removeUser (user: User) {
57 return this.authHttp.delete(UserService.BASE_USERS_URL + user.id)
58 .pipe(catchError(err => this.restExtractor.handleError(err)))
61 private formatUser (user: User) {
63 if (user.videoQuota === -1) {
64 videoQuota = 'Unlimited'
66 videoQuota = this.bytesPipe.transform(user.videoQuota)
69 return Object.assign(user, {