Added reviews/test/bookshop
This commit is contained in:
23
reviews/test/bookshop/package.json
Normal file
23
reviews/test/bookshop/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
13
reviews/test/bookshop/schema.cds
Normal file
13
reviews/test/bookshop/schema.cds
Normal 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;
|
||||
}
|
||||
35
reviews/test/bookshop/server.js
Normal file
35
reviews/test/bookshop/server.js
Normal 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
|
||||
@@ -1,6 +1,6 @@
|
||||
const _model = __dirname+'/..'
|
||||
const cds = require ('@sap/cds')
|
||||
const {expect} = require('chai').use(require('chai-subset'))
|
||||
const {expect} = cds.require.chai
|
||||
|
||||
describe('messaging tests', ()=>{
|
||||
|
||||
|
||||
@@ -4,21 +4,6 @@
|
||||
#
|
||||
|
||||
|
||||
### Use this one for ReviewsService running as a separate process
|
||||
# Note: use 5005 instead of 4004 in case of separate service
|
||||
POST http://localhost:5005/reviews/Reviews
|
||||
# POST http://localhost:4004/reviews/Reviews
|
||||
Content-Type: application/json;IEEE754Compatible=true
|
||||
|
||||
{"subject":"201", "rating":"4", "title":"boo"}
|
||||
|
||||
### Direct Request to ReviewsService
|
||||
# Note: use 5005 instead of 4004 in case of separate service
|
||||
GET http://localhost:5005/reviews/Reviews?
|
||||
# GET http://localhost:4004/reviews/Reviews?
|
||||
# &$filter=subject eq '201'
|
||||
|
||||
|
||||
### Request to CatalogService > delegated to ReviewsService
|
||||
GET http://localhost:4004/browse/Books(201)/reviews
|
||||
|
||||
@@ -28,5 +13,28 @@ GET http://localhost:4004/browse/Books/201/reviews
|
||||
###
|
||||
GET http://localhost:4004/browse/Books(201)?
|
||||
&$select=ID,title,rating
|
||||
# &$expand=reviews
|
||||
&$expand=reviews
|
||||
# Note: the latter only works in case of ReviewsService in same process
|
||||
|
||||
|
||||
|
||||
### ReviewsService mocked in same process
|
||||
GET http://localhost:4004/reviews/Reviews?
|
||||
|
||||
###
|
||||
POST http://localhost:4004/reviews/Reviews
|
||||
Content-Type: application/json;IEEE754Compatible=true
|
||||
|
||||
{"subject":"201", "title":"boo"}
|
||||
|
||||
|
||||
|
||||
|
||||
### ReviewsService running as separate process
|
||||
GET http://localhost:5005/reviews/Reviews?
|
||||
|
||||
###
|
||||
POST http://localhost:5005/reviews/Reviews
|
||||
Content-Type: application/json;IEEE754Compatible=true
|
||||
|
||||
{"subject":"201", "title":"boo"}
|
||||
|
||||
Reference in New Issue
Block a user