BETTERZON-70: Adding API endpoint to get details for a list of products (#30)

This commit is contained in:
Patrick 2021-04-17 11:35:19 +02:00 committed by GitHub
parent 57962a7973
commit 432b63b6e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

View File

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

View File

@ -122,6 +122,29 @@ export const findBySearchTerm = async (term: string): Promise<Products> => {
return prodRows;
};
export const findList = async (ids: [number]): Promise<Products> => {
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<void> => {
// let conn;
// try {