Compare commits
14 Commits
openSAP-we
...
openSAP-we
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
62f32abf74 | ||
|
|
536282388c | ||
|
|
75e767ed5c | ||
|
|
ba1eead6db | ||
|
|
8233703815 | ||
|
|
6a9a1bc4d6 | ||
|
|
a0847e603f | ||
|
|
b1270bc0eb | ||
|
|
3a77707f49 | ||
|
|
583c97a494 | ||
|
|
eb7431afed | ||
|
|
c0bce5ae5b | ||
|
|
ad05e2b9db | ||
|
|
b7c2eee961 |
@@ -6,7 +6,7 @@
|
|||||||
"jest": true
|
"jest": true
|
||||||
},
|
},
|
||||||
"parserOptions": {
|
"parserOptions": {
|
||||||
"ecmaVersion": 2017
|
"ecmaVersion": 2018
|
||||||
},
|
},
|
||||||
"globals": {
|
"globals": {
|
||||||
"SELECT": true,
|
"SELECT": true,
|
||||||
|
|||||||
27
.vscode/launch.json
vendored
27
.vscode/launch.json
vendored
@@ -5,19 +5,17 @@
|
|||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "cds run",
|
"name": "bookshop", "request": "launch", "type": "node", "runtimeExecutable": "npx", "runtimeArgs": [ "-n" ],
|
||||||
"request": "launch",
|
"args": [ "--", "cds", "run", "--in-memory" ],
|
||||||
"type": "node",
|
"cwd": "${workspaceFolder}/packages/bookshop",
|
||||||
"runtimeExecutable": "npx",
|
"console": "integratedTerminal",
|
||||||
"runtimeArgs": ["-n"],
|
"skipFiles": ["<node_internals>/**"]
|
||||||
"args": ["--", "cds", "run", "--with-mocks", "--in-memory?"], // the leading "--" arg ensures it works with as well as without debugging
|
},
|
||||||
|
{
|
||||||
|
"name": "cds run ...", "request": "launch", "type": "node", "runtimeExecutable": "npx", "runtimeArgs": [ "-n" ],
|
||||||
|
"args": [ "--", "cds", "run", "--with-mocks", "--in-memory?" ],
|
||||||
"cwd": "${workspaceFolder}/packages/${input:service}",
|
"cwd": "${workspaceFolder}/packages/${input:service}",
|
||||||
"console": "integratedTerminal",
|
"console": "integratedTerminal",
|
||||||
"serverReadyAction": {
|
|
||||||
"pattern": "server listening on (https?://\\S+|[0-9]+)",
|
|
||||||
"uriFormat": "http://localhost:%s",
|
|
||||||
"action": "openExternally"
|
|
||||||
},
|
|
||||||
"skipFiles": ["<node_internals>/**"]
|
"skipFiles": ["<node_internals>/**"]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -31,12 +29,9 @@
|
|||||||
"bookstore",
|
"bookstore",
|
||||||
"media-server",
|
"media-server",
|
||||||
"office-supplies",
|
"office-supplies",
|
||||||
"orders-service",
|
"reviews-service"
|
||||||
"products-service",
|
|
||||||
"reviews-service",
|
|
||||||
"user-service"
|
|
||||||
],
|
],
|
||||||
"default": "bookstore"
|
"default": "bookshop"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@@ -1,7 +1,6 @@
|
|||||||
{
|
{
|
||||||
"files.exclude": {
|
"files.exclude": {
|
||||||
"**/.gitignore": false,
|
"**/.gitignore": true,
|
||||||
"**/.git": false,
|
"**/.vscode": true
|
||||||
"**/.vscode": false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
21
.vscode/tasks.json
vendored
21
.vscode/tasks.json
vendored
@@ -4,23 +4,14 @@
|
|||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"tasks": [
|
"tasks": [
|
||||||
{
|
{
|
||||||
"type": "shell", "label": "cds run bookshop",
|
"type": "npm", "script": "watch", "path": "packages/bookshop/",
|
||||||
"command": "npx", "args": [ "cds", "watch", "packages/bookshop" ],
|
"options": { "env": { "PORT": "4004" }},
|
||||||
"presentation": { "group": "A" },
|
"presentation": { "group": "A" }
|
||||||
"problemMatcher": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "shell", "label": "cds run bookshop-enhanced",
|
"type": "npm", "script": "watch", "path": "packages/reviews-service/",
|
||||||
"command": "npx", "args": [ "cds", "watch", "packages/bookshop-enhanced" ],
|
"options": { "env": { "PORT": "5005" }},
|
||||||
"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": []
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -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-week4-unit2-final
|
git clone https://github.com/sap-samples/cloud-cap-samples projects/cloud-cap-samples -b openSAP-week2-unit3
|
||||||
cd projects/cloud-cap-samples
|
cd projects/cloud-cap-samples
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
836
package-lock.json
generated
836
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
15
package.json
15
package.json
@@ -5,23 +5,20 @@
|
|||||||
"author": "daniel.hutzel@sap.com",
|
"author": "daniel.hutzel@sap.com",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"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",
|
"lerna": "npx --no-install lerna -v > /dev/null || npm i lerna --no-save",
|
||||||
"test": "jest",
|
"install": "(npm -s run lerna) && lerna bootstrap --hoist",
|
||||||
"bookshop-enhanced": "cds watch packages/bookshop-enhanced",
|
"cleanup": "lerna clean -y && rm -fr node_modules",
|
||||||
"bookshop": "cds watch packages/bookshop",
|
"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": {
|
||||||
|
"sqlite3": "*"
|
||||||
|
},
|
||||||
"--add-these-to-devDependencies-for-tests": {
|
"--add-these-to-devDependencies-for-tests": {
|
||||||
"@types/jest": "*",
|
"@types/jest": "*",
|
||||||
"sqlite3": "*",
|
|
||||||
"jest": "*"
|
"jest": "*"
|
||||||
},
|
},
|
||||||
"license": "SAP SAMPLE CODE LICENSE"
|
"license": "SAP SAMPLE CODE LICENSE"
|
||||||
|
|||||||
@@ -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_belize"
|
data-sap-ui-theme="sap_fiori_3"
|
||||||
data-sap-ui-frameOptions="allow"
|
data-sap-ui-frameOptions="allow"
|
||||||
></script>
|
></script>
|
||||||
<script>
|
<script>
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
ID;modifiedAt;createdAt;createdBy;modifiedBy;OrderNo;currency_code;status
|
ID;modifiedAt;createdAt;createdBy;modifiedBy;OrderNo;currency_code
|
||||||
da86efd0-4ba1-4078-b7f0-5c9c530297f7;;2019-01-31;ALICE;;1;EUR;processing
|
7e2f2640-6866-4dcf-8f4d-3027aa831cad;;2019-01-31;john.doe@test.com;;1;EUR
|
||||||
2f2f2640-6866-4dcf-8f4d-3027aa831cad;;2019-03-25;ALICE;;10;EUR;completed
|
64e718c9-ff99-47f1-8ca3-950c850777d4;;2019-01-30;jane.doe@test.com;;2;EUR
|
||||||
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,50 +1,36 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
entity Authors : managed {
|
|
||||||
key ID : Integer;
|
|
||||||
name : String(111);
|
|
||||||
dateOfBirth : Date;
|
|
||||||
dateOfDeath : Date;
|
|
||||||
placeOfBirth : String;
|
|
||||||
placeOfDeath : String;
|
|
||||||
books : Association to many Books
|
|
||||||
on books.author = $self;
|
|
||||||
}
|
|
||||||
|
|
||||||
entity Orders : cuid, managed {
|
|
||||||
OrderNo : String @title : 'Order Number'; //> readable key
|
|
||||||
status : Status default 'processing';
|
|
||||||
Items : Composition of many OrderItems
|
|
||||||
on Items.parent = $self;
|
|
||||||
total : Decimal(9, 2)@readonly;
|
|
||||||
currency : Currency;
|
currency : Currency;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@cds.autoexpose
|
||||||
|
entity Authors : managed {
|
||||||
|
key ID : Integer;
|
||||||
|
name : String(111);
|
||||||
|
dateOfBirth : Date;
|
||||||
|
dateOfDeath : Date;
|
||||||
|
placeOfBirth : String;
|
||||||
|
placeOfDeath : String;
|
||||||
|
books : Association to many Books on books.author = $self;
|
||||||
|
}
|
||||||
|
|
||||||
|
entity Orders : cuid, managed {
|
||||||
|
OrderNo : String @title:'Order Number'; //> readable key
|
||||||
|
Items : Composition of many OrderItems on Items.parent = $self;
|
||||||
|
total : Decimal(9,2) @readonly;
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,24 +5,17 @@
|
|||||||
"license": "SAP SAMPLE CODE LICENSE",
|
"license": "SAP SAMPLE CODE LICENSE",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sap/cds": "^3",
|
"@sap/cds": "^3",
|
||||||
"express": "^4",
|
"express": "^4"
|
||||||
"@sap/xb-msg-amqp-v100": "^0.9.35"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"sqlite3": "^4.1.1"
|
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "npx cds run"
|
"start": "cds run --in-memory?",
|
||||||
|
"watch": "cds watch"
|
||||||
},
|
},
|
||||||
"cds": {
|
"cds": {
|
||||||
"requires": {
|
"requires": {
|
||||||
"API_BUSINESS_PARTNER": {
|
"db": {
|
||||||
"kind": "odata",
|
"kind": "sql"
|
||||||
"model": "srv/external/API_BUSINESS_PARTNER",
|
|
||||||
"--credentials": {
|
|
||||||
"destination": "cap-api098"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
using { sap.capire.bookshop as my } from '../db/schema';
|
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 Books as projection on my.Books;
|
||||||
entity Authors as projection on my.Authors;
|
entity Authors as projection on my.Authors;
|
||||||
entity Orders as select from my.Orders;
|
entity Orders as select from my.Orders;
|
||||||
@@ -10,7 +10,7 @@ service AdminService @(_requires:'authenticated-user') {
|
|||||||
annotate AdminService.Orders with @odata.draft.enabled;
|
annotate AdminService.Orders with @odata.draft.enabled;
|
||||||
// annotate AdminService.Books 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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,9 @@
|
|||||||
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'
|
||||||
service CatalogService {
|
service CatalogService {
|
||||||
|
|
||||||
@readonly entity Books as SELECT from my.Books {*,
|
@readonly entity Books as SELECT from my.Books { * } excluding { createdBy, modifiedBy };
|
||||||
author.name as author
|
|
||||||
} excluding { createdBy, modifiedBy };
|
|
||||||
|
|
||||||
@readonly entity BusinessPartners as projection on external.A_BusinessPartner {
|
|
||||||
key BusinessPartner as ID,
|
|
||||||
FirstName,
|
|
||||||
MiddleName,
|
|
||||||
LastName,
|
|
||||||
BusinessPartnerIsBlocked
|
|
||||||
};
|
|
||||||
|
|
||||||
@requires_: 'authenticated-user'
|
@requires_: 'authenticated-user'
|
||||||
@insertonly entity Orders as projection on my.Orders;
|
@insertonly entity Orders as projection on my.Orders;
|
||||||
|
|||||||
@@ -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(async function () {
|
module.exports = cds.service.impl(function() {
|
||||||
const { Books, Orders, BusinessPartners } = this.entities
|
this.after ('READ', 'Books', each => each.stock > 111 && _addDiscount2(each,11))
|
||||||
const bupaSrv = await cds.connect.to('API_BUSINESS_PARTNER')
|
this.before ('CREATE', 'Orders', _reduceStock)
|
||||||
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))
|
|
||||||
|
|
||||||
/** 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 (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}`
|
||||||
|
)
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|||||||
2426
packages/bookshop/srv/external/API_BUSINESS_PARTNER.csn
vendored
2426
packages/bookshop/srv/external/API_BUSINESS_PARTNER.csn
vendored
File diff suppressed because it is too large
Load Diff
3261
packages/bookshop/srv/external/API_BUSINESS_PARTNER.edmx
vendored
3261
packages/bookshop/srv/external/API_BUSINESS_PARTNER.edmx
vendored
File diff suppressed because it is too large
Load Diff
@@ -1,4 +0,0 @@
|
|||||||
BusinessPartner;FirstName;MiddleName;LastName;BusinessPartnerIsBlocked
|
|
||||||
ALICE;Alice;In;Wonderland;false
|
|
||||||
BOB;Bob;The;Builder;false
|
|
||||||
JABBA;Jabba;The;Hutt;true
|
|
||||||
|
@@ -1,18 +1,14 @@
|
|||||||
### Service Document
|
### Browse Books
|
||||||
GET http://localhost:4004/browse
|
GET http://localhost:4004/browse/Books
|
||||||
|
|
||||||
### Service $metadata document
|
### Browse Books with expanded Authors
|
||||||
GET http://localhost:4004/browse/$metadata
|
GET http://localhost:4004/browse/Books?$expand=author
|
||||||
|
|
||||||
### Browsing Books
|
### Try to insert into Books
|
||||||
GET http://localhost:4004/browse/Books?
|
POST http://localhost:4004/browse/Books
|
||||||
# &$select=title,author
|
Content-Type: application/json
|
||||||
# &$expand=currency
|
|
||||||
# &sap-language=de
|
|
||||||
|
|
||||||
### Browsing Authors
|
{
|
||||||
GET http://localhost:4004/admin/Authors?
|
"title": "Anna Karenina",
|
||||||
# &$select=name,dateOfBirth,placeOfBirth
|
"stock": 10
|
||||||
# &$expand=books($select=title;$expand=currency)
|
}
|
||||||
# &$filter=ID eq 101
|
|
||||||
# &sap-language=de
|
|
||||||
@@ -1,18 +1,15 @@
|
|||||||
|
### List all Orders - deep read
|
||||||
### List Books with their current stocks
|
|
||||||
GET http://localhost:4004/admin/Books?$select=ID,stock
|
|
||||||
|
|
||||||
### List all Orders
|
|
||||||
GET http://localhost:4004/admin/Orders?
|
GET http://localhost:4004/admin/Orders?
|
||||||
&$expand=Items
|
&$expand=Items
|
||||||
|
|
||||||
### Submit Orders
|
### Submit Orders - deep insert
|
||||||
POST http://localhost:4004/browse/Orders
|
POST http://localhost:4004/browse/Orders
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|
||||||
{ "OrderNo":"2019-09...", "Items":[
|
{ "OrderNo":"1234", "Items":[
|
||||||
{ "book_ID":201, "amount":5 },
|
{ "book_ID":201, "amount":5 },
|
||||||
{ "book_ID":207, "amount":3 }
|
{ "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