...
This commit is contained in:
@@ -1,23 +1,2 @@
|
|||||||
using { Currency, User, managed, cuid } from '@sap/cds/common';
|
// using from './without-managed-composition/schema';
|
||||||
using from '@capire/common';
|
using from './with-managed-composition/schema';
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|||||||
23
orders/db/with-managed-composition/schema.cds
Normal file
23
orders/db/with-managed-composition/schema.cds
Normal file
@@ -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;
|
||||||
|
}
|
||||||
@@ -12,11 +12,20 @@
|
|||||||
<edmx:DataServices>
|
<edmx:DataServices>
|
||||||
<Schema Namespace="OrdersService" xmlns="http://docs.oasis-open.org/odata/ns/edm">
|
<Schema Namespace="OrdersService" xmlns="http://docs.oasis-open.org/odata/ns/edm">
|
||||||
<EntityContainer Name="EntityContainer">
|
<EntityContainer Name="EntityContainer">
|
||||||
<EntitySet Name="Currencies" EntityType="OrdersService.Currencies"/>
|
<EntitySet Name="Currencies" EntityType="OrdersService.Currencies">
|
||||||
|
<NavigationPropertyBinding Path="texts" Target="Currencies_texts"/>
|
||||||
|
<NavigationPropertyBinding Path="localized" Target="Currencies_texts"/>
|
||||||
|
</EntitySet>
|
||||||
|
<EntitySet Name="Currencies_texts" EntityType="OrdersService.Currencies_texts"/>
|
||||||
<EntitySet Name="Orders" EntityType="OrdersService.Orders">
|
<EntitySet Name="Orders" EntityType="OrdersService.Orders">
|
||||||
|
<NavigationPropertyBinding Path="Items" Target="Orders_Items"/>
|
||||||
<NavigationPropertyBinding Path="currency" Target="Currencies"/>
|
<NavigationPropertyBinding Path="currency" Target="Currencies"/>
|
||||||
<NavigationPropertyBinding Path="SiblingEntity" Target="Orders"/>
|
<NavigationPropertyBinding Path="SiblingEntity" Target="Orders"/>
|
||||||
</EntitySet>
|
</EntitySet>
|
||||||
|
<EntitySet Name="Orders_Items" EntityType="OrdersService.Orders_Items">
|
||||||
|
<NavigationPropertyBinding Path="up_" Target="Orders"/>
|
||||||
|
<NavigationPropertyBinding Path="SiblingEntity" Target="Orders_Items"/>
|
||||||
|
</EntitySet>
|
||||||
</EntityContainer>
|
</EntityContainer>
|
||||||
<EntityType Name="Currencies">
|
<EntityType Name="Currencies">
|
||||||
<Key>
|
<Key>
|
||||||
@@ -29,16 +38,22 @@
|
|||||||
<Property Name="numcode" Type="Edm.Int32"/>
|
<Property Name="numcode" Type="Edm.Int32"/>
|
||||||
<Property Name="exponent" Type="Edm.Int32"/>
|
<Property Name="exponent" Type="Edm.Int32"/>
|
||||||
<Property Name="minor" Type="Edm.String"/>
|
<Property Name="minor" Type="Edm.String"/>
|
||||||
<NavigationProperty Name="texts" Type="Collection(OrdersService.Currencies_texts)" ContainsTarget="true"/>
|
<NavigationProperty Name="texts" Type="Collection(OrdersService.Currencies_texts)">
|
||||||
<NavigationProperty Name="localized" Type="OrdersService.Currencies_texts"/>
|
<OnDelete Action="Cascade"/>
|
||||||
|
</NavigationProperty>
|
||||||
|
<NavigationProperty Name="localized" Type="OrdersService.Currencies_texts">
|
||||||
|
<ReferentialConstraint Property="code" ReferencedProperty="code"/>
|
||||||
|
</NavigationProperty>
|
||||||
</EntityType>
|
</EntityType>
|
||||||
<EntityType Name="Currencies_texts">
|
<EntityType Name="Currencies_texts">
|
||||||
<Key>
|
<Key>
|
||||||
<PropertyRef Name="locale"/>
|
<PropertyRef Name="locale"/>
|
||||||
|
<PropertyRef Name="code"/>
|
||||||
</Key>
|
</Key>
|
||||||
<Property Name="locale" Type="Edm.String" MaxLength="14" Nullable="false"/>
|
<Property Name="locale" Type="Edm.String" MaxLength="14" Nullable="false"/>
|
||||||
<Property Name="name" Type="Edm.String" MaxLength="255"/>
|
<Property Name="name" Type="Edm.String" MaxLength="255"/>
|
||||||
<Property Name="descr" Type="Edm.String" MaxLength="1000"/>
|
<Property Name="descr" Type="Edm.String" MaxLength="1000"/>
|
||||||
|
<Property Name="code" Type="Edm.String" MaxLength="3" Nullable="false"/>
|
||||||
</EntityType>
|
</EntityType>
|
||||||
<EntityType Name="DraftAdministrativeData">
|
<EntityType Name="DraftAdministrativeData">
|
||||||
<Key>
|
<Key>
|
||||||
@@ -64,7 +79,9 @@
|
|||||||
<Property Name="modifiedAt" Type="Edm.DateTimeOffset" Precision="7"/>
|
<Property Name="modifiedAt" Type="Edm.DateTimeOffset" Precision="7"/>
|
||||||
<Property Name="modifiedBy" Type="Edm.String" MaxLength="255"/>
|
<Property Name="modifiedBy" Type="Edm.String" MaxLength="255"/>
|
||||||
<Property Name="OrderNo" Type="Edm.String"/>
|
<Property Name="OrderNo" Type="Edm.String"/>
|
||||||
<NavigationProperty Name="Items" Type="Collection(OrdersService.Orders_Items)" Partner="up_" ContainsTarget="true"/>
|
<NavigationProperty Name="Items" Type="Collection(OrdersService.Orders_Items)" Partner="up_">
|
||||||
|
<OnDelete Action="Cascade"/>
|
||||||
|
</NavigationProperty>
|
||||||
<Property Name="buyer" Type="Edm.String" MaxLength="255"/>
|
<Property Name="buyer" Type="Edm.String" MaxLength="255"/>
|
||||||
<NavigationProperty Name="currency" Type="OrdersService.Currencies">
|
<NavigationProperty Name="currency" Type="OrdersService.Currencies">
|
||||||
<ReferentialConstraint Property="currency_code" ReferencedProperty="code"/>
|
<ReferentialConstraint Property="currency_code" ReferencedProperty="code"/>
|
||||||
@@ -78,10 +95,14 @@
|
|||||||
</EntityType>
|
</EntityType>
|
||||||
<EntityType Name="Orders_Items">
|
<EntityType Name="Orders_Items">
|
||||||
<Key>
|
<Key>
|
||||||
|
<PropertyRef Name="up__ID"/>
|
||||||
<PropertyRef Name="ID"/>
|
<PropertyRef Name="ID"/>
|
||||||
<PropertyRef Name="IsActiveEntity"/>
|
<PropertyRef Name="IsActiveEntity"/>
|
||||||
</Key>
|
</Key>
|
||||||
<NavigationProperty Name="up_" Type="OrdersService.Orders" Nullable="false" Partner="Items"/>
|
<Property Name="up__ID" Type="Edm.Guid" Nullable="false"/>
|
||||||
|
<NavigationProperty Name="up_" Type="OrdersService.Orders" Nullable="false" Partner="Items">
|
||||||
|
<ReferentialConstraint Property="up__ID" ReferencedProperty="ID"/>
|
||||||
|
</NavigationProperty>
|
||||||
<Property Name="ID" Type="Edm.Guid" Nullable="false"/>
|
<Property Name="ID" Type="Edm.Guid" Nullable="false"/>
|
||||||
<Property Name="amount" Type="Edm.Int32"/>
|
<Property Name="amount" Type="Edm.Int32"/>
|
||||||
<Property Name="title" Type="Edm.String"/>
|
<Property Name="title" Type="Edm.String"/>
|
||||||
@@ -140,6 +161,10 @@
|
|||||||
<Annotations Target="OrdersService.Currencies_texts/descr">
|
<Annotations Target="OrdersService.Currencies_texts/descr">
|
||||||
<Annotation Term="Common.Label" String="{i18n>Description}"/>
|
<Annotation Term="Common.Label" String="{i18n>Description}"/>
|
||||||
</Annotations>
|
</Annotations>
|
||||||
|
<Annotations Target="OrdersService.Currencies_texts/code">
|
||||||
|
<Annotation Term="Common.Label" String="{i18n>CurrencyCode}"/>
|
||||||
|
<Annotation Term="Common.Text" Path="name"/>
|
||||||
|
</Annotations>
|
||||||
<Annotations Target="OrdersService.DraftAdministrativeData">
|
<Annotations Target="OrdersService.DraftAdministrativeData">
|
||||||
<Annotation Term="Common.Label" String="{i18n>Draft_DraftAdministrativeData}"/>
|
<Annotation Term="Common.Label" String="{i18n>Draft_DraftAdministrativeData}"/>
|
||||||
</Annotations>
|
</Annotations>
|
||||||
@@ -413,6 +438,13 @@
|
|||||||
</Collection>
|
</Collection>
|
||||||
</Annotation>
|
</Annotation>
|
||||||
</Annotations>
|
</Annotations>
|
||||||
|
<Annotations Target="OrdersService.EntityContainer/Orders_Items">
|
||||||
|
<Annotation Term="Common.DraftNode">
|
||||||
|
<Record Type="Common.DraftNodeType">
|
||||||
|
<PropertyValue Property="PreparationAction" String="OrdersService.draftPrepare"/>
|
||||||
|
</Record>
|
||||||
|
</Annotation>
|
||||||
|
</Annotations>
|
||||||
<Annotations Target="OrdersService.Orders_Items/amount">
|
<Annotations Target="OrdersService.Orders_Items/amount">
|
||||||
<Annotation Term="Common.FieldControl" EnumMember="Common.FieldControlType/Mandatory"/>
|
<Annotation Term="Common.FieldControl" EnumMember="Common.FieldControlType/Mandatory"/>
|
||||||
</Annotations>
|
</Annotations>
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ class OrdersService extends cds.ApplicationService {
|
|||||||
|
|
||||||
/** register custom handlers */
|
/** register custom handlers */
|
||||||
init(){
|
init(){
|
||||||
const { OrderItems } = this.entities
|
const { Orders_Items:OrderItems } = this.entities
|
||||||
|
|
||||||
this.before ('UPDATE', 'Orders', async function(req) {
|
this.before ('UPDATE', 'Orders', async function(req) {
|
||||||
const { ID, Items } = req.data
|
const { ID, Items } = req.data
|
||||||
if (Items) for (let { product_ID, amount } of Items) {
|
if (Items) for (let { product_ID, amount } of Items) {
|
||||||
const { amount:before } = await cds.tx(req).run (
|
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)
|
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) {
|
this.before ('DELETE', 'Orders', async function(req) {
|
||||||
const { ID } = req.data
|
const { ID } = req.data
|
||||||
const Items = await cds.tx(req).run (
|
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)
|
if (Items) for (let it of Items) this.orderChanged (it.product_ID, -it.amount)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user