Added reviews/test/bookshop
This commit is contained in:
@@ -1,13 +0,0 @@
|
|||||||
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';
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
#################################################
|
|
||||||
#
|
|
||||||
# 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", "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"}
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using { sap.capire.reviews as my } from '../db/schema';
|
using { sap.capire.reviews as my } from '../db/schema';
|
||||||
namespace sap.capire.reviews;
|
|
||||||
|
|
||||||
service ReviewsService {
|
service ReviewsService {
|
||||||
// Sync API
|
// Sync API
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ module.exports = cds.service.impl (function(){
|
|||||||
// ( Note: we explicitly specify the namespace to support embedded reuse )
|
// ( Note: we explicitly specify the namespace to support embedded reuse )
|
||||||
const { Reviews, Likes } = this.entities ('sap.capire.reviews')
|
const { Reviews, Likes } = this.entities ('sap.capire.reviews')
|
||||||
|
|
||||||
// this.before (['CREATE','UPDATE'], 'Reviews', req => {
|
this.before (['CREATE','UPDATE'], 'Reviews', req => {
|
||||||
// if (!req.data.rating) req.data.rating = Math.round(Math.random()*4)+1
|
if (!req.data.rating) req.data.rating = Math.round(Math.random()*4)+1
|
||||||
// })
|
})
|
||||||
|
|
||||||
// Emit an event to inform subscribers about new avg ratings for reviewed subjects
|
// Emit an event to inform subscribers about new avg ratings for reviewed subjects
|
||||||
// ( Note: req.on.succeeded ensures we only do that if there's no error )
|
// ( Note: req.on.succeeded ensures we only do that if there's no error )
|
||||||
@@ -17,7 +17,7 @@ module.exports = cds.service.impl (function(){
|
|||||||
SELECT.one (['round(avg(rating),2) as rating']) .from (Reviews) .where ({subject})
|
SELECT.one (['round(avg(rating),2) as rating']) .from (Reviews) .where ({subject})
|
||||||
)
|
)
|
||||||
req.on ('succeeded', ()=>{
|
req.on ('succeeded', ()=>{
|
||||||
console.log ('< emitting:', 'reviewed', { subject, rating })
|
global.it || console.log ('< emitting:', 'reviewed', { subject, rating })
|
||||||
this.emit ('reviewed', { subject, rating })
|
this.emit ('reviewed', { subject, rating })
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,17 +3,16 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@capire/bookshop": "*",
|
"@capire/bookshop": "*",
|
||||||
"@capire/reviews": "*"
|
"@capire/reviews": "*",
|
||||||
},
|
"@sap/cds": "^3.31.1",
|
||||||
"scripts": {
|
"express": "^4.17.1"
|
||||||
"watch": "cds watch"
|
|
||||||
},
|
},
|
||||||
"cds": {
|
"cds": {
|
||||||
"requires": {
|
"requires": {
|
||||||
"db": {
|
"db": {
|
||||||
"kind": "sql"
|
"kind": "sql"
|
||||||
},
|
},
|
||||||
"sap.capire.reviews.ReviewsService": {
|
"ReviewsService": {
|
||||||
"kind": "odata"
|
"kind": "odata"
|
||||||
},
|
},
|
||||||
"messaging": {
|
"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;
|
||||||
|
}
|
||||||
@@ -1,24 +1,15 @@
|
|||||||
const cds = require ('@sap/cds')
|
const cds = require ('@sap/cds')
|
||||||
|
|
||||||
module.exports = cds.service.impl (async()=>{
|
cds.on('listening', async()=>{
|
||||||
|
|
||||||
// connect to requires services
|
// connect to requires services
|
||||||
const ReviewsService = await cds.connect.to ('sap.capire.reviews.ReviewsService')
|
const ReviewsService = await cds.connect.to ('ReviewsService')
|
||||||
const CatalogService = await cds.connect.to ('CatalogService')
|
const CatalogService = await cds.connect.to ('CatalogService')
|
||||||
const db = await cds.connect.to ('db')
|
const db = await cds.connect.to ('db')
|
||||||
|
|
||||||
// import model definitions from connected services to work with subsequently
|
// import model definitions from connected services to work with subsequently
|
||||||
const { Books } = db.entities
|
|
||||||
const { Reviews } = ReviewsService.entities
|
const { Reviews } = ReviewsService.entities
|
||||||
|
const { Books } = db.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
|
// react on event messages from reviews service
|
||||||
ReviewsService.on ('reviewed', (msg) => {
|
ReviewsService.on ('reviewed', (msg) => {
|
||||||
@@ -29,4 +20,16 @@ module.exports = cds.service.impl (async()=>{
|
|||||||
// return tx.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 _model = __dirname+'/..'
|
||||||
const cds = require ('@sap/cds')
|
const cds = require ('@sap/cds')
|
||||||
const {expect} = require('chai').use(require('chai-subset'))
|
const {expect} = cds.require.chai
|
||||||
|
|
||||||
describe('messaging tests', ()=>{
|
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
|
### Request to CatalogService > delegated to ReviewsService
|
||||||
GET http://localhost:4004/browse/Books(201)/reviews
|
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)?
|
GET http://localhost:4004/browse/Books(201)?
|
||||||
&$select=ID,title,rating
|
&$select=ID,title,rating
|
||||||
# &$expand=reviews
|
&$expand=reviews
|
||||||
# Note: the latter only works in case of ReviewsService in same process
|
# 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"}
|
||||||
|
|||||||
10
samples.md
10
samples.md
@@ -40,20 +40,14 @@ Each sub directory essentially is a individual npm package arranged in an [all-i
|
|||||||
## [reviews](reviews)
|
## [reviews](reviews)
|
||||||
|
|
||||||
- Shows how to implement a modular service to manage product reviews, including...
|
- Shows how to implement a modular service to manage product reviews, including...
|
||||||
|
- Consuming other services synchronously and asynchronously
|
||||||
- Serving requests synchronously
|
- Serving requests synchronously
|
||||||
- Emitting events asynchronously
|
- Emitting events asynchronously
|
||||||
- As well as managed data, input validations and authorization
|
|
||||||
|
|
||||||
|
|
||||||
## [reviewed](reviewed)
|
|
||||||
|
|
||||||
- Enhances [bookshop](#bookshop) with [reviews](#reviews), thereby showcasing...
|
|
||||||
- Consuming other services synchronously
|
|
||||||
- As well as asynchronously, subscribing to events
|
|
||||||
- Grow as you go, with...
|
- Grow as you go, with...
|
||||||
- Mocking app services
|
- Mocking app services
|
||||||
- Running service meshes
|
- Running service meshes
|
||||||
- Late-cut Micro Services
|
- Late-cut Micro Services
|
||||||
|
- As well as managed data, input validations and authorization
|
||||||
|
|
||||||
|
|
||||||
## [fiori](fiori)
|
## [fiori](fiori)
|
||||||
|
|||||||
Reference in New Issue
Block a user