Add new conditions and concatenation to FilterBuilder
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Wed, 4 Jul 2018 15:26:15 +0000 (17:26 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Wed, 4 Jul 2018 15:26:15 +0000 (17:26 +0200)
Feature #13703

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/common/api/common-resource-service.ts
src/common/api/filter-builder.ts
src/services/collection-service/collection-service.ts
src/services/groups-service/groups-service.ts

index 58bcaa5ff3c70bac428f61ae93db9f6dc60debd7..fe6c752c8ee1d4b2b42cc8d5bc64640ca8e93171 100644 (file)
@@ -90,7 +90,7 @@ export default class CommonResourceService<T extends Resource> {
         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
index cc31f384edd9e8a42021e48f66b509cd5b0dc069..1f2107f151cc9e374955ce8f0d40d8ab351c2843 100644 (file)
@@ -6,22 +6,14 @@ import * as _ from "lodash";
 import { Resource } from "./common-resource-service";
 
 export default class FilterBuilder<T extends Resource = Resource> {
-    private filters = "";
 
-    static create<T extends Resource = Resource>() {
-        return new FilterBuilder<T>();
+    static create<T extends Resource = Resource>(resourcePrefix = "") {
+        return new FilterBuilder<T>(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<T extends Resource = Resource> {
         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<O extends Resource>(filterBuilder: FilterBuilder<O>) {
+        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 ? "," : "";
+    }
 }
index 8a1d6932481534966771924a0dadc1f1e7af4cf6..8412dea1e5b74becf0eb58f8a45b25e1ba2e8b04 100644 (file)
@@ -34,7 +34,7 @@ export default class CollectionService {
             const fb = new FilterBuilder();
             fb.addLike("ownerUuid", parentUuid);
             return serverApi.get<CollectionsResponse>('/collections', { params: {
-                filters: fb.get()
+                filters: fb.serialize()
             }}).then(resp => {
                 const collections = resp.data.items.map(g => ({
                     name: g.name,
index aa39e64ed58ee8cccfebef071593cbebd39f4d78..8c5fd8fa6ce855d9a2cbc052eb924920431b1c9b 100644 (file)
@@ -37,7 +37,7 @@ export default class GroupsService extends CommonResourceService<GroupResource>
         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