From 19083d156e122ddeff2d6871d54255f30ce37f4c Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 8 Sep 2021 15:27:07 +0200 Subject: [PATCH] Rename amount -> quantity --- bookshop/app/vue/app.js | 12 ++++++------ bookshop/app/vue/index.html | 2 +- bookshop/srv/cat-service.cds | 4 ++-- bookshop/srv/cat-service.js | 10 +++++----- bookshop/test/requests.http | 2 +- fiori/srv/mashup.js | 10 +++++----- orders/app/orders/fiori-service.cds | 6 +++--- .../db/data/sap.capire.orders-Orders_Items.csv | 2 +- orders/db/schema.cds | 2 +- orders/srv/orders-service.js | 18 +++++++++--------- test/custom-handlers.test.js | 6 +++--- 11 files changed, 37 insertions(+), 37 deletions(-) diff --git a/bookshop/app/vue/app.js b/bookshop/app/vue/app.js index f99d87a8..8dbb6cdf 100644 --- a/bookshop/app/vue/app.js +++ b/bookshop/app/vue/app.js @@ -10,7 +10,7 @@ const books = new Vue ({ data: { list: [], book: undefined, - order: { amount:1, succeeded:'', failed:'' } + order: { quantity:1, succeeded:'', failed:'' } }, methods: { @@ -26,18 +26,18 @@ const books = new Vue ({ const book = books.book = books.list [eve.currentTarget.rowIndex-1] const res = await GET(`/Books/${book.ID}?$select=descr,stock,image`) Object.assign (book, res.data) - books.order = { amount:1 } + books.order = { quantity:1 } setTimeout (()=> $('form > input').focus(), 111) }, async submitOrder () { - const {book,order} = books, amount = parseInt (order.amount) || 1 // REVISIT: Okra should be less strict + const {book,order} = books, quantity = parseInt (order.quantity) || 1 // REVISIT: Okra should be less strict try { - const res = await POST(`/submitOrder`, { amount, book: book.ID }) + const res = await POST(`/submitOrder`, { quantity, book: book.ID }) book.stock = res.data.stock - books.order = { amount, succeeded: `Successfully ordered ${amount} item(s).` } + books.order = { quantity, succeeded: `Successfully ordered ${quantity} item(s).` } } catch (e) { - books.order = { amount, failed: e.response.data.error.message } + books.order = { quantity, failed: e.response.data.error.message } } } diff --git a/bookshop/app/vue/index.html b/bookshop/app/vue/index.html index c8e23209..6dae24bc 100644 --- a/bookshop/app/vue/index.html +++ b/bookshop/app/vue/index.html @@ -48,7 +48,7 @@    {{ book.stock }} in stock
- +

{{ book.title }}

diff --git a/bookshop/srv/cat-service.cds b/bookshop/srv/cat-service.cds index 4cc44dff..2441db25 100644 --- a/bookshop/srv/cat-service.cds +++ b/bookshop/srv/cat-service.cds @@ -11,6 +11,6 @@ service CatalogService @(path:'/browse') { } excluding { createdBy, modifiedBy }; @requires: 'authenticated-user' - action submitOrder ( book: Books:ID, amount: Integer ) returns { stock: Integer }; - event OrderedBook : { book: Books:ID; amount: Integer; buyer: String }; + action submitOrder ( book: Books:ID, quantity: Integer ) returns { stock: Integer }; + event OrderedBook : { book: Books:ID; quantity: Integer; buyer: String }; } diff --git a/bookshop/srv/cat-service.js b/bookshop/srv/cat-service.js index a27aa397..84c5cf6d 100644 --- a/bookshop/srv/cat-service.js +++ b/bookshop/srv/cat-service.js @@ -5,14 +5,14 @@ class CatalogService extends cds.ApplicationService { init(){ // Reduce stock of ordered books if available stock suffices this.on ('submitOrder', async req => { - const {book,amount} = req.data + const {book,quantity} = req.data let {stock} = await SELECT `stock` .from (Books,book) - if (stock >= amount) { - await UPDATE (Books,book) .with (`stock -=`, amount) - await this.emit ('OrderedBook', { book, amount, buyer:req.user.id }) + if (stock >= quantity) { + await UPDATE (Books,book) .with (`stock -=`, quantity) + await this.emit ('OrderedBook', { book, quantity, buyer:req.user.id }) return { stock } } - else return req.error (409,`${amount} exceeds stock for book #${book}`) + else return req.error (409,`${quantity} exceeds stock for book #${book}`) }) // Add some discount for overstocked books diff --git a/bookshop/test/requests.http b/bookshop/test/requests.http index 1fbdc0ca..890e621e 100644 --- a/bookshop/test/requests.http +++ b/bookshop/test/requests.http @@ -71,7 +71,7 @@ POST {{server}}/browse/submitOrder Content-Type: application/json {{me}} -{ "book":201, "amount":5 } +{ "book":201, "quantity":5 } ### ------------------------------------------------------------------------ diff --git a/fiori/srv/mashup.js b/fiori/srv/mashup.js index e8acf174..b76252fe 100644 --- a/fiori/srv/mashup.js +++ b/fiori/srv/mashup.js @@ -27,11 +27,11 @@ module.exports = async()=>{ // called by server.js // Create an order with the OrdersService when CatalogService signals a new order // CatalogService.on ('OrderedBook', async (msg) => { - const { book, amount, buyer } = msg.data + const { book, quantity, buyer } = msg.data const { title, price } = await db.tx(msg).read (Books, book, b => { b.title, b.price }) return OrdersService.tx(msg).create ('Orders').entries({ OrderNo: 'Order at '+ (new Date).toLocaleString(), - Items: [{ product:{ID:`${book}`}, title, price, amount }], + Items: [{ product:{ID:`${book}`}, title, price, quantity }], buyer, createdBy: buyer }) }) @@ -51,9 +51,9 @@ module.exports = async()=>{ // called by server.js // OrdersService.on ('OrderChanged', (msg) => { console.debug ('> received:', msg.event, msg.data) - const { product, deltaAmount } = msg.data + const { product, deltaQuantity } = msg.data return UPDATE (Books) .where ('ID =', product) - .and ('stock >=', deltaAmount) - .set ('stock -=', deltaAmount) + .and ('stock >=', deltaQuantity) + .set ('stock -=', deltaQuantity) }) } diff --git a/orders/app/orders/fiori-service.cds b/orders/app/orders/fiori-service.cds index e58c8ccd..ba096372 100644 --- a/orders/app/orders/fiori-service.cds +++ b/orders/app/orders/fiori-service.cds @@ -74,10 +74,10 @@ annotate OrdersService.Orders_Items with @( {Value: product_ID, Label:'Product ID'}, {Value: title, Label:'Product Title'}, {Value: price, Label:'Unit Price'}, - {Value: amount, Label:'Quantity'}, + {Value: quantity, Label:'Quantity'}, ], Identification: [ //Is the main field group - {Value: amount, Label:'Amount'}, + {Value: quantity, Label:'Quantity'}, {Value: title, Label:'Product'}, {Value: price, Label:'Unit Price'}, ], @@ -86,7 +86,7 @@ annotate OrdersService.Orders_Items with @( ], }, ) { - amount @( + quantity @( Common.FieldControl: #Mandatory ); }; diff --git a/orders/db/data/sap.capire.orders-Orders_Items.csv b/orders/db/data/sap.capire.orders-Orders_Items.csv index b3025abe..78487735 100644 --- a/orders/db/data/sap.capire.orders-Orders_Items.csv +++ b/orders/db/data/sap.capire.orders-Orders_Items.csv @@ -1,4 +1,4 @@ -ID;up__ID;amount;product_ID;title;price +ID;up__ID;quantity;product_ID;title;price 58040e66-1dcd-4ffb-ab10-fdce32028b79;7e2f2640-6866-4dcf-8f4d-3027aa831cad;1;201;Wuthering Heights;11.11 64e718c9-ff99-47f1-8ca3-950c850777d4;7e2f2640-6866-4dcf-8f4d-3027aa831cad;1;271;Catweazle;15 e9641166-e050-4261-bfee-d1e797e6cb7f;64e718c9-ff99-47f1-8ca3-950c850777d4;2;252;Eleonora;28 \ No newline at end of file diff --git a/orders/db/schema.cds b/orders/db/schema.cds index 0911a4c4..c543c200 100644 --- a/orders/db/schema.cds +++ b/orders/db/schema.cds @@ -12,7 +12,7 @@ entity Orders_Items { key ID : UUID; up_ : Association to Orders; product : Association to Products @assert.integrity:false; // REVISIT: this is a temporary workaround for a glitch in cds-runtime - amount : Integer; + quantity : Integer; title : String; //> intentionally replicated as snapshot from product.title price : Double; } diff --git a/orders/srv/orders-service.js b/orders/srv/orders-service.js index 10420410..434c9e20 100644 --- a/orders/srv/orders-service.js +++ b/orders/srv/orders-service.js @@ -7,30 +7,30 @@ class OrdersService extends cds.ApplicationService { this.before ('UPDATE', 'Orders', async function(req) { const { ID, Items } = req.data - if (Items) for (let { product_ID, amount } of Items) { - const { amount:before } = await cds.tx(req).run ( - SELECT.one.from (OrderItems, oi => oi.amount) .where ({up__ID:ID, product_ID}) + if (Items) for (let { product_ID, quantity } of Items) { + const { quantity:before } = await cds.tx(req).run ( + SELECT.one.from (OrderItems, oi => oi.quantity) .where ({up__ID:ID, product_ID}) ) - if (amount != before) await this.orderChanged (product_ID, amount-before) + if (quantity != before) await this.orderChanged (product_ID, quantity-before) } }) this.before ('DELETE', 'Orders', async function(req) { const { ID } = req.data const Items = await cds.tx(req).run ( - SELECT.from (OrderItems, oi => { oi.product_ID, oi.amount }) .where ({up__ID:ID}) + SELECT.from (OrderItems, oi => { oi.product_ID, oi.quantity }) .where ({up__ID:ID}) ) - if (Items) await Promise.all (Items.map(it => this.orderChanged (it.product_ID, -it.amount))) + if (Items) await Promise.all (Items.map(it => this.orderChanged (it.product_ID, -it.quantity))) }) return super.init() } /** order changed -> broadcast event */ - orderChanged (product, deltaAmount) { + orderChanged (product, deltaQuantity) { // Emit events to inform subscribers about changes in orders - console.log ('> emitting:', 'OrderChanged', { product, deltaAmount }) - return this.emit ('OrderChanged', { product, deltaAmount }) + console.log ('> emitting:', 'OrderChanged', { product, deltaQuantity }) + return this.emit ('OrderChanged', { product, deltaQuantity }) } } diff --git a/test/custom-handlers.test.js b/test/custom-handlers.test.js index f3c422d1..0e7bf7e2 100644 --- a/test/custom-handlers.test.js +++ b/test/custom-handlers.test.js @@ -6,9 +6,9 @@ else cds.User = cds.User.Privileged // hard core monkey patch for older cds rele describe('Custom Handlers', () => { it('should reject out-of-stock orders', async () => { - await POST `/browse/submitOrder ${{ book: 201, amount: 5 }}` - await POST `/browse/submitOrder ${{ book: 201, amount: 5 }}` - await expect(POST `/browse/submitOrder ${{ book: 201, amount: 5 }}`).to.be.rejectedWith(/409 - 5 exceeds stock for book #201/) + await POST `/browse/submitOrder ${{ book: 201, quantity: 5 }}` + await POST `/browse/submitOrder ${{ book: 201, quantity: 5 }}` + await expect(POST `/browse/submitOrder ${{ book: 201, quantity: 5 }}`).to.be.rejectedWith(/409 - 5 exceeds stock for book #201/) const { data } = await GET`/admin/Books/201/stock/$value` expect(data).to.equal(2) })