Separated reviewed and reviews

This commit is contained in:
Daniel
2020-05-29 16:56:03 +02:00
parent 0a552b4346
commit 6f9737ae38
6 changed files with 25 additions and 30 deletions

View File

@@ -2,8 +2,8 @@
"name": "@capire/bookshop-with-reviews",
"version": "1.0.0",
"dependencies": {
"@capire/bookshop": "../../../bookshop",
"@capire/reviews": "..",
"@capire/bookshop": "../bookshop",
"@capire/reviews": "../reviews",
"@sap/cds": "^3.33.1",
"express": "^4.17.1"
},
@@ -20,4 +20,4 @@
}
}
}
}
}

View File

@@ -12,9 +12,13 @@ cds.once('served', async()=>{
// react on event messages from reviews service
const ReviewsService = await cds.connect.to ('ReviewsService')
const db = await cds.connect.to ('db')
// reflect entities required below...
const { Books } = db.entities('sap.capire.bookshop')
const { Reviews } = ReviewsService.entities
ReviewsService.on ('reviewed', (msg) => {
console.debug ('> received:', msg.event, msg.data)
const { Books } = db.entities('sap.capire.bookshop')
const { subject, rating } = msg.data
const tx = db.tx (msg) // TODO: db.tx(msg) fully implemented?
return tx.update (Books,subject) .with ({rating})
@@ -24,10 +28,10 @@ cds.once('served', async()=>{
const CatalogService = await cds.connect.to ('CatalogService')
CatalogService.impl (srv => srv.on ('READ', 'Books/reviews', (req) => {
console.debug ('> delegating to ReviewsService')
const { Reviews } = ReviewsService.entities
const [ subject ] = req.params
const tx = ReviewsService.tx (req)
return tx.read (Reviews) .where({subject}) .columns (req.query.SELECT.columns)
const [ id ] = req.params
const tx = ReviewsService.tx(req)
return tx.read (Reviews) .where ({ subject: String(id) })
.columns (req.query.SELECT.columns)
}))
})

View File

@@ -1,13 +1,16 @@
#################################################
#
# To ReviewsService mocked in bookshop process
# To ReviewsService
#
# move the right down:
@reviews-service = http://localhost:4004/reviews
@reviews-service = http://localhost:5005/reviews
GET http://localhost:4004/reviews/Reviews?
### Get all reviews
GET {{reviews-service}}/Reviews
###
POST http://localhost:4004/reviews/Reviews
### Add a new review (with random rating)
POST {{reviews-service}}/Reviews
Content-Type: application/json;IEEE754Compatible=true
{"subject":"201", "title":"boo"}
@@ -20,17 +23,18 @@ Content-Type: application/json;IEEE754Compatible=true
# (both in-process as well as separate one)
#
@bookshop = http://localhost:4004
### Request to CatalogService > delegated to ReviewsService
GET http://localhost:4004/browse/Books(201)/reviews?
GET {{bookshop}}/browse/Books(201)/reviews?
&$select=rating,date,reviewer,title
### Alternative OData URL
GET http://localhost:4004/browse/Books/201/reviews?
GET {{bookshop}}/browse/Books/201/reviews?
&$select=rating,date,reviewer,title
###
GET http://localhost:4004/browse/Books(201)?
GET {{bookshop}}/browse/Books(201)?
&$select=ID,title,rating
&$expand=reviews
# Note: the $expand only works in case of ReviewsService in same process

View File

@@ -12,7 +12,7 @@
},
"scripts": {
"reviews-service": "cds watch",
"bookshop": "cds watch test/bookshop"
"books-reviewed": "cds watch ../reviewed"
},
"cds": {
"requires": {

View File

@@ -1,13 +0,0 @@
#################################################
#
# To 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"}