final state of exercise 1

This commit is contained in:
d045778
2019-09-23 13:19:24 +02:00
parent 2fc2cea260
commit 1e28cb217f
12 changed files with 124 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
namespace sap.capire.bookstore;
using { sap.capire.products.Products, sap.capire.bookstore as my } from '../db/schema';
service CatalogService {
@readonly entity Books as projection on Products {
*, category.name as category, author.name
} excluding { createdBy, modifiedBy };
@insertonly entity Orders as projection on my.Orders;
}
// Reuse services from @sap/capire-products...
using { sap.capire.products.AdminService } from '@sap/capire-products';
extend service AdminService with {
entity Authors as projection on my.Authors;
}

View File

@@ -0,0 +1,18 @@
const cds = require('@sap/cds')
module.exports = async (srv) => {
const { Books } = srv.entities
// Check all amounts against stock before activating
srv.before(['CREATE', 'UPDATE'], 'Orders', (req) => {
const tx = cds.transaction(req), order = req.data
return Promise.all(order.Items.map(each => tx.run(
UPDATE(Books).where({ ID: each.book_ID })
.and(`stock >=`, each.amount)
.set(`stock -=`, each.amount)
).then(affectedRows => {
if (!affectedRows) {
req.error(409, `${each.amount} exceeds stock for book #${each.book_ID}`)
}
})))
})
}