Compare commits
11 Commits
openSAP-we
...
openSAP-we
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
62f32abf74 | ||
|
|
536282388c | ||
|
|
75e767ed5c | ||
|
|
ba1eead6db | ||
|
|
8233703815 | ||
|
|
6a9a1bc4d6 | ||
|
|
a0847e603f | ||
|
|
b1270bc0eb | ||
|
|
3a77707f49 | ||
|
|
583c97a494 | ||
|
|
eb7431afed |
@@ -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
|
||||
|
||||
|
||||
39
.vscode/launch.json
vendored
39
.vscode/launch.json
vendored
@@ -5,30 +5,18 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "cds run",
|
||||
"request": "launch",
|
||||
"type": "node",
|
||||
"runtimeExecutable": "npx",
|
||||
"runtimeArgs": [
|
||||
"-n"
|
||||
],
|
||||
"args": [
|
||||
"--",
|
||||
"cds",
|
||||
"run",
|
||||
"--with-mocks",
|
||||
"--in-memory"
|
||||
], // the leading "--" arg ensures it works with as well as without debugging
|
||||
"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", "runtimeArgs": [ "-n" ],
|
||||
"args": [ "--", "cds", "run", "--with-mocks", "--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>/**"
|
||||
]
|
||||
"skipFiles": ["<node_internals>/**"]
|
||||
}
|
||||
],
|
||||
"inputs": [
|
||||
@@ -41,12 +29,9 @@
|
||||
"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-week2-unit3
|
||||
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\"}"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -11,6 +11,7 @@ entity Books : managed {
|
||||
currency : Currency;
|
||||
}
|
||||
|
||||
@cds.autoexpose
|
||||
entity Authors : managed {
|
||||
key ID : Integer;
|
||||
name : String(111);
|
||||
|
||||
@@ -5,14 +5,17 @@
|
||||
"license": "SAP SAMPLE CODE LICENSE",
|
||||
"dependencies": {
|
||||
"@sap/cds": "^3",
|
||||
"express": "^4",
|
||||
"hdb": "^0.17.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jest": "*"
|
||||
"express": "^4"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "npx cds run",
|
||||
"test": "jest"
|
||||
"start": "cds run --in-memory?",
|
||||
"watch": "cds watch"
|
||||
},
|
||||
"cds": {
|
||||
"requires": {
|
||||
"db": {
|
||||
"kind": "sql"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using { sap.capire.bookshop as my } from '../db/schema';
|
||||
|
||||
service AdminService @(_requires:'authenticated-user') {
|
||||
service AdminService @(_requires:'authenticated-user',path:'/admin') {
|
||||
entity Books as projection on my.Books;
|
||||
entity Authors as projection on my.Authors;
|
||||
entity Orders as select from my.Orders;
|
||||
@@ -8,6 +8,7 @@ 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
|
||||
extend service AdminService with {
|
||||
|
||||
@@ -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,12 +1,9 @@
|
||||
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 {*,
|
||||
author.name as author
|
||||
} excluding { createdBy, modifiedBy };
|
||||
@readonly entity Books as SELECT from my.Books { * } excluding { createdBy, modifiedBy };
|
||||
|
||||
@requires_: 'authenticated-user'
|
||||
@insertonly entity Orders as projection on my.Orders;
|
||||
|
||||
@@ -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,18 +1,14 @@
|
||||
### Service Document
|
||||
GET http://localhost:4004/browse
|
||||
### Browse Books
|
||||
GET http://localhost:4004/browse/Books
|
||||
|
||||
### Service $metadata document
|
||||
GET http://localhost:4004/browse/$metadata
|
||||
### Browse Books with expanded Authors
|
||||
GET http://localhost:4004/browse/Books?$expand=author
|
||||
|
||||
### Browsing Books
|
||||
GET http://localhost:4004/browse/Books?
|
||||
# &$select=title,author
|
||||
# &$expand=currency
|
||||
# &sap-language=de
|
||||
### Try to insert into Books
|
||||
POST http://localhost:4004/browse/Books
|
||||
Content-Type: application/json
|
||||
|
||||
### Browsing Authors
|
||||
GET http://localhost:4004/admin/Authors?
|
||||
# &$select=name,dateOfBirth,placeOfBirth
|
||||
# &$expand=books($select=title;$expand=currency)
|
||||
# &$filter=ID eq 101
|
||||
# &sap-language=de
|
||||
{
|
||||
"title": "Anna Karenina",
|
||||
"stock": 10
|
||||
}
|
||||
@@ -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' }
|
||||
])
|
||||
})
|
||||
})
|
||||
@@ -1,18 +1,15 @@
|
||||
|
||||
### List Books with their current stocks
|
||||
GET http://localhost:4004/admin/Books?$select=ID,stock
|
||||
|
||||
### List all Orders
|
||||
### List all Orders - deep read
|
||||
GET http://localhost:4004/admin/Orders?
|
||||
&$expand=Items
|
||||
|
||||
### Submit Orders
|
||||
### Submit Orders - deep insert
|
||||
POST http://localhost:4004/browse/Orders
|
||||
Content-Type: application/json
|
||||
|
||||
{ "OrderNo":"2019-09...", "Items":[
|
||||
{ "OrderNo":"1234", "Items":[
|
||||
{ "book_ID":201, "amount":5 },
|
||||
{ "book_ID":207, "amount":3 }
|
||||
]}
|
||||
|
||||
# Sending this three times should result in a 409: 5 exceeds stock for book #201
|
||||
### Try to get the Orders
|
||||
GET http://localhost:4004/browse/Orders
|
||||
Reference in New Issue
Block a user