I've to apply filter using prisma as per the below condition. if ShowActiveOnly is true, then only show which are active if showActiveOnly is false, then show everything. Below is the snippet I've tried.
export const getAllProducts = async (req: Request, res: Response) => {
try {
const page: number = Number(req.query.page) - 1 || 0;
const perPage: number = Number(req.query.perPage) || 10;
const showActiveOnly: boolean =
(req.query.showActiveOnly || "true") === "true";
const products = await _db.serviceProduct.findMany({
where: { IsActive: showActiveOnly },
skip: page * 10,
take: perPage,
});
res.json({ products, code: 200 }).status(200);
} catch (error) {
HandleError(error, res);
}
};
export const getAllProducts = async (req: Request, res: Response) => {
try {
const page: number = Number(req.params.page) - 1 || 0;
const perPage: number = Number(req.params.perPage) || 10;
const showActiveOnly: boolean =
(req.query.showActiveOnly || "true") === "true";
const products = await _db.serviceProduct.findMany({
where: showActiveOnly ? { IsActive: true } : undefined,
skip: page * perPage,
take: perPage,
});
res.json({ products, code: 200 }).status(200);
} catch (error) {
HandleError(error, res);
}
};
we can do in this way. I've applied a condition in where clause in data extraction, if showactiveonly is true then we're getting only isActive true else where clause is undefined.