Compare commits
34 Commits
openSAP-we
...
openSAP-we
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0e894313c3 | ||
|
|
83ea6a363d | ||
|
|
98cf14101a | ||
|
|
401ff039ff | ||
|
|
4d8b887389 | ||
|
|
8ab6a7ebf6 | ||
|
|
09eb51fa32 | ||
|
|
7a6a6a0257 | ||
|
|
f17c5bfa57 | ||
|
|
85498a9875 | ||
|
|
19b09645db | ||
|
|
9861b05f2d | ||
|
|
c58bcf905f | ||
|
|
50ab059c13 | ||
|
|
46960159d1 | ||
|
|
02228e5a96 | ||
|
|
d4793177fc | ||
|
|
2787284aad | ||
|
|
61a318b250 | ||
|
|
6e42e5a173 | ||
|
|
bf162c23cc | ||
|
|
9b41615ac8 | ||
|
|
29840afc0b | ||
|
|
fa880e2987 | ||
|
|
90881558dc | ||
|
|
6d3e9a211a | ||
|
|
e694fbaf13 | ||
|
|
43d0373d70 | ||
|
|
b632013b16 | ||
|
|
a6deddf022 | ||
|
|
513bf9711f | ||
|
|
00c99c0e0b | ||
|
|
e042317f82 | ||
|
|
04ab69c48f |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -13,5 +13,3 @@ target/
|
|||||||
connection.properties
|
connection.properties
|
||||||
default-env.json
|
default-env.json
|
||||||
packages/messageBox
|
packages/messageBox
|
||||||
*.db
|
|
||||||
|
|
||||||
|
|||||||
20
.vscode/launch.json
vendored
20
.vscode/launch.json
vendored
@@ -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"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
12
README.md
12
README.md
@@ -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-unit1
|
||||||
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
6253
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
12
package.json
12
package.json
@@ -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"
|
||||||
}
|
}
|
||||||
|
|||||||
31
packages/bookshop/.vscode/launch.json
vendored
31
packages/bookshop/.vscode/launch.json
vendored
@@ -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\"}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
@@ -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>
|
||||||
|
|||||||
@@ -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,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);
|
||||||
}
|
}
|
||||||
@@ -6,13 +6,12 @@
|
|||||||
"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"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
using { sap.capire.bookshop as my } from '../db/schema';
|
using { sap.capire.bookshop as my } from '../db/schema';
|
||||||
|
|
||||||
@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 {*,
|
||||||
|
|||||||
@@ -1,28 +1,26 @@
|
|||||||
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(function () {
|
||||||
srv.after ('READ', 'Books', each => each.stock > 111 && _addDiscount2(each,11))
|
const { Books, Orders } = this.entities
|
||||||
srv.before ('CREATE', 'Orders', _reduceStock)
|
this.after('READ', Books, each => each.stock > 111 && _addDiscount2(each, 11))
|
||||||
// srv.before ('*', (req) => { console.debug ('>>>', req.method, req.target.name) })
|
this.before('CREATE', Orders, _reduceStock)
|
||||||
|
|
||||||
|
/** 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}`)
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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' }
|
|
||||||
])
|
|
||||||
})
|
|
||||||
})
|
|
||||||
Reference in New Issue
Block a user