
module.exports = (srv) => {
const {Books} = cds.entities ('my.bookshop')
// Reduce stock of ordered books
srv.before ('CREATE', 'Orders', async (req) => {
const order = req.data
if (!order.amount || order.amount <= 0) return req.error (400, 'Order at least 1 book')
const tx = cds.transaction(req)
const affectedRows = await tx.run (
UPDATE (Books)
.set ({ stock: {'-=': order.amount}})
.where ({ stock: {'>=': order.amount},/*and*/ ID: order.book_ID})
)
if (affectedRows === 0) req.error (409, "Sold out, sorry")
})
// Add some discount for overstocked books
srv.after ('READ', 'Books', each => {
if (each.stock > 111) each.title += ' -- 11% discount!'
})
}
module.exports = (srv) => {
}
module.exports = (srv) => {
const {Books} = cds.entities ('my.bookshop')
}
module.exports = (srv) => {
const {Books} = cds.entities ('my.bookshop')
srv.before ('CREATE', 'Orders', async (req) => {
})
}
module.exports = (srv) => {
const {Books} = cds.entities ('my.bookshop')
srv.before ('CREATE', 'Orders', async (req) => {
const order = req.data
})
}
module.exports = (srv) => {
const {Books} = cds.entities ('my.bookshop')
srv.before ('CREATE', 'Orders', async (req) => {
const order = req.data
if (!order.amount || order.amount <= 0) return req.error (400, 'Order at least 1 book')
})
}
module.exports = (srv) => {
const {Books} = cds.entities ('my.bookshop')
srv.before ('CREATE', 'Orders', async (req) => {
const order = req.data
if (!order.amount || order.amount <= 0) return req.error (400, 'Order at least 1 book')
const tx = cds.transaction(req)
})
}
module.exports = (srv) => {
const {Books} = cds.entities ('my.bookshop')
srv.before ('CREATE', 'Orders', async (req) => {
const order = req.data
if (!order.amount || order.amount <= 0) return req.error (400, 'Order at least 1 book')
const tx = cds.transaction(req)
await tx.run (
UPDATE (Books)
.set ({ stock: {'-=': order.amount}})
.where ({ stock: {'>=': order.amount},/*and*/ ID: order.book_ID})
)
})
}
module.exports = (srv) => {
const {Books} = cds.entities ('my.bookshop')
srv.before ('CREATE', 'Orders', async (req) => {
const order = req.data
if (!order.amount || order.amount <= 0) return req.error (400, 'Order at least 1 book')
const tx = cds.transaction(req)
const affectedRows = await tx.run (
UPDATE (Books)
.set ({ stock: {'-=': order.amount}})
.where ({ stock: {'>=': order.amount},/*and*/ ID: order.book_ID})
)
if (affectedRows === 0) req.error (409, "Sold out, sorry")
})
}
module.exports = (srv) => {
const {Books} = cds.entities ('my.bookshop')
// Reduce stock of ordered books
srv.before ('CREATE', 'Orders', async (req) => {
const order = req.data
if (!order.amount || order.amount <= 0) return req.error (400, 'Order at least 1 book')
const tx = cds.transaction(req)
const affectedRows = await tx.run (
UPDATE (Books)
.set ({ stock: {'-=': order.amount}})
.where ({ stock: {'>=': order.amount},/*and*/ ID: order.book_ID})
)
if (affectedRows === 0) req.error (409, "Sold out, sorry")
})
srv.after ('READ', 'Books', each => {
})
}
module.exports = (srv) => {
const {Books} = cds.entities ('my.bookshop')
// Reduce stock of ordered books
srv.before ('CREATE', 'Orders', async (req) => {
const order = req.data
if (!order.amount || order.amount <= 0) return req.error (400, 'Order at least 1 book')
const tx = cds.transaction(req)
const affectedRows = await tx.run (
UPDATE (Books)
.set ({ stock: {'-=': order.amount}})
.where ({ stock: {'>=': order.amount},/*and*/ ID: order.book_ID})
)
if (affectedRows === 0) req.error (409, "Sold out, sorry")
})
srv.after ('READ', 'Books', each => {
if (each.stock > 111) each.title += ' -- 11% discount!'
})
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
13 | |
12 | |
11 | |
10 | |
10 | |
9 | |
8 | |
8 | |
8 | |
7 |