|
1 | | -const cds = require('@sap/cds') |
2 | | - |
3 | | -class CatalogService extends cds.ApplicationService { init() { |
4 | | - |
5 | | - const { Books } = cds.entities('sap.capire.bookshop') |
6 | | - const { ListOfBooks } = this.entities |
7 | | - |
8 | | - // Add some discount for overstocked books |
9 | | - this.after('each', ListOfBooks, book => { |
10 | | - if (book.stock > 111) book.title += ` -- 11% discount!` |
11 | | - }) |
12 | | - |
13 | | - // Reduce stock of ordered books if available stock suffices |
14 | | - this.on('submitOrder', async req => { |
15 | | - let { book:id, quantity } = req.data |
16 | | - let book = await SELECT.from (Books, id, b => b.stock) |
17 | | - |
18 | | - // Validate input data |
19 | | - if (!book) return req.error (404, `Book #${id} doesn't exist`) |
20 | | - if (quantity < 1) return req.error (400, `quantity has to be 1 or more`) |
21 | | - if (quantity > book.stock) return req.error (409, `${quantity} exceeds stock for book #${id}`) |
22 | | - |
23 | | - // Reduce stock in database and return updated stock value |
24 | | - await UPDATE (Books, id) .with ({ stock: book.stock -= quantity }) |
25 | | - return book |
26 | | - }) |
27 | | - |
28 | | - // Emit event when an order has been submitted |
29 | | - this.after('submitOrder', async (_,req) => { |
30 | | - let { book, quantity } = req.data |
31 | | - await this.emit('OrderedBook', { book, quantity, buyer: req.user.id }) |
32 | | - }) |
33 | | - |
34 | | - // Delegate requests to the underlying generic service |
35 | | - return super.init() |
36 | | -}} |
37 | | - |
38 | | -module.exports = CatalogService |
| 1 | +const cds = require("@sap/cds"); |
| 2 | + |
| 3 | +class CatalogService extends cds.ApplicationService { |
| 4 | + init() { |
| 5 | + const { Books } = cds.entities("sap.capire.bookshop"); |
| 6 | + const { ListOfBooks } = this.entities; |
| 7 | + |
| 8 | + // Add some discount for overstocked books |
| 9 | + this.after("each", ListOfBooks, (book) => { |
| 10 | + if (book.stock > 111) book.title += ` -- 11% discount!`; |
| 11 | + }); |
| 12 | + |
| 13 | + // Reduce stock of ordered books if available stock suffices |
| 14 | + this.on("submitOrder", async (req) => { |
| 15 | + let { book: id, quantity } = req.data; |
| 16 | + let book = await SELECT.from(Books, id, (b) => b.stock); |
| 17 | + |
| 18 | + // Validate input data |
| 19 | + if (!book) return req.error(404, `Book #${id} doesn't exist`); |
| 20 | + if (quantity < 1) return req.error(400, `quantity has to be 1 or more`); |
| 21 | + if (quantity > book.stock) return req.error(409, `${quantity} exceeds stock for book #${id}`); |
| 22 | + |
| 23 | + // Reduce stock in database and return updated stock value |
| 24 | + await UPDATE(Books, id).with({ stock: (book.stock -= quantity) }); |
| 25 | + return book; |
| 26 | + }); |
| 27 | + |
| 28 | + // Emit event when an order has been submitted |
| 29 | + this.after("submitOrder", async (_, req) => { |
| 30 | + let { book, quantity } = req.data; |
| 31 | + await this.emit("OrderedBook", { book, quantity, buyer: req.user.id }); |
| 32 | + }); |
| 33 | + |
| 34 | + // Delegate requests to the underlying generic service |
| 35 | + return super.init(); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +module.exports = CatalogService; |
0 commit comments