]>
Commit | Line | Data |
---|---|---|
f77eb73b | 1 | import { SortMeta } from 'primeng/api' |
feb34f6b C |
2 | import { HttpParams } from '@angular/common/http' |
3 | import { Injectable } from '@angular/core' | |
4 | import { ComponentPaginationLight } from './component-pagination.model' | |
df98563e | 5 | import { RestPagination } from './rest-pagination' |
de59c48f | 6 | |
feb34f6b C |
7 | interface QueryStringFilterPrefixes { |
8 | [key: string]: { | |
9 | prefix: string | |
10 | handler?: (v: string) => string | number | |
11 | multiple?: boolean | |
8491293b | 12 | isBoolean?: boolean |
feb34f6b C |
13 | } |
14 | } | |
15 | ||
16 | type ParseQueryStringFilterResult = { | |
8491293b | 17 | [key: string]: string | number | boolean | (string | number | boolean)[] |
feb34f6b C |
18 | } |
19 | ||
de59c48f C |
20 | @Injectable() |
21 | export class RestService { | |
22 | ||
d592e0a9 C |
23 | addRestGetParams (params: HttpParams, pagination?: RestPagination, sort?: SortMeta | string) { |
24 | let newParams = params | |
de59c48f | 25 | |
d592e0a9 C |
26 | if (pagination !== undefined) { |
27 | newParams = newParams.set('start', pagination.start.toString()) | |
28 | .set('count', pagination.count.toString()) | |
de59c48f C |
29 | } |
30 | ||
d592e0a9 C |
31 | if (sort !== undefined) { |
32 | let sortString = '' | |
33 | ||
34 | if (typeof sort === 'string') { | |
35 | sortString = sort | |
36 | } else { | |
37 | const sortPrefix = sort.order === 1 ? '' : '-' | |
38 | sortString = sortPrefix + sort.field | |
39 | } | |
40 | ||
41 | newParams = newParams.set('sort', sortString) | |
de59c48f C |
42 | } |
43 | ||
d592e0a9 | 44 | return newParams |
de59c48f C |
45 | } |
46 | ||
c199c427 | 47 | addObjectParams (params: HttpParams, object: { [ name: string ]: any }) { |
f37dc0dd C |
48 | for (const name of Object.keys(object)) { |
49 | const value = object[name] | |
1ebddadd | 50 | if (value === undefined || value === null) continue |
f37dc0dd | 51 | |
9abd170d | 52 | if (Array.isArray(value)) { |
f37dc0dd C |
53 | for (const v of value) params = params.append(name, v) |
54 | } else { | |
55 | params = params.append(name, value) | |
56 | } | |
57 | } | |
58 | ||
59 | return params | |
60 | } | |
61 | ||
440d39c5 | 62 | componentPaginationToRestPagination (componentPagination: ComponentPaginationLight): RestPagination { |
4635f59d C |
63 | const start: number = (componentPagination.currentPage - 1) * componentPagination.itemsPerPage |
64 | const count: number = componentPagination.itemsPerPage | |
65 | ||
66 | return { start, count } | |
67 | } | |
feb34f6b C |
68 | |
69 | parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes): ParseQueryStringFilterResult { | |
70 | if (!q) return {} | |
71 | ||
cc0e0d32 C |
72 | // Tokenize the strings using spaces that are not in quotes |
73 | const tokens = q.match(/(?:[^\s"]+|"[^"]*")+/g) | |
74 | .filter(token => !!token) | |
feb34f6b C |
75 | |
76 | // Build prefix array | |
77 | const prefixeStrings = Object.values(prefixes) | |
78 | .map(p => p.prefix) | |
79 | ||
80 | // Search is the querystring minus defined filters | |
81 | const searchTokens = tokens.filter(t => { | |
82 | return prefixeStrings.every(prefixString => t.startsWith(prefixString) === false) | |
83 | }) | |
84 | ||
85 | const additionalFilters: ParseQueryStringFilterResult = {} | |
86 | ||
87 | for (const prefixKey of Object.keys(prefixes)) { | |
88 | const prefixObj = prefixes[prefixKey] | |
89 | const prefix = prefixObj.prefix | |
90 | ||
91 | const matchedTokens = tokens.filter(t => t.startsWith(prefix)) | |
92 | .map(t => t.slice(prefix.length)) // Keep the value filter | |
cc0e0d32 | 93 | .map(t => t.replace(/^"|"$/g, '')) |
feb34f6b C |
94 | .map(t => { |
95 | if (prefixObj.handler) return prefixObj.handler(t) | |
96 | ||
97 | return t | |
98 | }) | |
1ebddadd | 99 | .filter(t => !!t || t === 0) |
8491293b | 100 | .map(t => prefixObj.isBoolean ? t === 'true' : t) |
feb34f6b C |
101 | |
102 | if (matchedTokens.length === 0) continue | |
103 | ||
104 | additionalFilters[prefixKey] = prefixObj.multiple === true | |
105 | ? matchedTokens | |
106 | : matchedTokens[0] | |
107 | } | |
108 | ||
109 | return { | |
1ebddadd | 110 | search: searchTokens.join(' ') || undefined, |
feb34f6b C |
111 | |
112 | ...additionalFilters | |
113 | } | |
114 | } | |
de59c48f | 115 | } |