BETTERZON-38: Added ability to search for a product by its name

This commit is contained in:
Patrick Müller 2020-11-27 19:01:01 +01:00
parent 64cb6959cb
commit 3de28d8835
2 changed files with 74 additions and 10 deletions

View File

@ -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/

View File

@ -77,7 +77,7 @@ export const findAll = async (): Promise<Products> => {
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<Products> => {
};
export const find = async (id: number): Promise<Product> => {
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<Products> => {
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<void> => {
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<void> => {