From 3de28d8835e23dcbc3cecac3f702e9165ef439d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Fri, 27 Nov 2020 19:01:01 +0100 Subject: [PATCH] BETTERZON-38: Added ability to search for a product by its name --- Backend/src/products/products.router.ts | 24 ++++++++++ Backend/src/products/products.service.ts | 60 ++++++++++++++++++++---- 2 files changed, 74 insertions(+), 10 deletions(-) diff --git a/Backend/src/products/products.router.ts b/Backend/src/products/products.router.ts index 6af5419..8501683 100644 --- a/Backend/src/products/products.router.ts +++ b/Backend/src/products/products.router.ts @@ -36,6 +36,11 @@ productsRouter.get('/', async (req: Request, res: Response) => { productsRouter.get('/:id', async (req: Request, res: Response) => { const id: number = parseInt(req.params.id, 10); + if(!id){ + res.status(400).send('Missing parameters.'); + return; + } + try { const product: Product = await ProductService.find(id); @@ -45,6 +50,25 @@ productsRouter.get('/:id', async (req: Request, res: Response) => { } }); +// GET items/:name + +productsRouter.get('/search/:term', async (req: Request, res: Response) => { + const term: string = req.params.term; + + if(!term){ + res.status(400).send('Missing parameters.'); + return; + } + + try { + const products: Products = await ProductService.findBySearchTerm(term); + + res.status(200).send(products); + } catch (e) { + res.status(404).send(e.message); + } +}); + // POST items/ diff --git a/Backend/src/products/products.service.ts b/Backend/src/products/products.service.ts index 16ee44b..0e3d7e3 100644 --- a/Backend/src/products/products.service.ts +++ b/Backend/src/products/products.service.ts @@ -77,7 +77,7 @@ export const findAll = async (): Promise => { let prodRows = []; try { conn = await pool.getConnection(); - const rows = await conn.query("SELECT product_id, name FROM products WHERE product_id = ?", 1); + const rows = await conn.query("SELECT product_id, name FROM products"); for(let row in rows){ if(row !== 'meta'){ prodRows.push(rows[row]); @@ -94,21 +94,61 @@ export const findAll = async (): Promise => { }; export const find = async (id: number): Promise => { - const record: Product = products[id]; + let conn; + let prod: any; + try { + conn = await pool.getConnection(); + const rows = await conn.query("SELECT product_id, name FROM products WHERE product_id = ?", id); + for(let row in rows){ + if(row !== 'meta'){ + prod = rows[row]; + } + } - if (record) { - return record; + } catch (err) { + throw err; + } finally { + if (conn) conn.end(); } - throw new Error("No record found"); + return prod; +}; + +export const findBySearchTerm = async (term: string): Promise => { + let conn; + let prodRows = []; + try { + conn = await pool.getConnection(); + term = '%' + term + '%'; + const rows = await conn.query("SELECT product_id, name FROM products WHERE name LIKE ?", term); + for(let row in rows){ + if(row !== 'meta'){ + prodRows.push(rows[row]); + } + } + + } catch (err) { + console.log(err); + throw err; + } finally { + if (conn) conn.end(); + } + + return prodRows; }; export const create = async (newItem: Product): Promise => { - const product_id = new Date().valueOf(); - products[product_id] = { - ...newItem, - product_id - }; + let conn; + try { + conn = await pool.getConnection(); + // TODO adjust query + await conn.query("SELECT product_id, name FROM products WHERE product_id = ?"); + + } catch (err) { + throw err; + } finally { + if (conn) conn.end(); + } }; export const update = async (updatedItem: Product): Promise => {