]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { catchError, map } from 'rxjs/operators' |
d592e0a9 | 2 | import { HttpClient, HttpParams } from '@angular/common/http' |
c30745f3 C |
3 | import { Injectable } from '@angular/core' |
4 | import { BytesPipe } from 'ngx-pipes' | |
5 | import { SortMeta } from 'primeng/components/common/sortmeta' | |
db400f44 | 6 | import { Observable } from 'rxjs' |
c30745f3 | 7 | import { ResultList, UserCreate, UserUpdate } from '../../../../../../shared' |
63c4db6d | 8 | import { environment } from '../../../../environments/environment' |
c30745f3 | 9 | import { RestExtractor, RestPagination, RestService, User } from '../../../shared' |
7da18e44 C |
10 | |
11 | @Injectable() | |
12 | export class UserService { | |
63c4db6d | 13 | private static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/' |
b0f9f39e | 14 | private bytesPipe = new BytesPipe() |
7da18e44 | 15 | |
df98563e | 16 | constructor ( |
d592e0a9 C |
17 | private authHttp: HttpClient, |
18 | private restService: RestService, | |
de59c48f | 19 | private restExtractor: RestExtractor |
db400f44 C |
20 | ) { |
21 | } | |
7da18e44 | 22 | |
4771e000 C |
23 | addUser (userCreate: UserCreate) { |
24 | return this.authHttp.post(UserService.BASE_USERS_URL, userCreate) | |
db400f44 C |
25 | .pipe( |
26 | map(this.restExtractor.extractDataBool), | |
27 | catchError(err => this.restExtractor.handleError(err)) | |
28 | ) | |
7da18e44 C |
29 | } |
30 | ||
8094a898 C |
31 | updateUser (userId: number, userUpdate: UserUpdate) { |
32 | return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate) | |
db400f44 C |
33 | .pipe( |
34 | map(this.restExtractor.extractDataBool), | |
35 | catchError(err => this.restExtractor.handleError(err)) | |
36 | ) | |
8094a898 C |
37 | } |
38 | ||
39 | getUser (userId: number) { | |
d592e0a9 | 40 | return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId) |
db400f44 | 41 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
8094a898 C |
42 | } |
43 | ||
d592e0a9 C |
44 | getUsers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<User>> { |
45 | let params = new HttpParams() | |
46 | params = this.restService.addRestGetParams(params, pagination, sort) | |
47 | ||
48 | return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params }) | |
db400f44 C |
49 | .pipe( |
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)) | |
53 | ) | |
7da18e44 C |
54 | } |
55 | ||
df98563e C |
56 | removeUser (user: User) { |
57 | return this.authHttp.delete(UserService.BASE_USERS_URL + user.id) | |
db400f44 | 58 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
7da18e44 | 59 | } |
b0f9f39e | 60 | |
d592e0a9 C |
61 | private formatUser (user: User) { |
62 | let videoQuota | |
63 | if (user.videoQuota === -1) { | |
64 | videoQuota = 'Unlimited' | |
65 | } else { | |
66 | videoQuota = this.bytesPipe.transform(user.videoQuota) | |
67 | } | |
b0f9f39e | 68 | |
d592e0a9 C |
69 | return Object.assign(user, { |
70 | videoQuota | |
b0f9f39e | 71 | }) |
b0f9f39e | 72 | } |
7da18e44 | 73 | } |