]>
Commit | Line | Data |
---|---|---|
df98563e | 1 | import { Injectable } from '@angular/core' |
d592e0a9 | 2 | import { HttpParams } from '@angular/common/http' |
3523b64a | 3 | import { SortMeta } from 'primeng/components/common/sortmeta' |
4635f59d | 4 | import { ComponentPagination } from './component-pagination.model' |
de59c48f | 5 | |
df98563e | 6 | import { RestPagination } from './rest-pagination' |
de59c48f C |
7 | |
8 | @Injectable() | |
9 | export class RestService { | |
10 | ||
d592e0a9 C |
11 | addRestGetParams (params: HttpParams, pagination?: RestPagination, sort?: SortMeta | string) { |
12 | let newParams = params | |
de59c48f | 13 | |
d592e0a9 C |
14 | if (pagination !== undefined) { |
15 | newParams = newParams.set('start', pagination.start.toString()) | |
16 | .set('count', pagination.count.toString()) | |
de59c48f C |
17 | } |
18 | ||
d592e0a9 C |
19 | if (sort !== undefined) { |
20 | let sortString = '' | |
21 | ||
22 | if (typeof sort === 'string') { | |
23 | sortString = sort | |
24 | } else { | |
25 | const sortPrefix = sort.order === 1 ? '' : '-' | |
26 | sortString = sortPrefix + sort.field | |
27 | } | |
28 | ||
29 | newParams = newParams.set('sort', sortString) | |
de59c48f C |
30 | } |
31 | ||
d592e0a9 | 32 | return newParams |
de59c48f C |
33 | } |
34 | ||
4635f59d C |
35 | componentPaginationToRestPagination (componentPagination: ComponentPagination): RestPagination { |
36 | const start: number = (componentPagination.currentPage - 1) * componentPagination.itemsPerPage | |
37 | const count: number = componentPagination.itemsPerPage | |
38 | ||
39 | return { start, count } | |
40 | } | |
de59c48f | 41 | } |