]>
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 | ||
3da38d6e C |
47 | addArrayParams (params: HttpParams, name: string, values: (string | number)[]) { |
48 | for (const v of values) { | |
49 | params = params.append(name, v) | |
50 | } | |
51 | ||
52 | return params | |
53 | } | |
54 | ||
c199c427 | 55 | addObjectParams (params: HttpParams, object: { [ name: string ]: any }) { |
f37dc0dd C |
56 | for (const name of Object.keys(object)) { |
57 | const value = object[name] | |
1ebddadd | 58 | if (value === undefined || value === null) continue |
f37dc0dd | 59 | |
9abd170d | 60 | if (Array.isArray(value)) { |
3da38d6e | 61 | params = this.addArrayParams(params, name, value) |
f37dc0dd C |
62 | } else { |
63 | params = params.append(name, value) | |
64 | } | |
65 | } | |
66 | ||
67 | return params | |
68 | } | |
69 | ||
440d39c5 | 70 | componentPaginationToRestPagination (componentPagination: ComponentPaginationLight): RestPagination { |
4635f59d C |
71 | const start: number = (componentPagination.currentPage - 1) * componentPagination.itemsPerPage |
72 | const count: number = componentPagination.itemsPerPage | |
73 | ||
74 | return { start, count } | |
75 | } | |
feb34f6b C |
76 | |
77 | parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes): ParseQueryStringFilterResult { | |
78 | if (!q) return {} | |
79 | ||
cc0e0d32 C |
80 | // Tokenize the strings using spaces that are not in quotes |
81 | const tokens = q.match(/(?:[^\s"]+|"[^"]*")+/g) | |
82 | .filter(token => !!token) | |
feb34f6b C |
83 | |
84 | // Build prefix array | |
85 | const prefixeStrings = Object.values(prefixes) | |
86 | .map(p => p.prefix) | |
87 | ||
88 | // Search is the querystring minus defined filters | |
89 | const searchTokens = tokens.filter(t => { | |
90 | return prefixeStrings.every(prefixString => t.startsWith(prefixString) === false) | |
91 | }) | |
92 | ||
93 | const additionalFilters: ParseQueryStringFilterResult = {} | |
94 | ||
95 | for (const prefixKey of Object.keys(prefixes)) { | |
96 | const prefixObj = prefixes[prefixKey] | |
97 | const prefix = prefixObj.prefix | |
98 | ||
99 | const matchedTokens = tokens.filter(t => t.startsWith(prefix)) | |
100 | .map(t => t.slice(prefix.length)) // Keep the value filter | |
1a7d0887 | 101 | .map(t => t.replace(/^"|"$/g, '')) // Remove "" |
feb34f6b C |
102 | .map(t => { |
103 | if (prefixObj.handler) return prefixObj.handler(t) | |
104 | ||
1a7d0887 C |
105 | if (prefixObj.isBoolean) { |
106 | if (t === 'true') return true | |
107 | if (t === 'false') return false | |
108 | ||
109 | return undefined | |
110 | } | |
111 | ||
feb34f6b C |
112 | return t |
113 | }) | |
1a7d0887 | 114 | .filter(t => t !== null && t !== undefined) |
feb34f6b C |
115 | |
116 | if (matchedTokens.length === 0) continue | |
117 | ||
118 | additionalFilters[prefixKey] = prefixObj.multiple === true | |
119 | ? matchedTokens | |
120 | : matchedTokens[0] | |
121 | } | |
122 | ||
123 | return { | |
1ebddadd | 124 | search: searchTokens.join(' ') || undefined, |
feb34f6b C |
125 | |
126 | ...additionalFilters | |
127 | } | |
128 | } | |
de59c48f | 129 | } |