03f2696a5614178daa4aaec77e0d346d045faf75
[arvados-workbench2.git] / src / services / api / order-builder.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as _ from "lodash";
6 import { Resource } from "src/models/resource";
7
8 export enum OrderDirection { ASC, DESC }
9
10 export class OrderBuilder<T extends Resource = Resource> {
11
12     constructor(private order: string[] = []) {}
13
14     addOrder(direction: OrderDirection, attribute: keyof T, prefix?: string) {
15         this.order.push(`${prefix ? prefix + "." : ""}${_.snakeCase(attribute.toString())} ${direction === OrderDirection.ASC ? "asc" : "desc"}`);
16         return this;
17     }
18
19     addAsc(attribute: keyof T, prefix?: string) {
20         return this.addOrder(OrderDirection.ASC, attribute, prefix);
21     }
22
23     addDesc(attribute: keyof T, prefix?: string) {
24         return this.addOrder(OrderDirection.DESC, attribute, prefix);
25     }
26
27     getOrder() {
28         return this.order.join(",");
29     }
30 }