Compare commits

..

28 Commits

Author SHA1 Message Date
Christian Georgi
7b9ee0f827 Update README.md 2020-04-20 17:12:32 +02:00
Christian Georgi
b75a47c6ef Add package-lock.json, fix readme 2020-04-02 16:17:45 +02:00
Christian Georgi
8055848430 Update readme 2020-03-23 17:11:45 +01:00
D065023
a6e9fac097 rm other packages 2020-03-20 10:55:01 +01:00
Dr. David Kunz
e33fb7df0b Update package.json 2020-02-11 11:50:45 +01:00
D065023
e2aac07054 cleaner 2020-01-30 08:30:53 +01:00
D065023
e6ab102512 default 2020-01-29 15:22:50 +01:00
D065023
50ab059c13 comment 2020-01-29 14:46:31 +01:00
D065023
46960159d1 cleaner 2020-01-29 14:35:22 +01:00
D065023
02228e5a96 easier 2020-01-29 14:16:34 +01:00
D065023
d4793177fc rm 2020-01-29 14:13:42 +01:00
D065023
2787284aad blocked 2020-01-29 14:13:15 +01:00
D065023
61a318b250 fixed bug 2020-01-29 13:02:23 +01:00
D065023
6e42e5a173 address -> bupa 2020-01-29 12:59:59 +01:00
D065023
bf162c23cc cleaner 2020-01-29 09:55:21 +01:00
D065023
9b41615ac8 file-based not needed 2020-01-29 08:34:49 +01:00
D065023
29840afc0b data 2020-01-28 18:14:24 +01:00
D065023
fa880e2987 rm lock 2020-01-28 18:02:09 +01:00
D065023
90881558dc working 2020-01-28 18:01:53 +01:00
D065023
6d3e9a211a rm bug 2020-01-28 16:04:32 +01:00
D065023
e694fbaf13 messaging 2020-01-28 16:00:01 +01:00
D065023
43d0373d70 rm complexity 2020-01-28 15:21:46 +01:00
D065023
b632013b16 old way 2020-01-28 13:08:20 +01:00
D065023
a6deddf022 added postalcode 2020-01-28 10:41:14 +01:00
D065023
513bf9711f changed data 2020-01-28 10:40:15 +01:00
D065023
00c99c0e0b devdep 2020-01-28 10:27:23 +01:00
D065023
e042317f82 sync API 2020-01-28 09:07:23 +01:00
D065023
04ab69c48f imported 2020-01-28 08:32:37 +01:00
21 changed files with 5831 additions and 6473 deletions

2
.gitignore vendored
View File

@@ -13,5 +13,3 @@ target/
connection.properties connection.properties
default-env.json default-env.json
packages/messageBox packages/messageBox
*.db

2
.npmrc
View File

@@ -1 +1 @@
@sap:registry=https://npm.sap.com @sap:registry=https://npm.sap.com

20
.vscode/launch.json vendored
View File

@@ -9,16 +9,8 @@
"request": "launch", "request": "launch",
"type": "node", "type": "node",
"runtimeExecutable": "npx", "runtimeExecutable": "npx",
"runtimeArgs": [ "runtimeArgs": ["-n"],
"-n" "args": ["--", "cds", "run", "--with-mocks", "--in-memory?"], // the leading "--" arg ensures it works with as well as without debugging
],
"args": [
"--",
"cds",
"run",
"--with-mocks",
"--in-memory"
], // the leading "--" arg ensures it works with as well as without debugging
"cwd": "${workspaceFolder}/packages/${input:service}", "cwd": "${workspaceFolder}/packages/${input:service}",
"console": "integratedTerminal", "console": "integratedTerminal",
"serverReadyAction": { "serverReadyAction": {
@@ -26,9 +18,7 @@
"uriFormat": "http://localhost:%s", "uriFormat": "http://localhost:%s",
"action": "openExternally" "action": "openExternally"
}, },
"skipFiles": [ "skipFiles": ["<node_internals>/**"]
"<node_internals>/**"
]
} }
], ],
"inputs": [ "inputs": [
@@ -46,7 +36,7 @@
"reviews-service", "reviews-service",
"user-service" "user-service"
], ],
"default": "bookshop" "default": "bookstore"
} }
] ]
} }

View File

