Compare commits
35 Commits
openSAP-we
...
openSAP-we
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a1a7b1f2b | ||
|
|
a560e12106 | ||
|
|
3dccfc30f0 | ||
|
|
cc0be09341 | ||
|
|
175a183f27 | ||
|
|
a1d663a717 | ||
|
|
e75cc7a0a5 | ||
|
|
37ed881102 | ||
|
|
a3ab8cd51f | ||
|
|
6c1f06a06c | ||
|
|
e9a18c9938 | ||
|
|
eb5334cafc | ||
|
|
d54b16bd94 | ||
|
|
ba054305a6 | ||
|
|
e89fc1d9bb | ||
|
|
659fe52509 | ||
|
|
4ad21a47d6 | ||
|
|
7b5b2210ae | ||
|
|
c20c1609a7 | ||
|
|
ffc9fbd5f5 | ||
|
|
11957c93d8 | ||
|
|
2ca71b0f7d | ||
|
|
452b442246 | ||
|
|
bf21d0220c | ||
|
|
b65be0cb41 | ||
|
|
c3c778b2be | ||
|
|
c38c5d832c | ||
|
|
5929d22f98 | ||
|
|
4030173bbc | ||
|
|
74ce1a5fd1 | ||
|
|
cbb27132ec | ||
|
|
72a17ffcf7 | ||
|
|
0e2218a5f1 | ||
|
|
fa8a7861d5 | ||
|
|
ceb5487e75 |
@@ -6,7 +6,7 @@
|
|||||||
"jest": true
|
"jest": true
|
||||||
},
|
},
|
||||||
"parserOptions": {
|
"parserOptions": {
|
||||||
"ecmaVersion": 2018
|
"ecmaVersion": 2017
|
||||||
},
|
},
|
||||||
"globals": {
|
"globals": {
|
||||||
"SELECT": true,
|
"SELECT": true,
|
||||||
|
|||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -13,3 +13,5 @@ target/
|
|||||||
connection.properties
|
connection.properties
|
||||||
default-env.json
|
default-env.json
|
||||||
packages/messageBox
|
packages/messageBox
|
||||||
|
*.db
|
||||||
|
|
||||||
|
|||||||
39
.vscode/launch.json
vendored
39
.vscode/launch.json
vendored
@@ -5,18 +5,30 @@
|
|||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "bookshop", "request": "launch", "type": "node", "runtimeExecutable": "npx", "runtimeArgs": [ "-n" ],
|
"name": "cds run",
|
||||||
"args": [ "--", "cds", "run", "--in-memory" ],
|
"request": "launch",
|
||||||
"cwd": "${workspaceFolder}/packages/bookshop",
|
"type": "node",
|
||||||
"console": "integratedTerminal",
|
"runtimeExecutable": "npx",
|
||||||
"skipFiles": ["<node_internals>/**"]
|
"runtimeArgs": [
|
||||||
},
|
"-n"
|
||||||
{
|
],
|
||||||
"name": "cds run ...", "request": "launch", "type": "node", "runtimeExecutable": "npx", "runtimeArgs": [ "-n" ],
|
"args": [
|
||||||
"args": [ "--", "cds", "run", "--with-mocks", "--in-memory?" ],
|
"--",
|
||||||
|
"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",
|
||||||
"skipFiles": ["<node_internals>/**"]
|
"serverReadyAction": {
|
||||||
|
"pattern": "server listening on (https?://\\S+|[0-9]+)",
|
||||||
|
"uriFormat": "http://localhost:%s",
|
||||||
|
"action": "openExternally"
|
||||||
|
},
|
||||||
|
"skipFiles": [
|
||||||
|
"<node_internals>/**"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"inputs": [
|
"inputs": [
|
||||||
@@ -29,9 +41,12 @@
|
|||||||
"bookstore",
|
"bookstore",
|
||||||
"media-server",
|
"media-server",
|
||||||
"office-supplies",
|
"office-supplies",
|
||||||
"reviews-service"
|
"orders-service",
|
||||||
|
"products-service",
|
||||||
|
"reviews-service",
|
||||||
|
"user-service"
|
||||||
],
|
],
|
||||||
"default": "bookshop"
|
"default": "bookshop"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"files.exclude": {
|
"files.exclude": {
|
||||||
"**/.gitignore": true,
|
"**/.gitignore": false,
|
||||||
"**/.vscode": true
|
"**/.git": false,
|
||||||
|
"**/.vscode": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
21
.vscode/tasks.json
vendored
21
.vscode/tasks.json
vendored
@@ -4,14 +4,23 @@
|
|||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"tasks": [
|
"tasks": [
|
||||||
{
|
{
|
||||||
"type": "npm", "script": "watch", "path": "packages/bookshop/",
|
"type": "shell", "label": "cds run bookshop",
|
||||||
"options": { "env": { "PORT": "4004" }},
|
"command": "npx", "args": [ "cds", "watch", "packages/bookshop" ],
|
||||||
"presentation": { "group": "A" }
|
"presentation": { "group": "A" },
|
||||||
|
"problemMatcher": []
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "npm", "script": "watch", "path": "packages/reviews-service/",
|
"type": "shell", "label": "cds run bookshop-enhanced",
|
||||||
"options": { "env": { "PORT": "5005" }},
|
"command": "npx", "args": [ "cds", "watch", "packages/bookshop-enhanced" ],
|
||||||
"presentation": { "group": "A" }
|
"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": []
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
24
Jenkinsfile
vendored
24
Jenkinsfile
vendored
@@ -1,24 +0,0 @@
|
|||||||
#!/usr/bin/env groovy
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file bootstraps the codified Continuous Delivery pipeline for extensions of SAP solutions, such as SAP S/4HANA.
|
|
||||||
* The pipeline helps you to deliver software changes quickly and in a reliable manner.
|
|
||||||
* A suitable Jenkins instance is required to run the pipeline.
|
|
||||||
* The Jenkins can easily be bootstraped using the life-cycle script located inside the 'cx-server' directory.
|
|
||||||
*
|
|
||||||
* More information on getting started with Continuous Delivery can be found in the following places:
|
|
||||||
* - GitHub repository: https://github.com/SAP/cloud-s4-sdk-pipeline
|
|
||||||
* - Blog Post: https://blogs.sap.com/2017/09/20/continuous-integration-and-delivery
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Set pipelineVersion to a fixed released version (e.g. "v15") when running in a productive environment.
|
|
||||||
* To find out about available versions and release notes, visit: https://github.com/SAP/cloud-s4-sdk-pipeline/releases
|
|
||||||
*/
|
|
||||||
String pipelineVersion = "v28"
|
|
||||||
|
|
||||||
node {
|
|
||||||
deleteDir()
|
|
||||||
sh "git clone --depth 1 https://github.com/SAP/cloud-s4-sdk-pipeline.git -b ${pipelineVersion} pipelines"
|
|
||||||
load './pipelines/s4sdk-pipeline.groovy'
|
|
||||||
}
|
|
||||||
39
README.md
39
README.md
@@ -2,20 +2,6 @@
|
|||||||
|
|
||||||
Find here the samples for the openSAP course [Building Applications with the SAP Cloud Application Programming Model](https://open.sap.com/courses/cp7).
|
Find here the samples for the openSAP course [Building Applications with the SAP Cloud Application Programming Model](https://open.sap.com/courses/cp7).
|
||||||
|
|
||||||
## Notes on the Demo in Week 4 Unit 4
|
|
||||||
To add all pipeline specific file to your project, run the following command:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
cds add pipeline
|
|
||||||
```
|
|
||||||
|
|
||||||
Details on how to start your Jenkins in your own environment can be found in the [Operations Guide](https://github.com/SAP/devops-docker-cx-server/blob/master/docs/operations/cx-server-operations-guide.md).
|
|
||||||
|
|
||||||
Please note that other than shown in the video Jenkins now is secured by default with an admin user and password.
|
|
||||||
After you have started Jenkins with the command `cx-server start`, you can get the initial password by running `./cx-server initial-credentials`.
|
|
||||||
|
|
||||||
The pipeline documentation can be found [here](https://sap.github.io/jenkins-library/pipelines/cloud-sdk/introduction/).
|
|
||||||
|
|
||||||
## Get Access to SAP Business Application Studio
|
## Get Access to SAP Business Application Studio
|
||||||
The recommended environment for the course is SAP Business Application Studio. Watch [unit 2 of week 1](https://open.sap.com/courses/cp7/items/51pzQUzbXHr2kdbOmVs6jI) for how to get access.
|
The recommended environment for the course is SAP Business Application Studio. Watch [unit 2 of week 1](https://open.sap.com/courses/cp7/items/51pzQUzbXHr2kdbOmVs6jI) for how to get access.
|
||||||
|
|
||||||
@@ -25,8 +11,8 @@ 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 -b openSAP-week4-unit4
|
git clone https://github.com/sap-samples/cloud-cap-samples projects/cloud-cap-samples -b openSAP-week2-unit4567
|
||||||
cd cloud-cap-samples
|
cd projects/cloud-cap-samples
|
||||||
```
|
```
|
||||||
|
|
||||||
In the `cloud-cap-samples` folder run:
|
In the `cloud-cap-samples` folder run:
|
||||||
@@ -34,6 +20,27 @@ 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
|
||||||
|
|
||||||
|
Now you're ready to run the samples, for example:
|
||||||
|
```sh
|
||||||
|
cd packages/bookshop
|
||||||
|
cds watch
|
||||||
|
```
|
||||||
|
|
||||||
|
After that, watch out for the little popup in the lower right corner of SAP Business Application Studio that asks you to open the application in your browser.
|
||||||
|
|
||||||
|
|
||||||
## Get Support
|
## Get Support
|
||||||
|
|
||||||
Check out the cap docs at https://cap.cloud.sap. <br>
|
Check out the cap docs at https://cap.cloud.sap. <br>
|
||||||
|
|||||||
1
lerna.json
Normal file
1
lerna.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"packages":["packages/*"],"version":"1.0.0"}
|
||||||
64
mta.yaml
64
mta.yaml
@@ -1,64 +0,0 @@
|
|||||||
####### Generated mta.yaml based on template version 0.2.0
|
|
||||||
####### appName = capire-bookshop
|
|
||||||
####### language=nodejs; multiTenant=
|
|
||||||
####### approuter=
|
|
||||||
|
|
||||||
_schema-version: '3.1'
|
|
||||||
ID: 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."
|
|
||||||
|
|
||||||
build-parameters:
|
|
||||||
before-all:
|
|
||||||
- builder: custom
|
|
||||||
commands:
|
|
||||||
- npm install
|
|
||||||
- cds build/all
|
|
||||||
|
|
||||||
parameters:
|
|
||||||
enable-parallel-deployments: true
|
|
||||||
|
|
||||||
modules:
|
|
||||||
############## SERVER MODULE ##########################
|
|
||||||
- name: capire-bookshop-srv
|
|
||||||
type: nodejs
|
|
||||||
path: gen/srv
|
|
||||||
properties:
|
|
||||||
EXIT: 1 # required by deploy.js task to terminate
|
|
||||||
|
|
||||||
|
|
||||||
requires:
|
|
||||||
#### Resources extracted from CAP configuration ####
|
|
||||||
- name: capire-bookshop-db
|
|
||||||
|
|
||||||
provides:
|
|
||||||
- name: srv-binding # required by consumers of CAP services (e.g. approuter)
|
|
||||||
properties:
|
|
||||||
srv-url: ${default-url}
|
|
||||||
|
|
||||||
############################################################
|
|
||||||
|
|
||||||
############## SIDECAR MODULE #########################
|
|
||||||
- name: db
|
|
||||||
|
|
||||||
type: hdb
|
|
||||||
path: gen/db
|
|
||||||
parameters:
|
|
||||||
app-name: capire-bookshop-db
|
|
||||||
requires:
|
|
||||||
#### Hana and xsuaa resources extracted from CAP configuration ####
|
|
||||||
- name: capire-bookshop-db
|
|
||||||
############################################################
|
|
||||||
|
|
||||||
|
|
||||||
############## RESOURCES ##################################
|
|
||||||
resources:
|
|
||||||
##### Services extracted from CAP configuration ####
|
|
||||||
##### 'service-plan' can be configured via 'cds.requires.<name>.vcap.plan'
|
|
||||||
- name: capire-bookshop-db
|
|
||||||
type: com.sap.xs.hdi-container
|
|
||||||
|
|
||||||
parameters:
|
|
||||||
properties:
|
|
||||||
hdi-service-name: ${service-name} # required for Java case
|
|
||||||
############################################################
|
|
||||||
5477
package-lock.json
generated
5477
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
41
package.json
41
package.json
@@ -1,29 +1,26 @@
|
|||||||
{
|
{
|
||||||
"name": "@sap/capire-bookshop",
|
"name": "@sap/capire-samples",
|
||||||
"version": "1.0.0",
|
"description": "The umbrella project for all samples to easily setup for local development and tests.",
|
||||||
"description": "A simple bookshop application, build in a self-contained all-in-one fashion, i.e. w/o reusing other packages.",
|
"repository": "https://github.com/SAP-samples/cloud-cap-samples.git",
|
||||||
"license": "SAP SAMPLE CODE LICENSE",
|
"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",
|
||||||
|
"bookshop": "cds watch packages/bookshop"
|
||||||
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sap/cds": "^3",
|
"@sap/cds": "^3",
|
||||||
"express": "^4",
|
"express": "^4"
|
||||||
"@sap/hana-client": "^2.4.182"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"sqlite3": "^4.1.1"
|
"@types/jest": "*",
|
||||||
|
"sqlite3": "*",
|
||||||
|
"jest": "*",
|
||||||
|
"supertest": "^4.0.2",
|
||||||
|
"@sap/hdi-deploy": "3.7.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"license": "SAP SAMPLE CODE LICENSE"
|
||||||
"start": "cds run",
|
|
||||||
"watch": "cds watch"
|
|
||||||
},
|
|
||||||
"cds": {
|
|
||||||
"requires": {
|
|
||||||
"db": {
|
|
||||||
"kind": "hana",
|
|
||||||
"model": [
|
|
||||||
"db",
|
|
||||||
"srv"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
31
packages/bookshop/.vscode/launch.json
vendored
Normal file
31
packages/bookshop/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
// 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\"}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
18
packages/bookshop/package.json
Normal file
18
packages/bookshop/package.json
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"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"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,9 +8,8 @@ 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 -> https://github.wdf.sap.corp/cap/issues/issues/3121
|
// Temporary workaround -> cap/issues#3121
|
||||||
extend service AdminService with {
|
extend service AdminService with {
|
||||||
entity OrderItems as select from my.OrderItems;
|
entity OrderItems as select from my.OrderItems;
|
||||||
}
|
}
|
||||||
22
packages/bookshop/srv/admin-service.js
Normal file
22
packages/bookshop/srv/admin-service.js
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
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,6 +1,7 @@
|
|||||||
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 {*,
|
||||||
28
packages/bookshop/srv/cat-service.js
Normal file
28
packages/bookshop/srv/cat-service.js
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
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) })
|
||||||
|
})
|
||||||
|
|
||||||
|
/** 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}`)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
79
packages/bookshop/tests/bookshop.test.js
Normal file
79
packages/bookshop/tests/bookshop.test.js
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
|
||||||
|
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,47 +0,0 @@
|
|||||||
###
|
|
||||||
# This file configures the SAP Cloud SDK Continuous Delivery pipeline of your project.
|
|
||||||
# For a reference of the configuration concept and available options, please have a look into its documentation.
|
|
||||||
#
|
|
||||||
# The documentation for the most recent pipeline version can always be found at:
|
|
||||||
# https://github.com/SAP/cloud-s4-sdk-pipeline/blob/master/configuration.md
|
|
||||||
# If you are using a fixed version of the pipeline, please make sure to view the corresponding version from the tag
|
|
||||||
# list of GitHub (e.g. "v15" when you configured pipelineVersion = "v15" in the Jenkinsfile).
|
|
||||||
#
|
|
||||||
# For general information on how to get started with Continuous Delivery, visit:
|
|
||||||
# https://blogs.sap.com/2017/09/20/continuous-integration-and-delivery
|
|
||||||
#
|
|
||||||
# We aim to keep the pipeline configuration as stable as possible. However, major changes might also imply breaking
|
|
||||||
# changes in the configuration. Before doing an update, please check the the release notes of all intermediate releases
|
|
||||||
# and adapt this file if necessary.
|
|
||||||
#
|
|
||||||
# This is a YAML-file. YAML is a indentation-sensitive file format. Please make sure to properly indent changes to it.
|
|
||||||
###
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### General project setup
|
|
||||||
general:
|
|
||||||
productiveBranch: 'openSAP-week4-unit4'
|
|
||||||
|
|
||||||
### Step-specific configuration
|
|
||||||
steps:
|
|
||||||
|
|
||||||
|
|
||||||
### Stage-specific configuration
|
|
||||||
stages:
|
|
||||||
|
|
||||||
# integrationTests:
|
|
||||||
# credentials:
|
|
||||||
# - alias: 'mySystemAlias'
|
|
||||||
# credentialId: 'mySystemCredentialsId'
|
|
||||||
|
|
||||||
# s4SdkQualityChecks:
|
|
||||||
# nonErpDestinations:
|
|
||||||
# - 'myCustomDestination'
|
|
||||||
|
|
||||||
# productionDeployment:
|
|
||||||
# cfTargets:
|
|
||||||
# - org: 'myOrg'
|
|
||||||
# space: 'mySpace'
|
|
||||||
# apiEndpoint: 'https://api.cf.eu10.hana.ondemand.com'
|
|
||||||
# credentialsId: 'cf-deploy-sap'
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
const cds = require('@sap/cds')
|
|
||||||
const { Books } = cds.entities
|
|
||||||
|
|
||||||
/** Service implementation for CatalogService */
|
|
||||||
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 (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}`
|
|
||||||
)
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user