Added samples overview
This commit is contained in:
24
reviewed/package.json
Normal file
24
reviewed/package.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "@capire/bookshop-with-reviews",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"@capire/bookshop": "*",
|
||||
"@capire/reviews": "*"
|
||||
},
|
||||
"scripts": {
|
||||
"watch": "cds watch"
|
||||
},
|
||||
"cds": {
|
||||
"requires": {
|
||||
"db": {
|
||||
"kind": "sql"
|
||||
},
|
||||
"sap.capire.reviews.ReviewsService": {
|
||||
"kind": "odata"
|
||||
},
|
||||
"messaging": {
|
||||
"kind": "file-based-messaging"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
13
reviewed/services.cds
Normal file
13
reviewed/services.cds
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace sap.capire.bookshop;
|
||||
using { sap.capire.reviews.ReviewsService as external } from '@capire/reviews';
|
||||
using { sap.capire.bookshop.Books } from '@capire/bookshop/db/schema';
|
||||
|
||||
// Extending Books by Reviews
|
||||
extend Books with {
|
||||
reviews : Composition of many external.Reviews on reviews.subject = ID;
|
||||
rating : external.Reviews.rating;
|
||||
}
|
||||
|
||||
using from '@capire/bookshop/srv/admin-service';
|
||||
using from '@capire/bookshop/srv/cat-service';
|
||||
annotate AdminService with @impl:'services.js';
|
||||
32
reviewed/services.js
Normal file
32
reviewed/services.js
Normal file
@@ -0,0 +1,32 @@
|
||||
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})
|
||||
})
|
||||
|
||||
})
|
||||
40
reviewed/tests.http
Normal file
40
reviewed/tests.http
Normal file
@@ -0,0 +1,40 @@
|
||||
#################################################
|
||||
#
|
||||
# Reviews Service
|
||||
#
|
||||
|
||||
|
||||
### Request to CatalogService > delegated to ReviewsService
|
||||
GET http://localhost:4004/browse/Books(201)/reviews
|
||||
|
||||
### Alternative OData URL
|
||||
GET http://localhost:4004/browse/Books/201/reviews
|
||||
|
||||
###
|
||||
GET http://localhost:4004/browse/Books(201)?
|
||||
&$select=ID,title,rating
|
||||
&$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", "rating":"5", "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", "rating":"5", "title":"boo"}
|
||||
Reference in New Issue
Block a user