Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 1fde0c9

Browse files
committed
Fixed submit order -> returned stock was wrong
1 parent fa854a2 commit 1fde0c9

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

bookshop/srv/cat-service.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ class CatalogService extends cds.ApplicationService { init(){
77
// Reduce stock of ordered books if available stock suffices
88
this.on ('submitOrder', async req => {
99
const {book,quantity} = req.data
10+
if (quantity < 1) return req.reject (400,`quantity has to be 1 or more`)
1011
let {stock} = await SELECT `stock` .from (Books,book)
11-
if (stock >= quantity) {
12-
await UPDATE (Books,book) .with (`stock -=`, quantity)
13-
await this.emit ('OrderedBook', { book, quantity, buyer:req.user.id })
14-
return { stock }
15-
}
16-
else return req.error (409,`${quantity} exceeds stock for book #${book}`)
12+
if (quantity > stock) return req.reject (409,`${quantity} exceeds stock for book #${book}`)
13+
await UPDATE (Books,book) .with ({ stock: stock -= quantity })
14+
await this.emit ('OrderedBook', { book, quantity, buyer:req.user.id })
15+
return { stock }
1716
})
1817

1918
// Add some discount for overstocked books

0 commit comments

Comments
 (0)