]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/rest/rest.service.ts
Better spacing beetween comments
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / rest / rest.service.ts
CommitLineData
df98563e 1import { Injectable } from '@angular/core'
d592e0a9 2import { HttpParams } from '@angular/common/http'
3523b64a 3import { SortMeta } from 'primeng/components/common/sortmeta'
4635f59d 4import { ComponentPagination } from './component-pagination.model'
de59c48f 5
df98563e 6import { RestPagination } from './rest-pagination'
de59c48f
C
7
8@Injectable()
9export 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
c199c427 35 addObjectParams (params: HttpParams, object: { [ name: string ]: any }) {
f37dc0dd
C
36 for (const name of Object.keys(object)) {
37 const value = object[name]
38 if (!value) continue
39
40 if (Array.isArray(value) && value.length !== 0) {
41 for (const v of value) params = params.append(name, v)
42 } else {
43 params = params.append(name, value)
44 }
45 }
46
47 return params
48 }
49
4635f59d
C
50 componentPaginationToRestPagination (componentPagination: ComponentPagination): RestPagination {
51 const start: number = (componentPagination.currentPage - 1) * componentPagination.itemsPerPage
52 const count: number = componentPagination.itemsPerPage
53
54 return { start, count }
55 }
de59c48f 56}