@@ -11,7 +11,7 @@ In SAP Business Application Studio, open a terminal.
Then clone the repo with this specific branch: Then clone the repo with this specific branch:
```sh ```sh
git clone https://github.com/sap-samples/cloud-cap-samples projects/cloud-cap-samples -b openSAP-week2-unit4567 git clone https://github.com/sap-samples/cloud-cap-samples projects/cloud-cap-samples -b openSAP-week4-unit3-final
cd projects/cloud-cap-samples cd projects/cloud-cap-samples
``` ```
@@ -20,16 +20,6 @@ In the `cloud-cap-samples` folder run:
npm install npm install
``` ```
### Cloud Foundry Login
This is required later in the demo. In Studio's terminal, execute:
```sh
cf login
```
As input, provide
- The Cloud Foundry API endpoint, which is usally `https://api.cf.eu10.hana.ondemand.com`. It can be obtained from the Overview page of your Subaccount in Cloud Cockpit.
- Your user's email address and password
- The name of your trial organization and space
## Run ## Run
Now you're ready to run the samples, for example: Now you're ready to run the samples, for example:

6253
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -9,18 +9,20 @@
"install": "(npm -s run lerna) && lerna bootstrap --hoist", "install": "(npm -s run lerna) && lerna bootstrap --hoist",
"lerna": "npx --no-install lerna -v > /dev/null || npm i lerna --no-save", "lerna": "npx --no-install lerna -v > /dev/null || npm i lerna --no-save",
"test": "jest", "test": "jest",
"bookshop": "cds watch packages/bookshop" "bookshop-enhanced": "cds watch packages/bookshop-enhanced",
"bookshop": "cds watch packages/bookshop",
"bookstore": "cds watch packages/bookstore",
"products-service": "cds watch packages/products-service",
"reviews-service": "cds watch packages/reviews-service"
}, },
"dependencies": { "dependencies": {
"@sap/cds": "^3", "@sap/cds": "^3",
"express": "^4" "express": "^4"
}, },
"devDependencies": { "--add-these-to-devDependencies-for-tests": {
"@types/jest": "*", "@types/jest": "*",
"sqlite3": "*", "sqlite3": "*",
"jest": "*", "jest": "*"
"supertest": "^4.0.2",
"@sap/hdi-deploy": "3.7.0"
}, },
"license": "SAP SAMPLE CODE LICENSE" "license": "SAP SAMPLE CODE LICENSE"
} }

View File

@@ -1,31 +0,0 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
"version": "0.2.0",
"configurations": [
{
"name": "Run bookshop",
"request": "launch",
"type": "node",
"cwd": "/home/user/projects/cloud-cap-samples/packages/bookshop",
"runtimeExecutable": "npx",
"runtimeArgs": [
"-n"
],
"args": [
"--",
"cds",
"run",
"--in-memory?"
],
"console": "internalConsole",
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": [
"<node_internals>/**"
],
"env": {
"run.config": "{\"handlerId\":\"cap_run_config_handler_id\",\"runnableId\":\"/home/user/projects/cloud-cap-samples/packages/bookshop\"}"
}
}
]
}

View File

@@ -43,7 +43,7 @@
<script src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js" <script src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js"
data-sap-ui-libs="sap.m, sap.ushell, sap.collaboration, sap.ui.layout" data-sap-ui-libs="sap.m, sap.ushell, sap.collaboration, sap.ui.layout"
data-sap-ui-compatVersion="edge" data-sap-ui-compatVersion="edge"
data-sap-ui-theme="sap_fiori_3" data-sap-ui-theme="sap_belize"
data-sap-ui-frameOptions="allow" data-sap-ui-frameOptions="allow"
></script> ></script>
<script> <script>

View File

