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'
12 export class UserService {
13 private static BASE_USERS_URL = API_URL + '/api/v1/users/'
14 private bytesPipe = new BytesPipe()
17 private authHttp: HttpClient,
18 private restService: RestService,
19 private restExtractor: RestExtractor
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))
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))
34 getUser (userId: number) {
35 return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId)
36 .catch(err => this.restExtractor.handleError(err))
39 getUsers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<User>> {
40 let params = new HttpParams()
41 params = this.restService.addRestGetParams(params, pagination, sort)
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))
49 removeUser (user: User) {
50 return this.authHttp.delete(UserService.BASE_USERS_URL + user.id)
53 private formatUser (user: User) {
55 if (user.videoQuota === -1) {
56 videoQuota = 'Unlimited'
58 videoQuota = this.bytesPipe.transform(user.videoQuota)
61 return Object.assign(user, {