From 8f3d112558cda1e56d7693cf3db027f3531b07f2 Mon Sep 17 00:00:00 2001 From: D065023 Date: Fri, 29 Nov 2019 12:11:29 +0100 Subject: [PATCH] prettier --- packages/bookshop/srv/cat-service.js | 39 ++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/packages/bookshop/srv/cat-service.js b/packages/bookshop/srv/cat-service.js index bd04c51e..150e076a 100644 --- a/packages/bookshop/srv/cat-service.js +++ b/packages/bookshop/srv/cat-service.js @@ -2,25 +2,42 @@ const cds = require('@sap/cds') const { Books } = cds.entities /** Service implementation for CatalogService */ -module.exports = cds.service.impl(function() { - this.after ('READ', 'Books', each => each.stock > 111 && _addDiscount2(each,11)) - this.before ('CREATE', 'Orders', _reduceStock) +module.exports = cds.service.impl(function () { + this.after( + 'READ', + 'Books', + each => each.stock > 111 && _addDiscount2(each, 11) + ) + this.before('CREATE', 'Orders', _reduceStock) }) /** Add some discount for overstocked books */ -function _addDiscount2 (each,discount) { +function _addDiscount2 (each, discount) { each.title += ` -- ${discount}% discount!` } /** Reduce stock of ordered books if available stock suffices */ async function _reduceStock (req) { const { Items: OrderItems } = req.data - return cds.transaction(req) .run (()=> OrderItems.map (order => - UPDATE (Books) .set ('stock -=', order.amount) - .where ('ID =', order.book_ID) .and ('stock >=', order.amount) - )) .then (all => all.forEach ((affectedRows,i) => { - if (affectedRows === 0) req.error (409, - `${OrderItems[i].amount} exceeds stock for book #${OrderItems[i].book_ID}` + return cds + .transaction(req) + .run(() => + OrderItems.map(order => + UPDATE(Books) + .set('stock -=', order.amount) + .where('ID =', order.book_ID) + .and('stock >=', order.amount) + ) + ) + .then(all => + all.forEach((affectedRows, i) => { + if (affectedRows === 0) + req.error( + 409, + `${OrderItems[i].amount} exceeds stock for book #${ + OrderItems[i].book_ID + }` + ) + }) ) - })) }