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" "bookshop": "cds watch packages/bookshop"
}, },
"dependencies": { "dependencies": {
"@sap/cds": "^3", "@sap/cds": "^4",
"express": "^4" "express": "^4"
}, },
"devDependencies": { "devDependencies": {

View File

@@ -3,7 +3,7 @@
"version": "0.0.1", "version": "0.0.1",
"description": "", "description": "",
"dependencies": { "dependencies": {
"@sap/approuter": "^6" "@sap/approuter": "^8"
}, },
"scripts": { "scripts": {
"start": "node node_modules/@sap/approuter/approuter.js" "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.", "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", "license": "SAP SAMPLE CODE LICENSE",
"dependencies": { "dependencies": {
"@sap/cds": "^3", "@sap/cds": "^4",
"express": "^4", "express": "^4",
"passport": "^0.4.1", "hdb": "^0.18.1",
"hdb": "^0.17.1" "passport": "^0.4.1"
}, },
"scripts": { "scripts": {
"start": "cds run --in-memory?", "start": "cds run --in-memory?",

View File

@@ -14,16 +14,20 @@ 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
return cds.transaction(req) .run (()=> OrderItems.map (order => if (!Array.isArray(orderItems)) return
UPDATE (Books) .set ('stock -=', order.amount)
.where ('ID =', order.book_ID) .and ('stock >=', order.amount) const all = await cds.transaction(req).run(orderItems.map(item =>
)) .then (all => all.forEach ((affectedRows,i) => { UPDATE(Books)
if (affectedRows === 0) req.error (409, .set('stock -=', item.amount)
`${OrderItems[i].amount} exceeds stock for book #${OrderItems[i].book_ID}` .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
} }