diff --git a/orders/db/schema.cds b/orders/db/schema.cds index 920d27d2..5bf1aeba 100644 --- a/orders/db/schema.cds +++ b/orders/db/schema.cds @@ -1,23 +1,2 @@ -using { Currency, User, managed, cuid } from '@sap/cds/common'; -using from '@capire/common'; -namespace sap.capire.orders; - -entity Orders : cuid, managed { - OrderNo : String @title:'Order Number'; //> readable key - Items : Composition of many { - key ID : UUID; - @assert.integrity:false // REVISIT: this is a temporary workaround for a glitch in cds-runtime - product : Association to Products; - amount : Integer; - title : String; - price : Double; - }; - buyer : User; - currency : Currency; -} - -/** This is a stand-in for arbitrary ordered Products */ -@cds.persistence.skip:'always' -entity Products { - key ID : String; -} +// using from './without-managed-composition/schema'; +using from './with-managed-composition/schema'; diff --git a/orders/db/with-managed-composition/schema.cds b/orders/db/with-managed-composition/schema.cds new file mode 100644 index 00000000..920d27d2 --- /dev/null +++ b/orders/db/with-managed-composition/schema.cds @@ -0,0 +1,23 @@ +using { Currency, User, managed, cuid } from '@sap/cds/common'; +using from '@capire/common'; +namespace sap.capire.orders; + +entity Orders : cuid, managed { + OrderNo : String @title:'Order Number'; //> readable key + Items : Composition of many { + key ID : UUID; + @assert.integrity:false // REVISIT: this is a temporary workaround for a glitch in cds-runtime + product : Association to Products; + amount : Integer; + title : String; + price : Double; + }; + buyer : User; + currency : Currency; +} + +/** This is a stand-in for arbitrary ordered Products */ +@cds.persistence.skip:'always' +entity Products { + key ID : String; +} diff --git a/orders/db/schema.cds- b/orders/db/without-managed-composition/schema.cds similarity index 100% rename from orders/db/schema.cds- rename to orders/db/without-managed-composition/schema.cds diff --git a/orders/managed-comp.xml b/orders/managed-comp.xml index 5e9f4bfb..a1b9f107 100644 --- a/orders/managed-comp.xml +++ b/orders/managed-comp.xml @@ -12,11 +12,20 @@ - + + + + + + + + + + @@ -29,16 +38,22 @@ - - + + + + + + + + @@ -64,7 +79,9 @@ - + + + @@ -78,10 +95,14 @@ + - + + + + @@ -140,6 +161,10 @@ + + + + @@ -413,6 +438,13 @@ + + + + + + + diff --git a/orders/srv/orders-service.js b/orders/srv/orders-service.js index ed42770f..887ee593 100644 --- a/orders/srv/orders-service.js +++ b/orders/srv/orders-service.js @@ -3,13 +3,13 @@ class OrdersService extends cds.ApplicationService { /** register custom handlers */ init(){ - const { OrderItems } = this.entities + const { Orders_Items:OrderItems } = this.entities 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 ({order_ID:ID, product_ID}) + SELECT.one.from (OrderItems, oi => oi.amount) .where ({up__ID:ID, product_ID}) ) if (amount != before) this.orderChanged (product_ID, amount-before) } @@ -18,7 +18,7 @@ class OrdersService extends cds.ApplicationService { 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 ({order_ID:ID}) + SELECT.from (OrderItems, oi => { oi.product_ID, oi.amount }) .where ({up__ID:ID}) ) if (Items) for (let it of Items) this.orderChanged (it.product_ID, -it.amount) })