From 9fbe0f44685e9c3c00f753bd36e858bd23c46680 Mon Sep 17 00:00:00 2001 From: Michal Klobukowski Date: Wed, 4 Jul 2018 17:26:15 +0200 Subject: [PATCH] Add new conditions and concatenation to FilterBuilder Feature #13703 Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski --- src/common/api/common-resource-service.ts | 2 +- src/common/api/filter-builder.ts | 51 ++++++++++++++----- .../collection-service/collection-service.ts | 2 +- src/services/groups-service/groups-service.ts | 2 +- 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/common/api/common-resource-service.ts b/src/common/api/common-resource-service.ts index 58bcaa5f..fe6c752c 100644 --- a/src/common/api/common-resource-service.ts +++ b/src/common/api/common-resource-service.ts @@ -90,7 +90,7 @@ export default class CommonResourceService { const { filters, order, ...other } = args; const params = { ...other, - filters: filters ? filters.get() : undefined, + filters: filters ? filters.serialize() : undefined, order: order ? order.getOrder() : undefined }; return this.serverApi diff --git a/src/common/api/filter-builder.ts b/src/common/api/filter-builder.ts index cc31f384..1f2107f1 100644 --- a/src/common/api/filter-builder.ts +++ b/src/common/api/filter-builder.ts @@ -6,22 +6,14 @@ import * as _ from "lodash"; import { Resource } from "./common-resource-service"; export default class FilterBuilder { - private filters = ""; - static create() { - return new FilterBuilder(); + static create(resourcePrefix = "") { + return new FilterBuilder(resourcePrefix); } - private addCondition(field: keyof T, cond: string, value?: string | string[], prefix: string = "", postfix: string = "") { - if (value) { - value = typeof value === "string" - ? `"${prefix}${value}${postfix}"` - : `["${value.join(`","`)}"]`; - - this.filters += `["${_.snakeCase(field.toString())}","${cond}",${value}]`; - } - return this; - } + constructor( + private resourcePrefix = "", + private filters = "") { } public addEqual(field: keyof T, value?: string) { return this.addCondition(field, "=", value); @@ -39,7 +31,38 @@ export default class FilterBuilder { return this.addCondition(field, "is_a", value); } - public get() { + public addIn(field: keyof T, value?: string | string[]) { + return this.addCondition(field, "in", value); + } + + public concat(filterBuilder: FilterBuilder) { + return new FilterBuilder(this.resourcePrefix, this.filters + this.getSeparator() + filterBuilder.getFilters()); + } + + public getFilters() { + return this.filters; + } + + public serialize() { return "[" + this.filters + "]"; } + + private addCondition(field: keyof T, cond: string, value?: string | string[], prefix: string = "", postfix: string = "") { + if (value) { + value = typeof value === "string" + ? `"${prefix}${value}${postfix}"` + : `["${value.join(`","`)}"]`; + + const resourcePrefix = this.resourcePrefix + ? _.snakeCase(this.resourcePrefix) + "." + : ""; + + this.filters += `${this.getSeparator()}["${resourcePrefix}${_.snakeCase(field.toString())}","${cond}",${value}]`; + } + return this; + } + + private getSeparator () { + return this.filters ? "," : ""; + } } diff --git a/src/services/collection-service/collection-service.ts b/src/services/collection-service/collection-service.ts index 8a1d6932..8412dea1 100644 --- a/src/services/collection-service/collection-service.ts +++ b/src/services/collection-service/collection-service.ts @@ -34,7 +34,7 @@ export default class CollectionService { const fb = new FilterBuilder(); fb.addLike("ownerUuid", parentUuid); return serverApi.get('/collections', { params: { - filters: fb.get() + filters: fb.serialize() }}).then(resp => { const collections = resp.data.items.map(g => ({ name: g.name, diff --git a/src/services/groups-service/groups-service.ts b/src/services/groups-service/groups-service.ts index aa39e64e..8c5fd8fa 100644 --- a/src/services/groups-service/groups-service.ts +++ b/src/services/groups-service/groups-service.ts @@ -37,7 +37,7 @@ export default class GroupsService extends CommonResourceService const { filters, order, ...other } = args; const params = { ...other, - filters: filters ? filters.get() : undefined, + filters: filters ? filters.serialize() : undefined, order: order ? order.getOrder() : undefined }; return this.serverApi -- 2.30.2