33 lines
1.2 KiB
JavaScript
33 lines
1.2 KiB
JavaScript
const cds = require ('@sap/cds')
|
|
|
|
module.exports = cds.service.impl (async()=>{
|
|
|
|
// connect to requires services
|
|
const ReviewsService = await cds.connect.to ('sap.capire.reviews.ReviewsService')
|
|
const CatalogService = await cds.connect.to ('CatalogService')
|
|
const db = await cds.connect.to ('db')
|
|
|
|
// import model definitions from connected services to work with subsequently
|
|
const { Books } = db.entities
|
|
const { Reviews } = ReviewsService.entities
|
|
|
|
// delegate requests to read reviews to ReviewsService
|
|
CatalogService.impl (srv => {
|
|
srv.on ('READ', 'Books/reviews', (req) => {
|
|
const [ subject ] = req.params
|
|
const tx = ReviewsService.transaction (req)
|
|
return tx.run (SELECT.from (Reviews) .where ({subject}))
|
|
})
|
|
})
|
|
|
|
// react on event messages from reviews service
|
|
ReviewsService.on ('reviewed', (msg) => {
|
|
console.debug ('> received:', msg.event, msg.data)
|
|
const { subject, rating } = msg.data
|
|
const tx = db // TODO: db.transaction (msg)
|
|
return tx.run (UPDATE (Books, subject) .with ({rating}))
|
|
// return tx.update (Books, subject) .with ({rating})
|
|
})
|
|
|
|
})
|