Compare commits
21 Commits
openSAP-we
...
openSAP-we
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dff5568618 | ||
|
|
20bb8d8c54 | ||
|
|
045e65487c | ||
|
|
e660e01b77 | ||
|
|
06a9ef0f5b | ||
|
|
8978799491 | ||
|
|
6d0194acc0 | ||
|
|
db75a99808 | ||
|
|
a04755efed | ||
|
|
6a9a1bc4d6 | ||
|
|
a0847e603f | ||
|
|
b1270bc0eb | ||
|
|
ba72d7f478 | ||
|
|
cd808c76dd | ||
|
|
3a77707f49 | ||
|
|
583c97a494 | ||
|
|
eb7431afed | ||
|
|
630bb2b19c | ||
|
|
f9a7aa59de | ||
|
|
9205e0893a | ||
|
|
7137bf227e |
@@ -6,7 +6,7 @@
|
||||
"jest": true
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2017
|
||||
"ecmaVersion": 2018
|
||||
},
|
||||
"globals": {
|
||||
"SELECT": true,
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -13,5 +13,3 @@ target/
|
||||
connection.properties
|
||||
default-env.json
|
||||
packages/messageBox
|
||||
*.db
|
||||
|
||||
|
||||
36
.vscode/launch.json
vendored
36
.vscode/launch.json
vendored
@@ -5,7 +5,27 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "cds run",
|
||||
"name": "bookshop",
|
||||
"request": "launch",
|
||||
"type": "node",
|
||||
"runtimeExecutable": "npx",
|
||||
"runtimeArgs": [
|
||||
"-n"
|
||||
],
|
||||
"args": [
|
||||
"--",
|
||||
"cds",
|
||||
"run",
|
||||
"--in-memory"
|
||||
],
|
||||
"cwd": "${workspaceFolder}/packages/bookshop",
|
||||
"console": "integratedTerminal",
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "cds run ...",
|
||||
"request": "launch",
|
||||
"type": "node",
|
||||
"runtimeExecutable": "npx",
|
||||
@@ -17,15 +37,10 @@
|
||||
"cds",
|
||||
"run",
|
||||
"--with-mocks",
|
||||
"--in-memory"
|
||||
], // the leading "--" arg ensures it works with as well as without debugging
|
||||
"--in-memory?"
|
||||
],
|
||||
"cwd": "${workspaceFolder}/packages/${input:service}",
|
||||
"console": "integratedTerminal",
|
||||
"serverReadyAction": {
|
||||
"pattern": "server listening on (https?://\\S+|[0-9]+)",
|
||||
"uriFormat": "http://localhost:%s",
|
||||
"action": "openExternally"
|
||||
},
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
]
|
||||
@@ -41,10 +56,7 @@
|
||||
"bookstore",
|
||||
"media-server",
|
||||
"office-supplies",
|
||||
"orders-service",
|
||||
"products-service",
|
||||
"reviews-service",
|
||||
"user-service"
|
||||
"reviews-service"
|
||||
],
|
||||
"default": "bookshop"
|
||||
}
|
||||
|
||||
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"files.exclude": {
|
||||
"**/.gitignore": false,
|
||||
"**/.git": false,
|
||||
"**/.vscode": false
|
||||
"**/.gitignore": true,
|
||||
"**/.vscode": true
|
||||
}
|
||||
}
|
||||
|
||||
21
.vscode/tasks.json
vendored
21
.vscode/tasks.json
vendored
@@ -4,23 +4,14 @@
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"type": "shell", "label": "cds run bookshop",
|
||||
"command": "npx", "args": [ "cds", "watch", "packages/bookshop" ],
|
||||
"presentation": { "group": "A" },
|
||||
"problemMatcher": []
|
||||
"type": "npm", "script": "watch", "path": "packages/bookshop/",
|
||||
"options": { "env": { "PORT": "4004" }},
|
||||
"presentation": { "group": "A" }
|
||||
},
|
||||
{
|
||||
"type": "shell", "label": "cds run bookshop-enhanced",
|
||||
"command": "npx", "args": [ "cds", "watch", "packages/bookshop-enhanced" ],
|
||||
"presentation": { "group": "A" },
|
||||
"problemMatcher": []
|
||||
},
|
||||
{
|
||||
"type": "shell", "label": "cds run reviews-service",
|
||||
"command": "npx", "args": [ "cds", "watch", "packages/reviews-service" ],
|
||||
"options": {"env": { "PORT":"5005" }},
|
||||
"presentation": { "group": "A" },
|
||||
"problemMatcher": []
|
||||
"type": "npm", "script": "watch", "path": "packages/reviews-service/",
|
||||
"options": { "env": { "PORT": "5005" }},
|
||||
"presentation": { "group": "A" }
|
||||
}
|
||||
]
|
||||
}
|
||||
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:
|
||||
|
||||
```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-week3-unit4
|
||||
cd projects/cloud-cap-samples
|
||||
```
|
||||
|
||||
@@ -20,16 +20,6 @@ In the `cloud-cap-samples` folder run:
|
||||
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
|
||||
|
||||
Now you're ready to run the samples, for example:
|
||||
|
||||
5463
package-lock.json
generated
5463
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
13
package.json
13
package.json
@@ -5,10 +5,9 @@
|
||||
"author": "daniel.hutzel@sap.com",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"cleanup": "lerna clean -y && rm -fr node_modules",
|
||||
"install": "(npm -s run lerna) && lerna bootstrap --hoist",
|
||||
"lerna": "npx --no-install lerna -v > /dev/null || npm i lerna --no-save",
|
||||
"test": "jest",
|
||||
"install": "(npm -s run lerna) && lerna bootstrap --hoist",
|
||||
"cleanup": "lerna clean -y && rm -fr node_modules",
|
||||
"bookshop": "cds watch packages/bookshop"
|
||||
},
|
||||
"dependencies": {
|
||||
@@ -16,11 +15,11 @@
|
||||
"express": "^4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"sqlite3": "*"
|
||||
},
|
||||
"--add-these-to-devDependencies-for-tests": {
|
||||
"@types/jest": "*",
|
||||
"sqlite3": "*",
|
||||
"jest": "*",
|
||||
"supertest": "^4.0.2",
|
||||
"@sap/hdi-deploy": "3.7.0"
|
||||
"jest": "*"
|
||||
},
|
||||
"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\"}"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,18 +1,25 @@
|
||||
{
|
||||
"name": "@sap/capire-bookshop",
|
||||
"version": "1.0.0",
|
||||
"description": "A simple bookshop application, build in a self-contained all-in-one fashion, i.e. w/o reusing other packages.",
|
||||
"license": "SAP SAMPLE CODE LICENSE",
|
||||
"dependencies": {
|
||||
"@sap/cds": "^3",
|
||||
"express": "^4",
|
||||
"hdb": "^0.17.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jest": "*"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "npx cds run",
|
||||
"test": "jest"
|
||||
}
|
||||
"name": "@sap/capire-bookshop",
|
||||
"version": "1.0.0",
|
||||
"description": "A simple bookshop application, build in a self-contained all-in-one fashion, i.e. w/o reusing other packages.",
|
||||
"license": "SAP SAMPLE CODE LICENSE",
|
||||
"dependencies": {
|
||||
"@sap/cds": "^3",
|
||||
"express": "^4"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "cds run --in-memory?",
|
||||
"watch": "cds watch"
|
||||
},
|
||||
"cds": {
|
||||
"requires": {
|
||||
"db": {
|
||||
"model": [
|
||||
"db",
|
||||
"srv"
|
||||
],
|
||||
"kind": "sqlite"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,9 @@ service AdminService @(_requires:'authenticated-user') {
|
||||
|
||||
// Enable Fiori Draft for Orders
|
||||
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 {
|
||||
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';
|
||||
|
||||
@path:'/browse'
|
||||
// @impl: './cat-service.js'
|
||||
service CatalogService {
|
||||
|
||||
@readonly entity Books as SELECT from my.Books {*,
|
||||
|
||||
@@ -2,27 +2,25 @@ const cds = require('@sap/cds')
|
||||
const { Books } = cds.entities
|
||||
|
||||
/** Service implementation for CatalogService */
|
||||
module.exports = cds.service.impl(srv => {
|
||||
srv.after ('READ', 'Books', each => each.stock > 111 && _addDiscount2(each,11))
|
||||
srv.before ('CREATE', 'Orders', _reduceStock)
|
||||
// srv.before ('*', (req) => { console.debug ('>>>', req.method, req.target.name) })
|
||||
module.exports = cds.service.impl(function() {
|
||||
this.after ('READ', 'Books', each => each.stock > 111 && _addDiscount2(each,11))
|
||||
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 (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}`)
|
||||
}
|
||||
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}`
|
||||
)
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -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