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