]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/rest/rest.service.ts
Merge branch 'master' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / rest / rest.service.ts
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'
5
6 import { RestPagination } from './rest-pagination'
7
8 @Injectable()
9 export class RestService {
10
11 addRestGetParams (params: HttpParams, pagination?: RestPagination, sort?: SortMeta | string) {
12 let newParams = params
13
14 if (pagination !== undefined) {
15 newParams = newParams.set('start', pagination.start.toString())
16 .set('count', pagination.count.toString())
17 }
18
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)
30 }
31
32 return newParams
33 }
34
35 addObjectParams (params: HttpParams, object: { [ name: string ]: any }) {
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
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 }
56 }