From ac7ebbbf5e5104183d803ceba3b1e178beb979be Mon Sep 17 00:00:00 2001 From: Spacelord Date: Tue, 27 Jun 2023 20:56:04 +0200 Subject: [PATCH] Added api search route for sku --- src/routes/api/v1/search/sku.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/routes/api/v1/search/sku.ts diff --git a/src/routes/api/v1/search/sku.ts b/src/routes/api/v1/search/sku.ts new file mode 100644 index 0000000..cdc3d36 --- /dev/null +++ b/src/routes/api/v1/search/sku.ts @@ -0,0 +1,30 @@ +import { Request, Response } from 'express'; +import { prisma, __path, log } from '../../../../index.js'; + +// Get item. +function get(req: Request, res: Response) { + if (!req.query.sku) { + res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' }); + return; + } + prisma.item + .findMany({ + where: { + SKU: { + contains: req.query.sku.toString() + } + }, + include: { + category: true + } + }) + .then((items) => { + res.status(200).json(JSON.stringify(items)); + }) + .catch((err) => { + log.db.error(err); + res.status(500).json({ errorcode: 'DB_ERROR', error: err }); + }); +} + +export default { get };