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
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2017
|
||||
"ecmaVersion": 2018
|
||||
},
|
||||
"globals": {
|
||||
"SELECT": true,
|
||||
|
||||
27
.vscode/launch.json
vendored
27
.vscode/launch.json
vendored
@@ -5,19 +5,17 @@
|
||||
"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>/**"]
|
||||
}
|
||||
],
|
||||
@@ -31,12 +29,9 @@
|
||||
"bookstore",
|
||||
"media-server",
|
||||
"office-supplies",
|
||||
"orders-service",
|
||||
"products-service",
|
||||
"reviews-service",
|
||||
"user-service"
|
||||
"reviews-service"
|
||||
],
|
||||
"default": "bookstore"
|
||||
"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" }
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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-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
|
||||
```
|
||||
|
||||
|
||||
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",
|
||||
"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-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"
|
||||
"install": "(npm -s run lerna) && lerna bootstrap --hoist",
|
||||
"cleanup": "lerna clean -y && rm -fr node_modules",
|
||||
"bookshop": "cds watch packages/bookshop"
|
||||
},
|
||||
"dependencies": {
|
||||
"@sap/cds": "^3",
|
||||
"express": "^4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"sqlite3": "*"
|
||||
},
|
||||
"--add-these-to-devDependencies-for-tests": {
|
||||
"@types/jest": "*",
|
||||
"sqlite3": "*",
|
||||
"jest": "*"
|
||||
},
|
||||
"license": "SAP SAMPLE CODE LICENSE"
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
<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-compatVersion="edge"
|
||||
data-sap-ui-theme="sap_belize"
|
||||
data-sap-ui-theme="sap_fiori_3"
|
||||
data-sap-ui-frameOptions="allow"
|
||||
></script>
|
||||
<script>
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
ID;modifiedAt;createdAt;createdBy;modifiedBy;OrderNo;currency_code;status
|
||||
da86efd0-4ba1-4078-b7f0-5c9c530297f7;;2019-01-31;ALICE;;1;EUR;processing
|
||||
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
|
||||
ID;modifiedAt;createdAt;createdBy;modifiedBy;OrderNo;currency_code
|
||||
7e2f2640-6866-4dcf-8f4d-3027aa831cad;;2019-01-31;john.doe@test.com;;1;EUR
|
||||
64e718c9-ff99-47f1-8ca3-950c850777d4;;2019-01-30;jane.doe@test.com;;2;EUR
|
||||
|
@@ -1,50 +1,36 @@
|
||||
namespace sap.capire.bookshop;
|
||||
|
||||
using {
|
||||
Currency,
|
||||
managed,
|
||||
cuid
|
||||
} from '@sap/cds/common';
|
||||
|
||||
type Status : String enum {
|
||||
completed;
|
||||
processing;
|
||||
blocked;
|
||||
}
|
||||
using { Currency, managed, cuid } from '@sap/cds/common';
|
||||
|
||||
entity Books : managed {
|
||||
key ID : Integer;
|
||||
title : localized String(111);
|
||||
descr : localized String(1111);
|
||||
author : Association to Authors;
|
||||
stock : Integer;
|
||||
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;
|
||||
key ID : Integer;
|
||||
title : localized String(111);
|
||||
descr : localized String(1111);
|
||||
author : Association to Authors;
|
||||
stock : Integer;
|
||||
price : Decimal(9,2);
|
||||
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 {
|
||||
parent : Association to Orders;
|
||||
book : Association to Books;
|
||||
amount : Integer;
|
||||
netAmount : Decimal(9, 2);
|
||||
}
|
||||
netAmount : Decimal(9,2);
|
||||
}
|
||||
|
||||
@@ -5,24 +5,17 @@
|
||||
"license": "SAP SAMPLE CODE LICENSE",
|
||||
"dependencies": {
|
||||
"@sap/cds": "^3",
|
||||
"express": "^4",
|
||||
"@sap/xb-msg-amqp-v100": "^0.9.35"
|
||||
},
|
||||
"devDependencies": {
|
||||
"sqlite3": "^4.1.1"
|
||||
"express": "^4"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "npx cds run"
|
||||
"start": "cds run --in-memory?",
|
||||
"watch": "cds watch"
|
||||
},
|
||||
"cds": {
|
||||
"requires": {
|
||||
"API_BUSINESS_PARTNER": {
|
||||
"kind": "odata",
|
||||
"model": "srv/external/API_BUSINESS_PARTNER",
|
||||
"--credentials": {
|
||||
"destination": "cap-api098"
|
||||
}
|
||||
"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;
|
||||
@@ -10,7 +10,7 @@ service AdminService @(_requires:'authenticated-user') {
|
||||
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 {
|
||||
entity OrderItems as select from my.OrderItems;
|
||||
}
|
||||
|
||||
@@ -1,20 +1,9 @@
|
||||
using { sap.capire.bookshop as my } from '../db/schema';
|
||||
using { API_BUSINESS_PARTNER as external } from './external/API_BUSINESS_PARTNER.csn';
|
||||
|
||||
@path:'/browse'
|
||||
service CatalogService {
|
||||
|
||||
@readonly entity Books as SELECT from my.Books {*,
|
||||
author.name as author
|
||||
} excluding { createdBy, modifiedBy };
|
||||
|
||||
@readonly entity BusinessPartners as projection on external.A_BusinessPartner {
|
||||
key BusinessPartner as ID,
|
||||
FirstName,
|
||||
MiddleName,
|
||||
LastName,
|
||||
BusinessPartnerIsBlocked
|
||||
};
|
||||
@readonly entity Books as SELECT from my.Books { * } excluding { createdBy, modifiedBy };
|
||||
|
||||
@requires_: 'authenticated-user'
|
||||
@insertonly entity Orders as projection on my.Orders;
|
||||
|
||||
@@ -1,28 +1,26 @@
|
||||
const cds = require('@sap/cds')
|
||||
const { Books } = cds.entities
|
||||
|
||||
/** Service implementation for CatalogService */
|
||||
module.exports = cds.service.impl(async function () {
|
||||
const { Books, Orders, BusinessPartners } = this.entities
|
||||
const bupaSrv = await cds.connect.to('API_BUSINESS_PARTNER')
|
||||
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}`
|
||||
)
|
||||
}))
|
||||
}
|
||||
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}`
|
||||
)
|
||||
}))
|
||||
}
|
||||
|
||||
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
|
||||
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,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