Cosmetics

This commit is contained in:
Christian Georgi
2020-07-31 16:25:10 +02:00
committed by Christian Georgi
parent 4b78a8b637
commit ada05cf279

View File

@@ -6,14 +6,14 @@ module.exports = cds.service.impl(function() {
// Reduce stock of ordered books if available stock suffices // Reduce stock of ordered books if available stock suffices
this.before ('CREATE', 'Orders', (req) => { this.before ('CREATE', 'Orders', (req) => {
const { Items: OrderItems } = req.data const { Items: items } = req.data
return cds.transaction(req) .run (OrderItems.map (order => return cds.transaction(req) .run (items.map (item =>
UPDATE (Books) .where ('ID =', order.book_ID) UPDATE (Books) .where ('ID =', item.book_ID)
.and ('stock >=', order.amount) .and ('stock >=', item.amount)
.set ('stock -=', order.amount) .set ('stock -=', item.amount)
)) .then (all => all.forEach ((affectedRows,i) => { )) .then (all => all.forEach ((affectedRows,i) => {
if (affectedRows === 0) req.error (409, if (affectedRows === 0) req.error (409,
`${OrderItems[i].amount} exceeds stock for book #${OrderItems[i].book_ID}` `${items[i].amount} exceeds stock for book #${items[i].book_ID}`
) )
})) }))
}) })