@@ -1,3 +1,5 @@
ID;modifiedAt;createdAt;createdBy;modifiedBy;OrderNo;currency_code ID;modifiedAt;createdAt;createdBy;modifiedBy;OrderNo;currency_code;status
7e2f2640-6866-4dcf-8f4d-3027aa831cad;;2019-01-31;john.doe@test.com;;1;EUR da86efd0-4ba1-4078-b7f0-5c9c530297f7;;2019-01-31;ALICE;;1;EUR;processing
64e718c9-ff99-47f1-8ca3-950c850777d4;;2019-01-30;jane.doe@test.com;;2;EUR 2f2f2640-6866-4dcf-8f4d-3027aa831cad;;2019-03-25;ALICE;;10;EUR;completed
64e718c9-ff99-47f1-8ca3-950c850777d4;;2019-01-30;BOB;;2;EUR;processing
1af3322d-3cb1-46be-b312-0ae9ec311537;;2019-03-16;BOB;;9;EUR;completed
1 ID modifiedAt createdAt createdBy modifiedBy OrderNo currency_code status
2 7e2f2640-6866-4dcf-8f4d-3027aa831cad da86efd0-4ba1-4078-b7f0-5c9c530297f7 2019-01-31 john.doe@test.com ALICE 1 EUR processing
3 64e718c9-ff99-47f1-8ca3-950c850777d4 2f2f2640-6866-4dcf-8f4d-3027aa831cad 2019-01-30 2019-03-25 jane.doe@test.com ALICE 2 10 EUR completed
4 64e718c9-ff99-47f1-8ca3-950c850777d4 2019-01-30 BOB 2 EUR processing
5 1af3322d-3cb1-46be-b312-0ae9ec311537 2019-03-16 BOB 9 EUR completed

View File

@@ -1,35 +1,50 @@
namespace sap.capire.bookshop; namespace sap.capire.bookshop;
using { Currency, managed, cuid } from '@sap/cds/common';
using {
Currency,
managed,
cuid
} from '@sap/cds/common';
type Status : String enum {
completed;
processing;
blocked;
}
entity Books : managed { entity Books : managed {
key ID : Integer; key ID : Integer;
title : localized String(111); title : localized String(111);
descr : localized String(1111); descr : localized String(1111);
author : Association to Authors; author : Association to Authors;
stock : Integer; stock : Integer;
price : Decimal(9,2); price : Decimal(9, 2);
currency : Currency; currency : Currency;
} }
entity Authors : managed { entity Authors : managed {
key ID : Integer; key ID : Integer;
name : String(111); name : String(111);
dateOfBirth : Date; dateOfBirth : Date;
dateOfDeath : Date; dateOfDeath : Date;
placeOfBirth : String; placeOfBirth : String;
placeOfDeath : String; placeOfDeath : String;
books : Association to many Books on books.author = $self; books : Association to many Books
on books.author = $self;
} }
entity Orders : cuid, managed { entity Orders : cuid, managed {
OrderNo : String @title:'Order Number'; //> readable key OrderNo : String @title : 'Order Number'; //> readable key
Items : Composition of many OrderItems on Items.parent = $self; status : Status default 'processing';
total : Decimal(9,2) @readonly; Items : Composition of many OrderItems
on Items.parent = $self;
total : Decimal(9, 2)@readonly;
currency : Currency; currency : Currency;
} }
entity OrderItems : cuid { entity OrderItems : cuid {
parent : Association to Orders; parent : Association to Orders;
book : Association to Books; book : Association to Books;
amount : Integer; amount : Integer;
netAmount : Decimal(9,2); netAmount : Decimal(9, 2);
} }

View File

@@ -6,13 +6,27 @@
"dependencies": { "dependencies": {
"@sap/cds": "^3", "@sap/cds": "^3",
"express": "^4", "express": "^4",
"hdb": "^0.17.1" "@sap/xb-msg-amqp-v100": "^0.9.35"
}, },
"devDependencies": { "devDependencies": {
"jest": "*" "sqlite3": "^4.1.1"
}, },
"scripts": { "scripts": {
"start": "npx cds run", "start": "npx cds run"
"test": "jest" },
"cds": {
"requires": {
"API_BUSINESS_PARTNER": {
"kind": "odata",
"model": "srv/external/API_BUSINESS_PARTNER",
"--credentials": {
"prefix": "sap/S4HANAOD/c098/BO",
"destination": "cap-api098"
}
},
"--messaging": {
"kind": "enterprise-messaging"
}
}
} }
} }

View File

@@ -0,0 +1,6 @@
PATCH http://localhost:4004/api-business-partner/A_BusinessPartner('ALICE')
Content-Type: application/json
{
"BusinessPartnerIsBlocked": true
}

View File

