final state of exercise 2

This commit is contained in:
d045778
2019-09-23 13:25:44 +02:00
parent 1e28cb217f
commit fb8f6acac7
8 changed files with 153 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
namespace sap.capire.reviews;
using { sap.capire.reviews as my } from '../db/schema';
service ReviewsService {
event reviewed : { subject:String; rating: Decimal(2,1) };
// API
entity Reviews as projection on my.Reviews excluding { likes }
action like (review:Reviews.ID);
action unlike (review:Reviews.ID);
// Input validation
annotate Reviews with {
subject @mandatory;
title @mandatory;
rating @mandatory @assert.enum;
}
// Auto-fill reviewers and review dates
annotate Reviews with {
reviewer @cds.on.insert:$user;
date @cds.on.insert:$now;
date @cds.on.update:$now;
}
}

View File

@@ -0,0 +1,40 @@
const cds = require('@sap/cds')
module.exports = cds.service.impl((srv) => {
// 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 } = cds.entities('sap.capire.reviews')
// Increment counter for reviews considered helpful
srv.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)
return tx.run ([
INSERT.into (Likes) .entries ({review_ID: review, user: user.id}),
UPDATE (Reviews) .set({liked: {'+=': 1}}) .where({ID:review})
]).catch(() => req.reject(400, 'You already liked that review'))
})
// Delete a former like by the same user
srv.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 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 }))
})
// 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 )
srv.after(['CREATE', 'UPDATE', 'DELETE'], 'Reviews', async (_, req) => {
const { subject } = req.data
const { rating } = await cds.transaction(req).run(
SELECT.one(['avg(rating) as rating']).from(Reviews).where({ subject })
)
req.on('succeeded', () => {
srv.emit('reviewed', { subject, rating })
console.log(`Reviewed event was emitted for book "${subject}" with rating ${rating}.`)
})
})
})