mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2024-12-22 19:55:12 +00:00
BETTERZON-38: Adjusted prices API
This commit is contained in:
parent
acdc9a4e92
commit
6338060b78
|
@ -23,7 +23,20 @@ export const pricesRouter = express.Router();
|
|||
|
||||
pricesRouter.get('/', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const prices: Prices = await PriceService.findAll();
|
||||
let prices: Prices = [];
|
||||
const product = req.query.product;
|
||||
const vendor = req.query.vendor;
|
||||
const type = req.query.type;
|
||||
|
||||
if (product) {
|
||||
if (vendor) {
|
||||
prices = await PriceService.findByVendor(<string> product, <string> vendor, <string> type);
|
||||
} else {
|
||||
prices = await PriceService.findByType(<string> product, <string> type);
|
||||
}
|
||||
} else {
|
||||
prices = await PriceService.findAll();
|
||||
}
|
||||
|
||||
res.status(200).send(prices);
|
||||
} catch (e) {
|
||||
|
@ -50,26 +63,6 @@ pricesRouter.get('/:id', async (req: Request, res: Response) => {
|
|||
}
|
||||
});
|
||||
|
||||
// GET items/:name
|
||||
|
||||
pricesRouter.get('/products/: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 prices: Prices = await PriceService.findByProduct(id);
|
||||
|
||||
res.status(200).send(prices);
|
||||
} catch (e) {
|
||||
res.status(404).send(e.message);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// POST items/
|
||||
|
||||
// pricesRouter.post('/', async (req: Request, res: Response) => {
|
||||
|
|
|
@ -106,6 +106,86 @@ export const findByProduct = async (product: number): Promise<Prices> => {
|
|||
return priceRows;
|
||||
};
|
||||
|
||||
export const findByType = async (product: string, type: string): Promise<Prices> => {
|
||||
let conn;
|
||||
let priceRows = [];
|
||||
try {
|
||||
conn = await pool.getConnection();
|
||||
let rows = [];
|
||||
if (type === 'newest') {
|
||||
// Used to get the newest price for this product per vendor
|
||||
rows = await conn.query(('WITH summary AS ( ' +
|
||||
'SELECT p.product_id, ' +
|
||||
'p.vendor_id, ' +
|
||||
'p.price_in_cents, ' +
|
||||
'p.timestamp, ' +
|
||||
'ROW_NUMBER() OVER( ' +
|
||||
'PARTITION BY p.vendor_id ' +
|
||||
'ORDER BY p.timestamp DESC) AS rk ' +
|
||||
'FROM prices p ' +
|
||||
'WHERE product_id = ?) ' +
|
||||
'SELECT s.* ' +
|
||||
'FROM summary s ' +
|
||||
'WHERE s.rk = 1 '), product);
|
||||
} else if (type === 'lowest') {
|
||||
// Used to get the lowest prices for this product over a period of time
|
||||
rows = await conn.query('SELECT price_id, product_id, vendor_id, MIN(price_in_cents) as price_in_cents, timestamp FROM prices WHERE product_id = ? GROUP BY DAY(timestamp) ORDER BY timestamp', product);
|
||||
} else {
|
||||
// If no type is given, return all prices for this product
|
||||
rows = await conn.query('SELECT price_id, product_id, vendor_id, price_in_cents, timestamp FROM prices WHERE product_id = ?', product);
|
||||
}
|
||||
|
||||
for (let row in rows) {
|
||||
if (row !== 'meta') {
|
||||
priceRows.push(rows[row]);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) {
|
||||
conn.end();
|
||||
}
|
||||
}
|
||||
|
||||
return priceRows;
|
||||
};
|
||||
|
||||
export const findByVendor = async (product: string, vendor: string, type: string): Promise<Prices> => {
|
||||
let conn;
|
||||
let priceRows = [];
|
||||
try {
|
||||
conn = await pool.getConnection();
|
||||
let rows = [];
|
||||
if (type === 'newest') {
|
||||
// Used to get the newest price for this product and vendor
|
||||
rows = await conn.query('SELECT price_id, product_id, vendor_id, price_in_cents, timestamp FROM prices WHERE product_id = ? AND vendor_id = ? ORDER BY timestamp DESC LIMIT 1', [product, vendor]);
|
||||
} else if (type === 'lowest') {
|
||||
// Used to get the lowest prices for this product and vendor in all time
|
||||
rows = await conn.query('SELECT price_id, product_id, vendor_id, MIN(price_in_cents) as price_in_cents, timestamp FROM prices WHERE product_id = ? AND vendor_id = ? LIMIT 1', [product, vendor]);
|
||||
} else {
|
||||
// If no type is given, return all prices for this product and vendor
|
||||
rows = await conn.query('SELECT price_id, product_id, vendor_id, price_in_cents, timestamp FROM prices WHERE product_id = ? AND vendor_id = ?', [product, vendor]);
|
||||
}
|
||||
|
||||
for (let row in rows) {
|
||||
if (row !== 'meta') {
|
||||
priceRows.push(rows[row]);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) {
|
||||
conn.end();
|
||||
}
|
||||
}
|
||||
|
||||
return priceRows;
|
||||
};
|
||||
|
||||
// export const create = async (newItem: Product): Promise<void> => {
|
||||
// let conn;
|
||||
// try {
|
||||
|
|
Loading…
Reference in New Issue
Block a user