Merge branch 'main' into d070615-tree-table
This commit is contained in:
@@ -8,18 +8,14 @@ class OrdersService extends cds.ApplicationService {
|
|||||||
this.before ('UPDATE', 'Orders', async function(req) {
|
this.before ('UPDATE', 'Orders', async function(req) {
|
||||||
const { ID, Items } = req.data
|
const { ID, Items } = req.data
|
||||||
if (Items) for (let { product_ID, quantity } of Items) {
|
if (Items) for (let { product_ID, quantity } of Items) {
|
||||||
const { quantity:before } = await cds.tx(req).run (
|
const { quantity:before } = await SELECT.one.from (OrderItems, oi => oi.quantity) .where ({up__ID:ID, product_ID})
|
||||||
SELECT.one.from (OrderItems, oi => oi.quantity) .where ({up__ID:ID, product_ID})
|
|
||||||
)
|
|
||||||
if (quantity != before) await this.orderChanged (product_ID, quantity-before)
|
if (quantity != before) await this.orderChanged (product_ID, quantity-before)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
this.before ('DELETE', 'Orders', async function(req) {
|
this.before ('DELETE', 'Orders', async function(req) {
|
||||||
const { ID } = req.data
|
const { ID } = req.data
|
||||||
const Items = await cds.tx(req).run (
|
const Items = await SELECT.from (OrderItems, oi => { oi.product_ID, oi.quantity }) .where ({up__ID:ID})
|
||||||
SELECT.from (OrderItems, oi => { oi.product_ID, oi.quantity }) .where ({up__ID:ID})
|
|
||||||
)
|
|
||||||
if (Items) await Promise.all (Items.map(it => this.orderChanged (it.product_ID, -it.quantity)))
|
if (Items) await Promise.all (Items.map(it => this.orderChanged (it.product_ID, -it.quantity)))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -12,9 +12,7 @@ module.exports = cds.service.impl (function(){
|
|||||||
// Emit an event to inform subscribers about new avg ratings for reviewed subjects
|
// Emit an event to inform subscribers about new avg ratings for reviewed subjects
|
||||||
this.after (['CREATE','UPDATE','DELETE'], 'Reviews', async function(_,req) {
|
this.after (['CREATE','UPDATE','DELETE'], 'Reviews', async function(_,req) {
|
||||||
const {subject} = req.data
|
const {subject} = req.data
|
||||||
const { count, rating } = await cds.tx(req) .run (
|
const { count, rating } = await SELECT.one `round(avg(rating),2) as rating, count(*) as count` .from (Reviews) .where ({subject})
|
||||||
SELECT.one `round(avg(rating),2) as rating, count(*) as count` .from (Reviews) .where ({subject})
|
|
||||||
)
|
|
||||||
global.it || console.log ('< emitting:', 'reviewed', { subject, count, rating }) // eslint-disable-line no-console
|
global.it || console.log ('< emitting:', 'reviewed', { subject, count, rating }) // eslint-disable-line no-console
|
||||||
await this.emit ('reviewed', { subject, count, rating })
|
await this.emit ('reviewed', { subject, count, rating })
|
||||||
})
|
})
|
||||||
@@ -23,8 +21,7 @@ module.exports = cds.service.impl (function(){
|
|||||||
this.on ('like', (req) => {
|
this.on ('like', (req) => {
|
||||||
if (!req.user) return req.reject(400, 'You must be identified to like a review')
|
if (!req.user) return req.reject(400, 'You must be identified to like a review')
|
||||||
const {review} = req.data, {user} = req
|
const {review} = req.data, {user} = req
|
||||||
const tx = cds.tx(req)
|
return cds.run ([
|
||||||
return tx.run ([
|
|
||||||
INSERT.into (Likes) .entries ({review_ID: review, user: user.id}),
|
INSERT.into (Likes) .entries ({review_ID: review, user: user.id}),
|
||||||
UPDATE (Reviews) .set({liked: {'+=': 1}}) .where({ID:review})
|
UPDATE (Reviews) .set({liked: {'+=': 1}}) .where({ID:review})
|
||||||
]).catch(() => req.reject(400, 'You already liked that review'))
|
]).catch(() => req.reject(400, 'You already liked that review'))
|
||||||
@@ -34,9 +31,8 @@ module.exports = cds.service.impl (function(){
|
|||||||
this.on ('unlike', async (req) => {
|
this.on ('unlike', async (req) => {
|
||||||
if (!req.user) return req.reject(400, 'You must be identified to remove a former like of yours')
|
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 {review} = req.data, {user} = req
|
||||||
const tx = cds.tx(req)
|
const affectedRows = await DELETE.from (Likes) .where ({review_ID: review,user: user.id})
|
||||||
const affectedRows = await tx.run (DELETE.from (Likes) .where ({review_ID: review,user: user.id}))
|
if (affectedRows === 1) return UPDATE (Reviews) .set ({liked: {'-=': 1}}) .where ({ID:review})
|
||||||
if (affectedRows === 1) return tx.run (UPDATE (Reviews) .set ({liked: {'-=': 1}}) .where ({ID:review}))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user