@@ -12,9 +12,7 @@ module.exports = cds.service.impl (function(){
12
12
// Emit an event to inform subscribers about new avg ratings for reviewed subjects
13
13
this . after ( [ 'CREATE' , 'UPDATE' , 'DELETE' ] , 'Reviews' , async function ( _ , req ) {
14
14
const { subject} = req . data
15
- const { count, rating } = await cds . tx ( req ) . run (
16
- SELECT . one `round(avg(rating),2) as rating, count(*) as count` . from ( Reviews ) . where ( { subject} )
17
- )
15
+ const { count, rating } = await SELECT . one `round(avg(rating),2) as rating, count(*) as count` . from ( Reviews ) . where ( { subject} )
18
16
global . it || console . log ( '< emitting:' , 'reviewed' , { subject, count, rating } ) // eslint-disable-line no-console
19
17
await this . emit ( 'reviewed' , { subject, count, rating } )
20
18
} )
@@ -23,8 +21,7 @@ module.exports = cds.service.impl (function(){
23
21
this . on ( 'like' , ( req ) => {
24
22
if ( ! req . user ) return req . reject ( 400 , 'You must be identified to like a review' )
25
23
const { review} = req . data , { user} = req
26
- const tx = cds . tx ( req )
27
- return tx . run ( [
24
+ return cds . run ( [
28
25
INSERT . into ( Likes ) . entries ( { review_ID : review , user : user . id } ) ,
29
26
UPDATE ( Reviews ) . set ( { liked : { '+=' : 1 } } ) . where ( { ID :review } )
30
27
] ) . catch ( ( ) => req . reject ( 400 , 'You already liked that review' ) )
@@ -34,9 +31,8 @@ module.exports = cds.service.impl (function(){
34
31
this . on ( 'unlike' , async ( req ) => {
35
32
if ( ! req . user ) return req . reject ( 400 , 'You must be identified to remove a former like of yours' )
36
33
const { review} = req . data , { user} = req
37
- const tx = cds . tx ( req )
38
- const affectedRows = await tx . run ( DELETE . from ( Likes ) . where ( { review_ID : review , user : user . id } ) )
39
- if ( affectedRows === 1 ) return tx . run ( UPDATE ( Reviews ) . set ( { liked : { '-=' : 1 } } ) . where ( { ID :review } ) )
34
+ const affectedRows = await DELETE . from ( Likes ) . where ( { review_ID : review , user : user . id } )
35
+ if ( affectedRows === 1 ) return UPDATE ( Reviews ) . set ( { liked : { '-=' : 1 } } ) . where ( { ID :review } )
40
36
} )
41
37
42
38
} )
0 commit comments