diff options
Diffstat (limited to 'client/src/app/+admin/users/shared/user.service.ts')
-rw-r--r-- | client/src/app/+admin/users/shared/user.service.ts | 56 |
1 files changed, 29 insertions, 27 deletions
diff --git a/client/src/app/+admin/users/shared/user.service.ts b/client/src/app/+admin/users/shared/user.service.ts index 999013bcc..a4b89bf1d 100644 --- a/client/src/app/+admin/users/shared/user.service.ts +++ b/client/src/app/+admin/users/shared/user.service.ts | |||
@@ -1,11 +1,14 @@ | |||
1 | import { Injectable } from '@angular/core' | 1 | import { Injectable } from '@angular/core' |
2 | import { HttpClient, HttpParams } from '@angular/common/http' | ||
3 | import { Observable } from 'rxjs/Observable' | ||
2 | import 'rxjs/add/operator/catch' | 4 | import 'rxjs/add/operator/catch' |
3 | import 'rxjs/add/operator/map' | 5 | import 'rxjs/add/operator/map' |
4 | 6 | ||
7 | import { SortMeta } from 'primeng/primeng' | ||
5 | import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe' | 8 | import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe' |
6 | 9 | ||
7 | import { AuthHttp, RestExtractor, RestDataSource, User } from '../../../shared' | 10 | import { RestExtractor, User, RestPagination, RestService } from '../../../shared' |
8 | import { UserCreate, UserUpdate } from '../../../../../../shared' | 11 | import { UserCreate, UserUpdate, ResultList } from '../../../../../../shared' |
9 | 12 | ||
10 | @Injectable() | 13 | @Injectable() |
11 | export class UserService { | 14 | export class UserService { |
@@ -13,53 +16,52 @@ export class UserService { | |||
13 | private bytesPipe = new BytesPipe() | 16 | private bytesPipe = new BytesPipe() |
14 | 17 | ||
15 | constructor ( | 18 | constructor ( |
16 | private authHttp: AuthHttp, | 19 | private authHttp: HttpClient, |
20 | private restService: RestService, | ||
17 | private restExtractor: RestExtractor | 21 | private restExtractor: RestExtractor |
18 | ) {} | 22 | ) {} |
19 | 23 | ||
20 | addUser (userCreate: UserCreate) { | 24 | addUser (userCreate: UserCreate) { |
21 | return this.authHttp.post(UserService.BASE_USERS_URL, userCreate) | 25 | return this.authHttp.post(UserService.BASE_USERS_URL, userCreate) |
22 | .map(this.restExtractor.extractDataBool) | 26 | .map(this.restExtractor.extractDataBool) |
23 | .catch(this.restExtractor.handleError) | 27 | .catch(err => this.restExtractor.handleError(err)) |
24 | } | 28 | } |
25 | 29 | ||
26 | updateUser (userId: number, userUpdate: UserUpdate) { | 30 | updateUser (userId: number, userUpdate: UserUpdate) { |
27 | return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate) | 31 | return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate) |
28 | .map(this.restExtractor.extractDataBool) | 32 | .map(this.restExtractor.extractDataBool) |
29 | .catch(this.restExtractor.handleError) | 33 | .catch(err => this.restExtractor.handleError(err)) |
30 | } | 34 | } |
31 | 35 | ||
32 | getUser (userId: number) { | 36 | getUser (userId: number) { |
33 | return this.authHttp.get(UserService.BASE_USERS_URL + userId) | 37 | return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId) |
34 | .map(this.restExtractor.extractDataGet) | 38 | .catch(err => this.restExtractor.handleError(err)) |
35 | .catch(this.restExtractor.handleError) | ||
36 | } | 39 | } |
37 | 40 | ||
38 | getDataSource () { | 41 | getUsers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<User>> { |
39 | return new RestDataSource(this.authHttp, UserService.BASE_USERS_URL, this.formatDataSource.bind(this)) | 42 | let params = new HttpParams() |
43 | params = this.restService.addRestGetParams(params, pagination, sort) | ||
44 | |||
45 | return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params }) | ||
46 | .map(res => this.restExtractor.convertResultListDateToHuman(res)) | ||
47 | .map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))) | ||
48 | .catch(err => this.restExtractor.handleError(err)) | ||
40 | } | 49 | } |
41 | 50 | ||
42 | removeUser (user: User) { | 51 | removeUser (user: User) { |
43 | return this.authHttp.delete(UserService.BASE_USERS_URL + user.id) | 52 | return this.authHttp.delete(UserService.BASE_USERS_URL + user.id) |
44 | } | 53 | } |
45 | 54 | ||
46 | private formatDataSource (users: User[]) { | 55 | private formatUser (user: User) { |
47 | const newUsers = [] | 56 | let videoQuota |
57 | if (user.videoQuota === -1) { | ||
58 | videoQuota = 'Unlimited' | ||
59 | } else { | ||
60 | videoQuota = this.bytesPipe.transform(user.videoQuota) | ||
61 | } | ||
48 | 62 | ||
49 | users.forEach(user => { | 63 | return Object.assign(user, { |
50 | let videoQuota | 64 | videoQuota |
51 | if (user.videoQuota === -1) { | ||
52 | videoQuota = 'Unlimited' | ||
53 | } else { | ||
54 | videoQuota = this.bytesPipe.transform(user.videoQuota) | ||
55 | } | ||
56 | |||
57 | const newUser = Object.assign(user, { | ||
58 | videoQuota | ||
59 | }) | ||
60 | newUsers.push(newUser) | ||
61 | }) | 65 | }) |
62 | |||
63 | return newUsers | ||
64 | } | 66 | } |
65 | } | 67 | } |