Patch of _reduceStock() under @sap/cds 4 (#136)

* Bump to cds 4
* Cosmetics in handler code
  * await and cds.run([...]) instead of Promise.all

Co-authored-by: Christian Georgi <christian.georgi@sap.com>
This commit is contained in:
Laszlo Kajan
2020-09-18 10:49:46 +02:00
committed by GitHub
parent b5fc3fe4fa
commit 99861ca588
6 changed files with 513 additions and 409 deletions

2
.npmrc
View File

@@ -1 +1 @@
@sap:registry=https://npm.sap.com
registry=https://registry.npmjs.org

878
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -11,7 +11,7 @@
"bookshop": "cds watch packages/bookshop"
},
"dependencies": {
"@sap/cds": "^3",
"@sap/cds": "^4",
"express": "^4"
},
"devDependencies": {

View File

@@ -3,9 +3,9 @@
"version": "0.0.1",
"description": "",
"dependencies": {
"@sap/approuter": "^6"
"@sap/approuter": "^8"
},
"scripts": {
"start": "node node_modules/@sap/approuter/approuter.js"
}
}
}

View File

@@ -4,10 +4,10 @@
"description": "A simple bookshop application, build in a self-contained all-in-one fashion, i.e. w/o reusing other packages.",
"license": "SAP SAMPLE CODE LICENSE",
"dependencies": {
"@sap/cds": "^3",
"express": "^4",
"passport": "^0.4.1",
"hdb": "^0.17.1"
"@sap/cds": "^4",
"express": "^4",
"hdb": "^0.18.1",
"passport": "^0.4.1"
},
"scripts": {
"start": "cds run --in-memory?",

View File

@@ -14,16 +14,20 @@ function _addDiscount2 (each,discount) {
/** Reduce stock of ordered books if available stock suffices */
async function _reduceStock (req) {
const { Items: OrderItems } = req.data
return cds.transaction(req) .run (()=> OrderItems.map (order =>
UPDATE (Books) .set ('stock -=', order.amount)
.where ('ID =', order.book_ID) .and ('stock >=', order.amount)
)) .then (all => all.forEach ((affectedRows,i) => {
if (affectedRows === 0) req.error (409,
`${OrderItems[i].amount} exceeds stock for book #${OrderItems[i].book_ID}`
)
}))
const { Items: orderItems } = req.data
if (!Array.isArray(orderItems)) return
const all = await cds.transaction(req).run(orderItems.map(item =>
UPDATE(Books)
.set('stock -=', item.amount)
.where('ID =', item.book_ID).and('stock >=', item.amount)
))
all.forEach((affectedRows, i) => {
if (affectedRows === 0) {
req.error(409, `${orderItems[i].amount} exceeds stock for book #${orderItems[i].book_ID}`)
}
})
return all
}