Added reviews/test/bookshop

This commit is contained in:
Daniel
2020-03-08 12:42:26 +01:00
parent 737027ded4
commit 84a815e7e6
10 changed files with 64 additions and 101 deletions

View File

@@ -1,5 +1,4 @@
using { sap.capire.reviews as my } from '../db/schema';
namespace sap.capire.reviews;
service ReviewsService {
// Sync API

View File

@@ -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 })
})
})

View 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"
}
}
}
}

View 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;
}

View 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

View File

@@ -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', ()=>{

View File

@@ -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"}