Added reviews/test/bookshop

This commit is contained in:
Daniel
2020-03-08 12:42:26 +01:00
parent 737027ded4
commit 84a815e7e6
10 changed files with 64 additions and 101 deletions

View File

@@ -0,0 +1,23 @@
{
"name": "@capire/bookshop-with-reviews",
"version": "1.0.0",
"dependencies": {
"@capire/bookshop": "*",
"@capire/reviews": "*",
"@sap/cds": "^3.31.1",
"express": "^4.17.1"
},
"cds": {
"requires": {
"db": {
"kind": "sql"
},
"ReviewsService": {
"kind": "odata"
},
"messaging": {
"kind": "file-based-messaging"
}
}
}
}

View File

@@ -0,0 +1,13 @@
//
// Extending Books with Reviews
//
using { sap.capire.bookshop.Books } from '@capire/bookshop';
using { ReviewsService.Reviews } from '@capire/reviews';
extend Books with {
/** Access to detailed collection of Reviews */
reviews : Composition of many Reviews on reviews.subject = $self.ID;
/** Average rating */
rating : Reviews.rating;
}

View File

@@ -0,0 +1,35 @@
const cds = require ('@sap/cds')
cds.on('listening', async()=>{
// connect to requires services
const ReviewsService = await cds.connect.to ('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 { Reviews } = ReviewsService.entities
const { Books } = db.entities
// 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})
})
console.log (Reviews.name)
// 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}))
})
})
})
module.exports = cds.server