Cosmetics
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using { sap.capire.bookshop as my } from '../db/schema';
|
using { sap.capire.bookshop as my } from '../db/schema';
|
||||||
|
|
||||||
@path:'/browse'
|
@path:'/browse'
|
||||||
|
// @impl: './cat-service.js'
|
||||||
service CatalogService {
|
service CatalogService {
|
||||||
|
|
||||||
@readonly entity Books as SELECT from my.Books {*,
|
@readonly entity Books as SELECT from my.Books {*,
|
||||||
|
|||||||
@@ -5,18 +5,17 @@ const { Books } = cds.entities
|
|||||||
module.exports = cds.service.impl(srv => {
|
module.exports = cds.service.impl(srv => {
|
||||||
srv.after ('READ', 'Books', each => each.stock > 111 && _addDiscount2(each,11))
|
srv.after ('READ', 'Books', each => each.stock > 111 && _addDiscount2(each,11))
|
||||||
srv.before ('CREATE', 'Orders', _reduceStock)
|
srv.before ('CREATE', 'Orders', _reduceStock)
|
||||||
// srv.before ('*', (req) => { console.debug ('>>>', req.method, req.target && req.target.name) })
|
// srv.before ('*', (req) => { console.debug ('>>>', req.method, req.target.name) })
|
||||||
})
|
})
|
||||||
|
|
||||||
/** Add some discount for overstocked books */
|
/** Add some discount for overstocked books */
|
||||||
function _addDiscount2 (each,discount) {
|
function _addDiscount2 (each,discount) {
|
||||||
each.title += ` -- ${discount}% discount!`
|
each.title += ` -- ${discount}% 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)})
|
// 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)
|
||||||
|
|||||||
Reference in New Issue
Block a user