mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2024-11-22 14:23:57 +00:00
BETTERZON-38: Added ability to search for a product by its name
This commit is contained in:
parent
64cb6959cb
commit
3de28d8835
|
@ -36,6 +36,11 @@ productsRouter.get('/', async (req: Request, res: Response) => {
|
||||||
productsRouter.get('/:id', async (req: Request, res: Response) => {
|
productsRouter.get('/:id', async (req: Request, res: Response) => {
|
||||||
const id: number = parseInt(req.params.id, 10);
|
const id: number = parseInt(req.params.id, 10);
|
||||||
|
|
||||||
|
if(!id){
|
||||||
|
res.status(400).send('Missing parameters.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const product: Product = await ProductService.find(id);
|
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/
|
// POST items/
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,7 @@ export const findAll = async (): Promise<Products> => {
|
||||||
let prodRows = [];
|
let prodRows = [];
|
||||||
try {
|
try {
|
||||||
conn = await pool.getConnection();
|
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){
|
for(let row in rows){
|
||||||
if(row !== 'meta'){
|
if(row !== 'meta'){
|
||||||
prodRows.push(rows[row]);
|
prodRows.push(rows[row]);
|
||||||
|
@ -94,21 +94,61 @@ export const findAll = async (): Promise<Products> => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const find = async (id: number): Promise<Product> => {
|
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) {
|
} catch (err) {
|
||||||
return record;
|
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> => {
|
export const create = async (newItem: Product): Promise<void> => {
|
||||||
const product_id = new Date().valueOf();
|
let conn;
|
||||||
products[product_id] = {
|
try {
|
||||||
...newItem,
|
conn = await pool.getConnection();
|
||||||
product_id
|
// 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> => {
|
export const update = async (updatedItem: Product): Promise<void> => {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user