Aligned content with latest capire docs

This commit is contained in:
Daniel
2020-03-29 17:22:04 +02:00
parent 251d07f38e
commit a8de029389
7 changed files with 60 additions and 25 deletions

View File

@@ -6,5 +6,5 @@ service CatalogService @(path:'/browse') {
} excluding { createdBy, modifiedBy };
@requires_: 'authenticated-user'
action order (book : Books.ID, amount: Integer);
action submitOrder (book : Books.ID, amount: Integer);
}

View File

@@ -1,23 +1,20 @@
const cds = require('@sap/cds')
module.exports = async function (){
/** Service implementation for CatalogService */
module.exports = cds.service.impl (function() {
// Get entity definitions from reflected model
const { Books } = cds.entities
// Add some discount for overstocked books
this.after ('READ', 'Books', each => {
if (each.stock > 111) each.title += ` -- 11% discount!`
})
const db = await cds.connect.to('db') // connect to database service
const { Books } = db.entities // get reflected definitions
// Reduce stock of ordered books if available stock suffices
this.on ('order', async (req) => {
const {UPDATE} = cds.ql(req), {book,amount} = req.data
this.on ('submitOrder', async req => {
const {book,amount} = req.data
const n = await UPDATE (Books, book)
.where ('stock >=', amount)
.set ('stock -=', amount)
.with ({ stock: {'-=': amount }})
.where ({ stock: {'>=': amount }})
n > 0 || req.error (409,`${amount} exceeds stock for book #${book}`)
})
})
// Add some discount for overstocked books
this.after ('READ','Books', each => {
if (each.stock > 111) each.title += ` -- 11% discount!`
})
}