1 import { Injectable } from '@angular/core'
2 import { HttpParams } from '@angular/common/http'
3 import { SortMeta } from 'primeng/components/common/sortmeta'
4 import { ComponentPagination } from './component-pagination.model'
6 import { RestPagination } from './rest-pagination'
9 export class RestService {
11 addRestGetParams (params: HttpParams, pagination?: RestPagination, sort?: SortMeta | string) {
12 let newParams = params
14 if (pagination !== undefined) {
15 newParams = newParams.set('start', pagination.start.toString())
16 .set('count', pagination.count.toString())
19 if (sort !== undefined) {
22 if (typeof sort === 'string') {
25 const sortPrefix = sort.order === 1 ? '' : '-'
26 sortString = sortPrefix + sort.field
29 newParams = newParams.set('sort', sortString)
35 addObjectParams (params: HttpParams, object: { [ name: string ]: any }) {
36 for (const name of Object.keys(object)) {
37 const value = object[name]
40 if (Array.isArray(value) && value.length !== 0) {
41 for (const v of value) params = params.append(name, v)
43 params = params.append(name, value)
50 componentPaginationToRestPagination (componentPagination: ComponentPagination): RestPagination {
51 const start: number = (componentPagination.currentPage - 1) * componentPagination.itemsPerPage
52 const count: number = componentPagination.itemsPerPage
54 return { start, count }