aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/rest
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/shared/rest')
-rw-r--r--client/src/app/shared/rest/rest.service.ts63
1 files changed, 59 insertions, 4 deletions
diff --git a/client/src/app/shared/rest/rest.service.ts b/client/src/app/shared/rest/rest.service.ts
index 5bd2b5e43..cd6db1f3c 100644
--- a/client/src/app/shared/rest/rest.service.ts
+++ b/client/src/app/shared/rest/rest.service.ts
@@ -1,10 +1,21 @@
1import { Injectable } from '@angular/core'
2import { HttpParams } from '@angular/common/http'
3import { SortMeta } from 'primeng/api' 1import { SortMeta } from 'primeng/api'
4import { ComponentPagination, ComponentPaginationLight } from './component-pagination.model' 2import { HttpParams } from '@angular/common/http'
5 3import { Injectable } from '@angular/core'
4import { ComponentPaginationLight } from './component-pagination.model'
6import { RestPagination } from './rest-pagination' 5import { RestPagination } from './rest-pagination'
7 6
7interface QueryStringFilterPrefixes {
8 [key: string]: {
9 prefix: string
10 handler?: (v: string) => string | number
11 multiple?: boolean
12 }
13}
14
15type ParseQueryStringFilterResult = {
16 [key: string]: string | number | (string | number)[]
17}
18
8@Injectable() 19@Injectable()
9export class RestService { 20export class RestService {
10 21
@@ -53,4 +64,48 @@ export class RestService {
53 64
54 return { start, count } 65 return { start, count }
55 } 66 }
67
68 parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes): ParseQueryStringFilterResult {
69 if (!q) return {}
70
71 // Tokenize the strings using spaces
72 const tokens = q.split(' ').filter(token => !!token)
73
74 // Build prefix array
75 const prefixeStrings = Object.values(prefixes)
76 .map(p => p.prefix)
77
78 // Search is the querystring minus defined filters
79 const searchTokens = tokens.filter(t => {
80 return prefixeStrings.every(prefixString => t.startsWith(prefixString) === false)
81 })
82
83 const additionalFilters: ParseQueryStringFilterResult = {}
84
85 for (const prefixKey of Object.keys(prefixes)) {
86 const prefixObj = prefixes[prefixKey]
87 const prefix = prefixObj.prefix
88
89 const matchedTokens = tokens.filter(t => t.startsWith(prefix))
90 .map(t => t.slice(prefix.length)) // Keep the value filter
91 .map(t => {
92 if (prefixObj.handler) return prefixObj.handler(t)
93
94 return t
95 })
96 .filter(t => !!t)
97
98 if (matchedTokens.length === 0) continue
99
100 additionalFilters[prefixKey] = prefixObj.multiple === true
101 ? matchedTokens
102 : matchedTokens[0]
103 }
104
105 return {
106 search: searchTokens.join(' '),
107
108 ...additionalFilters
109 }
110 }
56} 111}