cleaned up

This commit is contained in:
Daniel
2020-03-02 08:29:49 +01:00
parent cb066233c9
commit c6eb21ec51
26 changed files with 140 additions and 122 deletions

View File

@@ -0,0 +1,5 @@
using { sap.capire.bookshop as my } from '../db/schema';
service OrdersService {
entity Orders as projection on my.Orders;
}

View File

@@ -0,0 +1,21 @@
const cds = require('@sap/cds')
module.exports = cds.service.impl(function() {
const { Books } = cds.entities
// Reduce stock of ordered books if available stock suffices
this.before ('CREATE', 'Orders', (req) => {
const { Items: OrderItems } = req.data
return cds.transaction(req) .run (()=> OrderItems.map (order =>
UPDATE (Books) .where ('ID =', order.book_ID)
.and ('stock >=', order.amount)
.set ('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}`
)
}))
})
})