Rename amount -> quantity
This commit is contained in:
@@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@
|
||||
{{ book.stock }} in stock
|
||||
</label>
|
||||
<form @submit.prevent="submitOrder" style="float:right; display:flex; flex-direction:row-reverse">
|
||||
<input type="number" v-model="order.amount" v-bind:class="{ failed: order.failed }" style="width:5em">
|
||||
<input type="number" v-model="order.quantity" v-bind:class="{ failed: order.failed }" style="width:5em">
|
||||
<input type="submit" value="Order:" class="muted-button">
|
||||
</form>
|
||||
<h4> {{ book.title }} </h4>
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -71,7 +71,7 @@ POST {{server}}/browse/submitOrder
|
||||
Content-Type: application/json
|
||||
{{me}}
|
||||
|
||||
{ "book":201, "amount":5 }
|
||||
{ "book":201, "quantity":5 }
|
||||
|
||||
|
||||
### ------------------------------------------------------------------------
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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
|
||||
);
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user