@@ -8,8 +8,9 @@ service AdminService @(_requires:'authenticated-user') {
// Enable Fiori Draft for Orders // Enable Fiori Draft for Orders
annotate AdminService.Orders with @odata.draft.enabled; annotate AdminService.Orders with @odata.draft.enabled;
// annotate AdminService.Books with @odata.draft.enabled;
// Temporary workaround -> cap/issues#3121 // Temporary workaround -> https://github.wdf.sap.corp/cap/issues/issues/3121
extend service AdminService with { extend service AdminService with {
entity OrderItems as select from my.OrderItems; entity OrderItems as select from my.OrderItems;
} }

View File

@@ -1,22 +0,0 @@
const cds = require('@sap/cds')
/** Service implementation for AdminService */
module.exports = cds.service.impl(srv => {
const { OrderItems } = srv.entities ('sap.capire.bookshop')
srv.after (['READ','EDIT'], 'Orders', _calculateTotals)
// on-the-fly calculate the total Order price based on the OrderItems' netAmounts
async function _calculateTotals (orders, req) {
const ordersByID = Array.isArray(orders)
? orders.reduce ((all,o) => { (all[o.ID] = o).total=0; return all },{})
: { [orders.ID]: orders }
return cds.transaction(req) .run (
SELECT.from(OrderItems) .columns ('parent_ID', 'netAmount')
.where ({ parent_ID: {in: Object.keys(ordersByID)} })
) .then (items =>
items.forEach (item => ordersByID [item.parent_ID] .total += item.netAmount)
)
}
})

View File

@@ -1,13 +1,25 @@
using { sap.capire.bookshop as my } from '../db/schema'; using { sap.capire.bookshop as my } from '../db/schema';
using { API_BUSINESS_PARTNER as external } from './external/API_BUSINESS_PARTNER.csn';
@path:'/browse' @path:'/browse'
// @impl: './cat-service.js'
service CatalogService { service CatalogService {
@readonly entity Books as SELECT from my.Books {*, @readonly entity Books as SELECT from my.Books {*,
author.name as author author.name as author
} excluding { createdBy, modifiedBy }; } excluding { createdBy, modifiedBy };
@readonly entity BusinessPartners as projection on external.A_BusinessPartner {
key BusinessPartner as ID,
FirstName,
MiddleName,
LastName,
BusinessPartnerIsBlocked
};
event OrderBlocked {
ID: UUID;
};
@requires_: 'authenticated-user' @requires_: 'authenticated-user'
@insertonly entity Orders as projection on my.Orders; @insertonly entity Orders as projection on my.Orders;

View File

@@ -1,28 +1,41 @@
const cds = require('@sap/cds') const cds = require('@sap/cds')
const { Books } = cds.entities
/** Service implementation for CatalogService */ /** Service implementation for CatalogService */
module.exports = cds.service.impl(srv => { module.exports = cds.service.impl(async function () {
srv.after ('READ', 'Books', each => each.stock > 111 && _addDiscount2(each,11)) const { Books, Orders, BusinessPartners } = this.entities
srv.before ('CREATE', 'Orders', _reduceStock) const bupaSrv = await cds.connect.to('API_BUSINESS_PARTNER')
// srv.before ('*', (req) => { console.debug ('>>>', req.method, req.target.name) }) this.after('READ', Books, each => each.stock > 111 && _addDiscount2(each, 11))
this.before('CREATE', Orders, _reduceStock)
this.on('READ', BusinessPartners, req => bupaSrv.tx(req).run(req.query))
/** Block orders if business partner is blocked */
bupaSrv.on('BusinessPartner/Changed', async msg => {
console.log('>> Received BusinessPartner/Changed', msg.data)
const BUSINESSPARTNER = msg.data.KEY[0].BUSINESSPARTNER
const tx = cds.tx(msg)
const orders = await tx.run(SELECT('ID').from(Orders).where({ createdBy: BUSINESSPARTNER, status: 'processing' }))
if (!orders.length) return
const businessPartner = await bupaSrv.tx(msg).run(SELECT.one('BusinessPartnerIsBlocked').from(BusinessPartners).where({ ID: BUSINESSPARTNER }))
if (!businessPartner || !businessPartner.BusinessPartnerIsBlocked) return
await Promise.all(orders.map(order => tx.run(UPDATE(Orders).where(order).set({ status: 'blocked' }))))
orders.forEach(order => this.emit('OrderBlocked', order) && console.log('>> Emitted OrderBlocked', order))
})
/** Add some discount for overstocked books */
function _addDiscount2(each, discount) {
each.title += ` -- ${discount}% discount!`
}
/** Reduce stock of ordered books if available stock suffices */
async function _reduceStock(req) {
const { Items: OrderItems } = req.data
return cds.transaction(req).run(() => OrderItems.map(order =>
UPDATE(Books).set('stock -=', order.amount)
.where('ID =', order.book_ID).and('stock >=', order.amount)
)).then(all => all.forEach((affectedRows, i) => {
if (affectedRows === 0) req.error(409,
`${OrderItems[i].amount} exceeds stock for book #${OrderItems[i].book_ID}`
)
}))
}
}) })
/** Add some discount for overstocked books */
function _addDiscount2 (each,discount) {
each.title += ` -- ${discount}% discount!`
}
/** Reduce stock of ordered books if available stock suffices */
async function _reduceStock (req) {
const { Items: orderItems } = req.data
return cds.transaction(req) .run (()=> orderItems.map (item =>
UPDATE (Books)
.set ('stock -=', item.amount)
.where ('ID =', item.book_ID) .and ('stock >=', item.amount)
)).then (all => all.forEach ((affectedRows,i) => {
if (affectedRows === 0) {
req.error (409, `${orderItems[i].amount} exceeds stock for book #${orderItems[i].book_ID}`)
}
}))
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,9 @@
module.exports = srv => {
srv.on(['CREATE', 'UPDATE', 'DELETE'], req => {
const payload = {
KEY: [{ BUSINESSPARTNER: req.data.BusinessPartner }]
}
srv.emit('BusinessPartner/Changed', payload)
console.log('<< Emitted BusinessPartner/Changed', payload)
})
}

View File

@@ -0,0 +1,4 @@
BusinessPartner;FirstName;MiddleName;LastName;BusinessPartnerIsBlocked
ALICE;Alice;In;Wonderland;false
BOB;Bob;The;Builder;false
JABBA;Jabba;The;Hutt;true
1 BusinessPartner FirstName MiddleName LastName BusinessPartnerIsBlocked
2 ALICE Alice In Wonderland false
3 BOB Bob The Builder false
4 JABBA Jabba The Hutt true

View File

@@ -1,79 +0,0 @@
const cds = require('@sap/cds')
describe('Bookshop: OData Protocol Level Testing', () => {
jest.setTimeout(20*1000)
const app = require('express')()
const request = require('supertest')(app)
beforeAll(async () => {
await cds.deploy(__dirname + '/../srv/cat-service').to('sqlite::memory:')
await cds.serve('CatalogService').from(__dirname + '/../srv/cat-service').in(app)
})
it('Service $metadata document', async () => {
const response = await request
.get('/browse/$metadata')
.expect('Content-Type', /^application\/xml/)
.expect(200)
const expectedVersion = '<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">'
const expectedBooksEntitySet = '<EntitySet Name="Books" EntityType="CatalogService.Books">'
expect(response.text.includes(expectedVersion)).toBeTruthy()
expect(response.text.includes(expectedBooksEntitySet)).toBeTruthy()
})
it('Get with select, expand and localized', async () => {
const response = await request
.get('/browse/Books?$select=title,author&$expand=currency')
.set('Accept-Language', 'de')
.expect('Content-Type', /^application\/json/)
.expect(200)
expect(response.body.value).toMatchObject([
{
ID: 201, title: "Sturmhöhe", author: "Emily Brontë",
currency: { name: "Pfund", descr: "Britische Pfund", code: "GBP", symbol: "£" }
},
{
ID: 207, title: "Jane Eyre", author: "Charlotte Brontë",
currency: { name: "Pfund", descr: "Britische Pfund", code: "GBP", symbol: "£" }
},
{
ID: 251, title: "The Raven", author: "Edgar Allen Poe",
currency: { name: "US-Dollar", descr: "United States Dollar", code: "USD", symbol: "$" }
},
{
ID: 252, title: "Eleonora", author: "Edgar Allen Poe",
currency: { name: "US-Dollar", descr: "United States Dollar", code: "USD", symbol: "$" }
},
{
ID: 271, title: "Catweazle", author: "Richard Carpenter",
currency: { name: "Euro", descr: "European Euro", code: "EUR", symbol: "€" }
}
])
})
})
describe('Bookshop: CDS Service Level Testing', () => {
let srv, Books
beforeAll(async () => {
srv = await cds.serve('CatalogService').from(__dirname + '/../srv/cat-service')
Books = srv.entities.Books
expect(Books).toBeDefined()
})
it('GETs all books', async () => {
const books = await srv.read(Books, b => { b.title })
expect(books).toMatchObject([
{ title: 'Wuthering Heights' },
{ title: 'Jane Eyre' },
{ title: 'The Raven' },
{ title: 'Eleonora' },
{ title: 'Catweazle' }
])
})
})