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';
|
||||
namespace sap.capire.reviews;
|
||||
|
||||
service ReviewsService {
|
||||
// Sync API
|
||||
|
||||
@@ -5,9 +5,9 @@ module.exports = cds.service.impl (function(){
|
||||
// ( Note: we explicitly specify the namespace to support embedded reuse )
|
||||
const { Reviews, Likes } = this.entities ('sap.capire.reviews')
|
||||
|
||||
// this.before (['CREATE','UPDATE'], 'Reviews', req => {
|
||||
// if (!req.data.rating) req.data.rating = Math.round(Math.random()*4)+1
|
||||
// })
|
||||
this.before (['CREATE','UPDATE'], 'Reviews', req => {
|
||||
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
|
||||
// ( 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})
|
||||
)
|
||||
req.on ('succeeded', ()=>{
|
||||
console.log ('< emitting:', 'reviewed', { subject, rating })
|
||||
global.it || console.log ('< emitting:', 'reviewed', { subject, rating })
|
||||
this.emit ('reviewed', { subject, rating })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -3,17 +3,16 @@
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"@capire/bookshop": "*",
|
||||
"@capire/reviews": "*"
|
||||
},
|
||||
"scripts": {
|
||||
"watch": "cds watch"
|
||||
"@capire/reviews": "*",
|
||||
"@sap/cds": "^3.31.1",
|
||||
"express": "^4.17.1"
|
||||
},
|
||||
"cds": {
|
||||
"requires": {
|
||||
"db": {
|
||||
"kind": "sql"
|
||||
},
|
||||
"sap.capire.reviews.ReviewsService": {
|
||||
"ReviewsService": {
|
||||
"kind": "odata"
|
||||
},
|
||||
"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')
|
||||
|
||||
module.exports = cds.service.impl (async()=>{
|
||||
cds.on('listening', async()=>{
|
||||
|
||||
// connect to requires services
|
||||
const ReviewsService = await cds.connect.to ('sap.capire.reviews.ReviewsService')
|
||||
// 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 { 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}))
|
||||
})
|
||||
})
|
||||
const { Books } = db.entities
|
||||
|
||||
// react on event messages from reviews service
|
||||
ReviewsService.on ('reviewed', (msg) => {
|
||||
@@ -29,4 +20,16 @@ module.exports = cds.service.impl (async()=>{
|
||||
// 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"}
|
||||
|
||||
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)
|
||||
|
||||
- Shows how to implement a modular service to manage product reviews, including...
|
||||
- Consuming other services synchronously and asynchronously
|
||||
- Serving requests synchronously
|
||||
- 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...
|
||||
- Mocking app services
|
||||
- Running service meshes
|
||||
- Late-cut Micro Services
|
||||
- As well as managed data, input validations and authorization
|
||||
|
||||
|
||||
## [fiori](fiori)
|
||||
|
||||
Reference in New Issue
Block a user