//
// SPDX-License-Identifier: AGPL-3.0
+import * as _ from "lodash";
+import { Resource } from "~/models/resource";
-export default class OrderBuilder {
- private order: string[] = [];
+export enum OrderDirection { ASC, DESC }
- addAsc(attribute: string) {
- this.order.push(`${attribute} asc`);
+export class OrderBuilder<T extends Resource = Resource> {
+
+ constructor(private order: string[] = []) {}
+
+ addOrder(direction: OrderDirection, attribute: keyof T, prefix?: string) {
+ this.order.push(`${prefix ? prefix + "." : ""}${_.snakeCase(attribute.toString())} ${direction === OrderDirection.ASC ? "asc" : "desc"}`);
return this;
}
- addDesc(attribute: string) {
- this.order.push(`${attribute} desc`);
- return this;
+ addAsc(attribute: keyof T, prefix?: string) {
+ return this.addOrder(OrderDirection.ASC, attribute, prefix);
+ }
+
+ addDesc(attribute: keyof T, prefix?: string) {
+ return this.addOrder(OrderDirection.DESC, attribute, prefix);
}
- get() {
- return this.order;
+ getOrder() {
+ return this.order.join(",");
}
}