]>
Commit | Line | Data |
---|---|---|
d592e0a9 | 1 | import { HttpClient, HttpParams } from '@angular/common/http' |
c30745f3 C |
2 | import { Injectable } from '@angular/core' |
3 | import { BytesPipe } from 'ngx-pipes' | |
4 | import { SortMeta } from 'primeng/components/common/sortmeta' | |
df98563e C |
5 | import 'rxjs/add/operator/catch' |
6 | import 'rxjs/add/operator/map' | |
c30745f3 C |
7 | import { Observable } from 'rxjs/Observable' |
8 | import { ResultList, UserCreate, UserUpdate } from '../../../../../../shared' | |
63c4db6d | 9 | import { environment } from '../../../../environments/environment' |
c30745f3 | 10 | import { RestExtractor, RestPagination, RestService, User } from '../../../shared' |
7da18e44 C |
11 | |
12 | @Injectable() | |
13 | export class UserService { | |
63c4db6d | 14 | private static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/' |
b0f9f39e | 15 | private bytesPipe = new BytesPipe() |
7da18e44 | 16 | |
df98563e | 17 | constructor ( |
d592e0a9 C |
18 | private authHttp: HttpClient, |
19 | private restService: RestService, | |
de59c48f C |
20 | private restExtractor: RestExtractor |
21 | ) {} | |
7da18e44 | 22 | |
4771e000 C |
23 | addUser (userCreate: UserCreate) { |
24 | return this.authHttp.post(UserService.BASE_USERS_URL, userCreate) | |
de59c48f | 25 | .map(this.restExtractor.extractDataBool) |
d592e0a9 | 26 | .catch(err => this.restExtractor.handleError(err)) |
7da18e44 C |
27 | } |
28 | ||
8094a898 C |
29 | updateUser (userId: number, userUpdate: UserUpdate) { |
30 | return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate) | |
d592e0a9 C |
31 | .map(this.restExtractor.extractDataBool) |
32 | .catch(err => this.restExtractor.handleError(err)) | |
8094a898 C |
33 | } |
34 | ||
35 | getUser (userId: number) { | |
d592e0a9 C |
36 | return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId) |
37 | .catch(err => this.restExtractor.handleError(err)) | |
8094a898 C |
38 | } |
39 | ||
d592e0a9 C |
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)) | |
7da18e44 C |
48 | } |
49 | ||
df98563e C |
50 | removeUser (user: User) { |
51 | return this.authHttp.delete(UserService.BASE_USERS_URL + user.id) | |
7da18e44 | 52 | } |
b0f9f39e | 53 | |
d592e0a9 C |
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 | } | |
b0f9f39e | 61 | |
d592e0a9 C |
62 | return Object.assign(user, { |
63 | videoQuota | |
b0f9f39e | 64 | }) |
b0f9f39e | 65 | } |
7da18e44 | 66 | } |