From 432b63b6e4853c9068f0f7774a38500a421b099e Mon Sep 17 00:00:00 2001 From: Patrick <50352812+Mueller-Patrick@users.noreply.github.com> Date: Sat, 17 Apr 2021 11:35:19 +0200 Subject: [PATCH] BETTERZON-70: Adding API endpoint to get details for a list of products (#30) --- .../src/models/products/products.router.ts | 19 +++++++++++++++ .../src/models/products/products.service.ts | 23 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/Backend/src/models/products/products.router.ts b/Backend/src/models/products/products.router.ts index f2f3353..03649de 100644 --- a/Backend/src/models/products/products.router.ts +++ b/Backend/src/models/products/products.router.ts @@ -69,6 +69,25 @@ productsRouter.get('/search/:term', async (req: Request, res: Response) => { } }); +// GET products/list/[1,2,3] + +productsRouter.get('/list/:ids', async (req: Request, res: Response) => { + const ids: [number] = JSON.parse(req.params.ids); + + if (!ids) { + res.status(400).send('Missing parameters.'); + return; + } + + try { + const products: Products = await ProductService.findList(ids); + + res.status(200).send(products); + } catch (e) { + res.status(404).send(e.message); + } +}); + // GET products/bestDeals diff --git a/Backend/src/models/products/products.service.ts b/Backend/src/models/products/products.service.ts index 8cca5b0..2c612e2 100644 --- a/Backend/src/models/products/products.service.ts +++ b/Backend/src/models/products/products.service.ts @@ -122,6 +122,29 @@ export const findBySearchTerm = async (term: string): Promise => { return prodRows; }; +export const findList = async (ids: [number]): Promise => { + let conn; + let prodRows = []; + try { + conn = await pool.getConnection(); + const rows = await conn.query('SELECT product_id, name, asin, is_active, short_description, long_description, image_guid, date_added, last_modified, manufacturer_id, selling_rank, category_id FROM products WHERE product_id IN (?)', [ids]); + for (let row in rows) { + if (row !== 'meta') { + prodRows.push(rows[row]); + } + } + + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return prodRows; +}; + // export const create = async (newItem: Product): Promise => { // let conn; // try {