From df55110b9b6ec4e09117cfae2907e5be0599c271 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 28 Oct 2020 15:14:34 +0100 Subject: [PATCH] Updated to use srv-to-srv + declared events --- reviewed/server.js | 3 +-- reviews/srv/reviews-service.cds | 7 +++++++ reviews/srv/reviews-service.js | 11 +++++------ test/messaging.test.js | 8 ++------ 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/reviewed/server.js b/reviewed/server.js index 73ac1be3..1d59efb9 100644 --- a/reviewed/server.js +++ b/reviewed/server.js @@ -9,7 +9,6 @@ const cds = require ('@sap/cds') cds.once('served', async ({CatalogService}) => { const ReviewsService = await cds.connect.to('ReviewsService') - const messaging = await cds.connect.to('messaging') // reflect entity definitions used below... const { Books } = cds.entities('sap.capire.bookshop') @@ -22,7 +21,7 @@ cds.once('served', async ({CatalogService}) => { return SELECT(columns).from(Reviews).limit(limit).where({subject:String(id)}) })) - messaging.on ('reviewed', (msg) => { + ReviewsService.on ('reviewed', (msg) => { console.debug ('> received:', msg.event, msg.data) const { subject, rating } = msg.data return UPDATE(Books,subject).with({rating}) diff --git a/reviews/srv/reviews-service.cds b/reviews/srv/reviews-service.cds index 8999e636..44fa83e2 100644 --- a/reviews/srv/reviews-service.cds +++ b/reviews/srv/reviews-service.cds @@ -2,10 +2,17 @@ using { sap.capire.reviews as my } from '../db/schema'; service ReviewsService { + // Sync API entity Reviews as projection on my.Reviews excluding { likes } action like (review: type of Reviews:ID); action unlike (review: type of Reviews:ID); + // Async API + event reviewed : { + subject: type of Reviews:subject; + rating: Decimal(2,1) + } + // Input validation annotate Reviews with { subject @mandatory; diff --git a/reviews/srv/reviews-service.js b/reviews/srv/reviews-service.js index 0eec865f..10979994 100644 --- a/reviews/srv/reviews-service.js +++ b/reviews/srv/reviews-service.js @@ -4,27 +4,26 @@ module.exports = cds.service.impl (async function(){ // Get the CSN definition for Reviews from the db schema for sub-sequent queries // ( Note: we explicitly specify the namespace to support embedded reuse ) const { Reviews, Likes } = this.entities ('sap.capire.reviews') - const messaging = await cds.connect.to('messaging') 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 - this.after (['CREATE','UPDATE','DELETE'], 'Reviews', async(_,req) => { + this.after (['CREATE','UPDATE','DELETE'], 'Reviews', async function(_,req) { const {subject} = req.data - const {rating} = await cds.transaction(req) .run ( + const {rating} = await cds.tx(req) .run ( SELECT.one (['round(avg(rating),2) as rating']) .from (Reviews) .where ({subject}) ) global.it || console.log ('< emitting:', 'reviewed', { subject, rating }) - messaging.tx(req).emit ('reviewed', { subject, rating }) + this.emit ('reviewed', { subject, rating }) }) // Increment counter for reviews considered helpful this.on ('like', (req) => { if (!req.user) return req.reject(400, 'You must be identified to like a review') const {review} = req.data, {user} = req - const tx = cds.transaction(req) + const tx = cds.tx(req) return tx.run ([ INSERT.into (Likes) .entries ({review_ID: review, user: user.id}), UPDATE (Reviews) .set({liked: {'+=': 1}}) .where({ID:review}) @@ -35,7 +34,7 @@ module.exports = cds.service.impl (async function(){ this.on ('unlike', async (req) => { if (!req.user) return req.reject(400, 'You must be identified to remove a former like of yours') const {review} = req.data, {user} = req - const tx = cds.transaction(req) + const tx = cds.tx(req) const affectedRows = await tx.run (DELETE.from (Likes) .where ({review_ID: review,user: user.id})) if (affectedRows === 1) return tx.run (UPDATE (Reviews) .set ({liked: {'-=': 1}}) .where ({ID:review})) }) diff --git a/test/messaging.test.js b/test/messaging.test.js index abc0d305..632b093f 100644 --- a/test/messaging.test.js +++ b/test/messaging.test.js @@ -1,14 +1,10 @@ const cwd = process.cwd(); process.chdir (__dirname) //> only for internal CI/CD@SAP const cds = require ('./cds'), {expect} = cds.test const _model = '@capire/reviews' -let messaging describe('Messaging', ()=>{ - beforeAll(async () => { - messaging = await cds.connect.to('messaging') - }) after(()=> process.chdir(cwd)) it ('should bootstrap sqlite in-memory db', async()=>{ @@ -24,11 +20,11 @@ describe('Messaging', ()=>{ let N=0, received=[], M=0 it ('should add messaging event handlers', ()=>{ - messaging.on('reviewed', (msg,next)=> { received.push(msg); return next() }) + srv.on('reviewed', (msg,next)=> { received.push(msg); return next() }) }) it ('should add more messaging event handlers', ()=>{ - messaging.on('reviewed', (_,next)=> { ++M; return next() }) + srv.on('reviewed', (_,next)=> { ++M; return next() }) }) it ('should add review', async ()=>{