More handlers for demo
This commit is contained in:
@@ -2,9 +2,10 @@ const cds = require('@sap/cds')
|
|||||||
const { Books } = cds.entities
|
const { Books } = cds.entities
|
||||||
|
|
||||||
/** Service implementation for CatalogService */
|
/** Service implementation for CatalogService */
|
||||||
module.exports = cds.service.impl(function() {
|
module.exports = cds.service.impl(srv => {
|
||||||
this.after ('READ', 'Books', each => each.stock > 111 && _addDiscount2(each,11))
|
srv.after ('READ', 'Books', each => each.stock > 111 && _addDiscount2(each,11))
|
||||||
this.before ('CREATE', 'Orders', _reduceStock)
|
srv.before ('CREATE', 'Orders', _reduceStock)
|
||||||
|
// srv.before ('*', (req) => { console.debug ('>>>', req.method, req.target && req.target.name) })
|
||||||
})
|
})
|
||||||
|
|
||||||
/** Add some discount for overstocked books */
|
/** Add some discount for overstocked books */
|
||||||
@@ -15,6 +16,8 @@ function _addDiscount2 (each,discount) {
|
|||||||
/** Reduce stock of ordered books if available stock suffices */
|
/** Reduce stock of ordered books if available stock suffices */
|
||||||
async function _reduceStock (req) {
|
async function _reduceStock (req) {
|
||||||
const { Items: OrderItems } = req.data
|
const { Items: OrderItems } = req.data
|
||||||
|
// req.on('failed', () => {console.debug ('>>> failed for order', req.data.ID)})
|
||||||
|
|
||||||
return cds.transaction(req) .run (()=> OrderItems.map (order =>
|
return cds.transaction(req) .run (()=> OrderItems.map (order =>
|
||||||
UPDATE (Books) .set ('stock -=', order.amount)
|
UPDATE (Books) .set ('stock -=', order.amount)
|
||||||
.where ('ID =', order.book_ID) .and ('stock >=', order.amount)
|
.where ('ID =', order.book_ID) .and ('stock >=', order.amount)
|
||||||
|
|||||||
Reference in New Issue
Block a user