Obtain configuration from discovery endpoint
[arvados.git] / src / common / api / filter-builder.ts
index 3f8e323c40f212f387fe7fcece7b78d0e6cc5e87..b5558dbb16a291f8ecb81fdbd642f742bb99b9d3 100644 (file)
@@ -2,34 +2,51 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-export enum FilterField {
-    UUID = "uuid",
-    OWNER_UUID = "owner_uuid"
+import * as _ from "lodash";
+
+export function joinFilters(filters0?: string, filters1?: string) {
+    return [filters0, filters1].filter(s => s).join(",");
 }
 
-export default class FilterBuilder {
-    private filters = "";
+export class FilterBuilder {
+    constructor(private filters = "") { }
 
-    private addCondition(field: FilterField, cond: string, value?: string) {
-        if (value) {
-            this.filters += `["${field}","${cond}","${value}"]`;
-        }
-        return this;
+    public addEqual(field: string, value?: string, resourcePrefix?: string) {
+        return this.addCondition(field, "=", value, "", "", resourcePrefix );
     }
 
-    public addEqual(field: FilterField, value?: string) {
-        return this.addCondition(field, "=", value);
+    public addLike(field: string, value?: string, resourcePrefix?: string) {
+        return this.addCondition(field, "like", value, "%", "%", resourcePrefix);
     }
 
-    public addLike(field: FilterField, value?: string) {
-        return this.addCondition(field, "like", value);
+    public addILike(field: string, value?: string, resourcePrefix?: string) {
+        return this.addCondition(field, "ilike", value, "%", "%", resourcePrefix);
     }
 
-    public addILike(field: FilterField, value?: string) {
-        return this.addCondition(field, "ilike", value);
+    public addIsA(field: string, value?: string | string[], resourcePrefix?: string) {
+        return this.addCondition(field, "is_a", value, "", "", resourcePrefix);
     }
 
-    public get() {
-        return "[" + this.filters + "]";
+    public addIn(field: string, value?: string | string[], resourcePrefix?: string) {
+        return this.addCondition(field, "in", value, "", "", resourcePrefix);
+    }
+
+    public getFilters() {
+        return this.filters;
+    }
+
+    private addCondition(field: string, cond: string, value?: string | string[], prefix: string = "", postfix: string = "", resourcePrefix?: string) {
+        if (value) {
+            value = typeof value === "string"
+                ? `"${prefix}${value}${postfix}"`
+                : `["${value.join(`","`)}"]`;
+
+            const resPrefix = resourcePrefix
+                ? _.snakeCase(resourcePrefix) + "."
+                : "";
+
+            this.filters += `${this.filters ? "," : ""}["${resPrefix}${_.snakeCase(field)}","${cond}",${value}]`;
+        }
+        return this;
     }
 }