feat: accept order by and order field
This commit is contained in:
parent
a6aa96a6f9
commit
4d3f75b88d
1 changed files with 8 additions and 2 deletions
|
|
@ -12,7 +12,7 @@ import {
|
||||||
Tags,
|
Tags,
|
||||||
Query,
|
Query,
|
||||||
} from "tsoa";
|
} from "tsoa";
|
||||||
import { Prisma, Status } from "@prisma/client";
|
import { Prisma, Product, Status } from "@prisma/client";
|
||||||
|
|
||||||
import prisma from "../db";
|
import prisma from "../db";
|
||||||
import { RequestWithUser } from "../interfaces/user";
|
import { RequestWithUser } from "../interfaces/user";
|
||||||
|
|
@ -128,6 +128,8 @@ export class ProductController extends Controller {
|
||||||
@Query() query: string = "",
|
@Query() query: string = "",
|
||||||
@Query() page: number = 1,
|
@Query() page: number = 1,
|
||||||
@Query() pageSize: number = 30,
|
@Query() pageSize: number = 30,
|
||||||
|
@Query() orderField?: keyof Product,
|
||||||
|
@Query() orderBy?: "asc" | "desc",
|
||||||
) {
|
) {
|
||||||
const where = {
|
const where = {
|
||||||
OR: queryOrNot<Prisma.ProductWhereInput[]>(query, [
|
OR: queryOrNot<Prisma.ProductWhereInput[]>(query, [
|
||||||
|
|
@ -182,7 +184,11 @@ export class ProductController extends Controller {
|
||||||
createdBy: true,
|
createdBy: true,
|
||||||
updatedBy: true,
|
updatedBy: true,
|
||||||
},
|
},
|
||||||
orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }],
|
orderBy: [
|
||||||
|
{ statusOrder: "asc" },
|
||||||
|
...((orderField && orderBy && [{ [orderField]: orderBy }]) || []),
|
||||||
|
{ createdAt: "asc" },
|
||||||
|
],
|
||||||
where,
|
where,
|
||||||
take: pageSize,
|
take: pageSize,
|
||||||
skip: (page - 1) * pageSize,
|
skip: (page - 1) * pageSize,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue