From 94540658fc7e163de47e8f24c6a85a755b79217c Mon Sep 17 00:00:00 2001 From: Daniel Hutzel Date: Thu, 28 May 2026 16:07:19 +0200 Subject: [PATCH 1/3] fixed http tests --- test/requests.http | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/test/requests.http b/test/requests.http index 9cb0d78..05177b5 100644 --- a/test/requests.http +++ b/test/requests.http @@ -1,26 +1,33 @@ ### CatalogService.read Books -GET http://localhost:4004/browse/Books? +GET http://localhost:4004/odata/v4/browse/Books? &$select=ID,title,author &$filter=contains(author,'Bro') +### CatalogService.read Books/201 +GET http://localhost:4004/odata/v4/browse/Books/201 -### AdmingService.read Authors + +### AdmingService.read Books/201 +GET http://localhost:4004/admin/Books/201 + +### AdmingService.read Authors GET http://localhost:4004/admin/Authors? &$select=ID,name &$expand=books($select=ID,title;$expand=genre($select=name)) -Authorization: Basic alice: - ### Same with inofficial genre/name syntax GET http://localhost:4004/admin/Authors? &$select=ID,name &$expand=books($select=ID,title,genre/name) -Authorization: Basic alice: - ### CatalogService.submitOrder() -POST http://localhost:4004/browse/submitOrder +POST http://localhost:4004/odata/v4/browse/submitOrder +Content-Type: application/json +Authorization: Basic bob: + +### CatalogService.submitOrder(/201 +POST http://localhost:4004/odata/v4/browse/submitOrder Content-Type: application/json Authorization: Basic bob: From 8c732ee27021423792b96a6a911adc639f69ce7d Mon Sep 17 00:00:00 2001 From: Daniel Hutzel Date: Thu, 28 May 2026 16:08:39 +0200 Subject: [PATCH 2/3] Leveraging consolidated srv returns --- srv/cat-service.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/srv/cat-service.js b/srv/cat-service.js index 985001f..ee3a2b2 100644 --- a/srv/cat-service.js +++ b/srv/cat-service.js @@ -14,12 +14,10 @@ class CatalogService extends cds.ApplicationService { init() { this.on ('submitOrder', async req => { let { book:id, quantity } = req.data if (quantity < 1) return req.error (400, `quantity has to be 1 or more`) - let succeeded = await UPDATE (Books,id) - .with `stock = stock - ${quantity}` - .where `stock >= ${quantity}` - if (succeeded.affected ?? succeeded) return - else if (!this.exists(Books,id)) req.error (404, `Book #${id} doesn't exist`) - else req.error (409, `${quantity} exceeds stock for book #${id}`) + let {affected} = await UPDATE (Books,id) + .with `stock = stock - ${quantity}` + .where `stock >= ${quantity}` + if (!affected) req.error (409, `${quantity} exceeds stock for book #${id}`) }) // Delegate requests to the underlying generic service From 6d9205603a48b3d1291c019b41be86d43f52e324 Mon Sep 17 00:00:00 2001 From: Daniel Hutzel Date: Tue, 2 Jun 2026 08:53:18 +0200 Subject: [PATCH 3/3] fix: improve error handling in submitOrder for stock validation --- .cdsrc.yaml | 2 ++ srv/cat-service.js | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.cdsrc.yaml b/.cdsrc.yaml index 29c633e..a68dcd6 100644 --- a/.cdsrc.yaml +++ b/.cdsrc.yaml @@ -2,3 +2,5 @@ cds: requires: "[production]": auth: mocked # as a sample app run with mocked auth also in production + features: + legacy_db_results: false diff --git a/srv/cat-service.js b/srv/cat-service.js index ee3a2b2..b0f659d 100644 --- a/srv/cat-service.js +++ b/srv/cat-service.js @@ -17,7 +17,9 @@ class CatalogService extends cds.ApplicationService { init() { let {affected} = await UPDATE (Books,id) .with `stock = stock - ${quantity}` .where `stock >= ${quantity}` - if (!affected) req.error (409, `${quantity} exceeds stock for book #${id}`) + if (affected) return //> update was successful, so we are done + else if (!cds.db.exists(Books,id)) req.error (404, `Book #${id} doesn't exist`) + else req.error (409, `${quantity} exceeds stock for book #${id}`) }) // Delegate requests to the underlying generic service