diff --git a/README.md b/README.md index 035f62cd..bf6dc1fd 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,8 @@ Clone this repo as shown below, if you have [git](https://git-scm.com/downloads) otherwise [download as zip file](archive/master.zip). ```sh -git clone https://github.com/sap-samples/cloud-cap-samples samples +# git clone https://github.com/sap-samples/cloud-cap-samples samples +git clone https://github.com/SAP-samples/cloud-cap-samples -b cap-samples-reloaded samples cd samples ``` diff --git a/reviewed/tests.http b/reviewed/tests.http index 1476f53f..d1c89504 100644 --- a/reviewed/tests.http +++ b/reviewed/tests.http @@ -25,7 +25,7 @@ GET http://localhost:4004/reviews/Reviews? POST http://localhost:4004/reviews/Reviews Content-Type: application/json;IEEE754Compatible=true -{"subject":"201", "rating":"5", "title":"boo"} +{"subject":"201", "title":"boo"} @@ -37,4 +37,4 @@ GET http://localhost:5005/reviews/Reviews? POST http://localhost:5005/reviews/Reviews Content-Type: application/json;IEEE754Compatible=true -{"subject":"201", "rating":"5", "title":"boo"} +{"subject":"201", "title":"boo"} diff --git a/reviews/srv/reviews-service.js b/reviews/srv/reviews-service.js index ae24ebba..c798ec0c 100644 --- a/reviews/srv/reviews-service.js +++ b/reviews/srv/reviews-service.js @@ -5,6 +5,10 @@ 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','DELETE'], '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 ) this.after (['CREATE','UPDATE','DELETE'], 'Reviews', async(_,req) => { diff --git a/reviews/tests/requests.http b/reviews/tests/requests.http index 21206196..564c8789 100644 --- a/reviews/tests/requests.http +++ b/reviews/tests/requests.http @@ -10,7 +10,7 @@ POST http://localhost:5005/reviews/Reviews # POST http://localhost:4004/reviews/Reviews Content-Type: application/json;IEEE754Compatible=true -{"subject":"201", "rating":"5", "title":"boo"} +{"subject":"201", "rating":"4", "title":"boo"} ### Direct Request to ReviewsService # Note: use 5005 instead of 4004 in case of separate service