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 985001f..b0f659d 100644 --- a/srv/cat-service.js +++ b/srv/cat-service.js @@ -14,11 +14,11 @@ 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`) + let {affected} = await UPDATE (Books,id) + .with `stock = stock - ${quantity}` + .where `stock >= ${quantity}` + 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}`) }) 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: