Compare commits
7 Commits
openSAP-we
...
CAA265-nod
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
162bdb5b90 | ||
|
|
fb8f6acac7 | ||
|
|
1e28cb217f | ||
|
|
2fc2cea260 | ||
|
|
bfaaf36e14 | ||
|
|
33b24c8d96 | ||
|
|
387b25b1bf |
59
README.md
59
README.md
@@ -1,58 +1,3 @@
|
||||
# cloud-cap-samples
|
||||
|
||||
This is a monorepository for sample projects on [SAP Cloud Application Programming Model](https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/00823f91779d4d42aa29a498e0535cdf.html?q=cloud%20application%20programming%20model).
|
||||
|
||||
## Description
|
||||
|
||||
This repository provides a list of samples and reusable packages created based on SAP Cloud Application Programming Model.
|
||||
The SAP Cloud Application Programming Model enables you to quickly create business applications by allowing you to focus on your domain logic. It offers a consistent end-to-end programming model that includes languages, libraries and APIs tailored for full-stack development on SAP Cloud Platform.
|
||||
|
||||
The samples provided can be run in a local setup on SQLite Database.
|
||||
|
||||
#### Samples:
|
||||
* [bookstore](./packages/bookstore) - A variant of the bookshop application, built in a modular fashion on top of products-service and common reuse packages.
|
||||
* [products-service](./packages/products-service) - A reuse package providing domain models and services to manage product catalogs.
|
||||
|
||||
## Requirements
|
||||
* [Node.js](https://nodejs.org/en/) v8 or higher
|
||||
* [Git](https://git-scm.com)
|
||||
* [SQLite DB](https://www.sqlite.org/download.html) (Windows only; pre-installed on Mac/Linux)
|
||||
|
||||
#### Optional (if you want to import the code into an editor)
|
||||
* [VS Code](https://code.visualstudio.com)
|
||||
* [Add CDS extension to VS](https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/be944d6d51f343f6b3f53c29c44ff00a.html)
|
||||
|
||||
## Download and Installation
|
||||
|
||||
#### Clone and build the application
|
||||
`git clone https://github.com/SAP-samples/cloud-cap-samples.git`
|
||||
|
||||
`cd samples`
|
||||
|
||||
`npm install`
|
||||
|
||||
#### Run the samples
|
||||
|
||||
`npm run <sample name>`
|
||||
|
||||
|
||||
## Limitations
|
||||
|
||||
None
|
||||
|
||||
## Known Issues
|
||||
|
||||
None
|
||||
|
||||
## How to obtain support
|
||||
|
||||
In case you find a bug, or you need additional support, please open an issue [here](https://github.wdf.sap.corp/staging-for-SAP-samples-public/cloud-cap-samples/issues) in GitHub.
|
||||
|
||||
## To-Do (upcoming changes)
|
||||
|
||||
None
|
||||
|
||||
## License
|
||||
|
||||
Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under SAP Sample Code License Agreement, except as noted otherwise in the [LICENSE](/LICENSE) file.
|
||||
# Final state after exercise 2 for CAA265 - Rapid Service Development with SAP Cloud Application Programming Model
|
||||
|
||||
**DO NOT MERGE IN MASTER**
|
||||
|
||||
5
packages/bookstore/db/csv/sap.capire.bookstore-Authors.csv
Executable file
5
packages/bookstore/db/csv/sap.capire.bookstore-Authors.csv
Executable file
@@ -0,0 +1,5 @@
|
||||
ID;name;dateOfBirth;placeOfBirth;dateOfDeath;placeOfDeath
|
||||
101;Emily Brontë;1818-07-30;Thornton, Yorkshire;1848-12-19;Haworth, Yorkshire
|
||||
107;Charlotte Brontë;1818-04-21;Thornton, Yorkshire;1855-03-31;Haworth, Yorkshire
|
||||
150;Edgar Allen Poe;1809-01-19;Boston, Massachusetts;1849-10-07;Baltimore, Maryland
|
||||
170;Richard Carpenter;1929-08-14;King’s Lynn, Norfolk;2012-02-26;Hertfordshire, England
|
||||
|
4
packages/bookstore/db/csv/sap.capire.bookstore-OrderItems.csv
Executable file
4
packages/bookstore/db/csv/sap.capire.bookstore-OrderItems.csv
Executable file
@@ -0,0 +1,4 @@
|
||||
ID;amount;parent_ID;book_ID;netAmount
|
||||
301;1;401;201;11.11
|
||||
302;1;401;271;15
|
||||
303;2;402;252;28
|
||||
|
3
packages/bookstore/db/csv/sap.capire.bookstore-Orders.csv
Executable file
3
packages/bookstore/db/csv/sap.capire.bookstore-Orders.csv
Executable file
@@ -0,0 +1,3 @@
|
||||
ID;modifiedAt;createdAt;createdBy;modifiedBy;OrderNo;currency_code
|
||||
401;;2019-01-31;john.doe@test.com;;1;EUR
|
||||
402;;2019-01-30;christian.georgi@sap.com;;2;EUR
|
||||
|
11
packages/bookstore/db/csv/sap.capire.products-Categories.csv
Executable file
11
packages/bookstore/db/csv/sap.capire.products-Categories.csv
Executable file
@@ -0,0 +1,11 @@
|
||||
ID;parent_ID;name
|
||||
1;;Poetry
|
||||
2;;Biography
|
||||
3;;Fantasy
|
||||
4;;Science Fiction
|
||||
5;;Romance
|
||||
6;;Mystery
|
||||
7;;Thriller
|
||||
8;;Dystopia
|
||||
9;;Tragedy
|
||||
10;;Novel
|
||||
|
6
packages/bookstore/db/csv/sap.capire.products-Products.csv
Executable file
6
packages/bookstore/db/csv/sap.capire.products-Products.csv
Executable file
@@ -0,0 +1,6 @@
|
||||
ID;title;descr;author_ID;stock;price;currency_code;category_ID
|
||||
201;Wuthering Heights;"Wuthering Heights, Emily Brontë's only novel, was published in 1847 under the pseudonym ""Ellis Bell"". It was written between October 1845 and June 1846. Wuthering Heights and Anne Brontë's Agnes Grey were accepted by publisher Thomas Newby before the success of their sister Charlotte's novel Jane Eyre. After Emily's death, Charlotte edited the manuscript of Wuthering Heights and arranged for the edited version to be published as a posthumous second edition in 1850.";101;12;11.11;GBP;9
|
||||
207;Jane Eyre;"Jane Eyre /ɛər/ (originally published as Jane Eyre: An Autobiography) is a novel by English writer Charlotte Brontë, published under the pen name ""Currer Bell"", on 16 October 1847, by Smith, Elder & Co. of London. The first American edition was published the following year by Harper & Brothers of New York. Primarily a bildungsroman, Jane Eyre follows the experiences of its eponymous heroine, including her growth to adulthood and her love for Mr. Rochester, the brooding master of Thornfield Hall. The novel revolutionised prose fiction in that the focus on Jane's moral and spiritual development is told through an intimate, first-person narrative, where actions and events are coloured by a psychological intensity. The book contains elements of social criticism, with a strong sense of Christian morality at its core and is considered by many to be ahead of its time because of Jane's individualistic character and how the novel approaches the topics of class, sexuality, religion and feminism.";107;11;12.34;GBP;10
|
||||
251;The Raven;"“The Raven"" is a narrative poem by American writer Edgar Allan Poe. First published in January 1845, the poem is often noted for its musicality, stylized language, and supernatural atmosphere. It tells of a talking raven's mysterious visit to a distraught lover, tracing the man's slow fall into madness. The lover, often identified as being a student, is lamenting the loss of his love, Lenore. Sitting on a bust of Pallas, the raven seems to further distress the protagonist with its constant repetition of the word ""Nevermore"". The poem makes use of folk, mythological, religious, and classical references.";150;333;13.13;USD;1
|
||||
252;Eleonora;"""Eleonora"" is a short story by Edgar Allan Poe, first published in 1842 in Philadelphia in the literary annual The Gift. It is often regarded as somewhat autobiographical and has a relatively ""happy"" ending.";150;555;14;USD;5
|
||||
271;Catweazle;Catweazle is a British fantasy television series, starring Geoffrey Bayldon in the title role, and created by Richard Carpenter for London Weekend Television. The first series, produced and directed by Quentin Lawrence, was screened in the UK on ITV in 1970. The second series, directed by David Reid and David Lane, was shown in 1971. Each series had thirteen episodes, most but not all written by Carpenter, who also published two books based on the scripts.;170;22;15;EUR;3
|
||||
|
4
packages/bookstore/db/csv/sap.capire.products-Products_texts.csv
Executable file
4
packages/bookstore/db/csv/sap.capire.products-Products_texts.csv
Executable file
@@ -0,0 +1,4 @@
|
||||
ID;locale;title;descr
|
||||
201;de;Sturmhöhe;Sturmhöhe (Originaltitel: Wuthering Heights) ist der einzige Roman der englischen Schriftstellerin Emily Brontë (1818–1848). Der 1847 unter dem Pseudonym Ellis Bell veröffentlichte Roman wurde vom viktorianischen Publikum weitgehend abgelehnt, heute gilt er als ein Klassiker der britischen Romanliteratur des 19. Jahrhunderts.
|
||||
207;de;Jane Eyre;Jane Eyre. Eine Autobiographie (Originaltitel: Jane Eyre. An Autobiography), erstmals erschienen im Jahr 1847 unter dem Pseudonym Currer Bell, ist der erste veröffentlichte Roman der britischen Autorin Charlotte Brontë und ein Klassiker der viktorianischen Romanliteratur des 19. Jahrhunderts. Der Roman erzählt in Form einer Ich-Erzählung die Lebensgeschichte von Jane Eyre (ausgesprochen /ˌdʒeɪn ˈɛə/), die nach einer schweren Kindheit eine Stelle als Gouvernante annimmt und sich in ihren Arbeitgeber verliebt, jedoch immer wieder um ihre Freiheit und Selbstbestimmung kämpfen muss. Als klein, dünn, blass, stets schlicht dunkel gekleidet und mit strengem Mittelscheitel beschrieben, gilt die Heldin des Romans Jane Eyre nicht zuletzt aufgrund der Kino- und Fernsehversionen der melodramatischen Romanvorlage als die bekannteste englische Gouvernante der Literaturgeschichte
|
||||
252;de;Eleonora;“Eleonora” ist eine Erzählung von Edgar Allan Poe. Sie wurde 1841 erstveröffentlicht. In ihr geht es um das Paradox der Treue in der Treulosigkeit.
|
||||
|
7
packages/bookstore/db/csv/sap.common-Currencies.csv
Executable file
7
packages/bookstore/db/csv/sap.common-Currencies.csv
Executable file
@@ -0,0 +1,7 @@
|
||||
code;symbol;name;descr
|
||||
EUR;€;Euro;European Euro
|
||||
USD;$;US Dollar;United States Dollar
|
||||
CAD;$;Canadian Dollar;Canadian Dollar
|
||||
AUD;$;Australian Dollar;Australian Dollar
|
||||
GBP;£;Pound;Great Britian Pound
|
||||
ILS;₪;Shekel;Israeli New Shekel
|
||||
|
13
packages/bookstore/db/csv/sap.common-Currencies_texts.csv
Executable file
13
packages/bookstore/db/csv/sap.common-Currencies_texts.csv
Executable file
@@ -0,0 +1,13 @@
|
||||
code;locale;name;descr
|
||||
EUR;de;Euro;European Euro
|
||||
USD;de;US-Dollar;United States Dollar
|
||||
CAD;de;Kanadischer Dollar;Kanadischer Dollar
|
||||
AUD;de;Australischer Dollar;Australischer Dollar
|
||||
GBP;de;Pfund;Britische Pfund
|
||||
ILS;de;Schekel;Israelische Schekel
|
||||
EUR;fr;euro;de la Zone euro
|
||||
USD;fr;dollar;dollar des États-Unis
|
||||
CAD;fr;dollar canadien;dollar canadien
|
||||
AUD;fr;dollar australien;dollar australien
|
||||
GBP;fr;livre sterling;pound sterling
|
||||
ILS;fr;Shekel;shekel israelien
|
||||
|
35
packages/bookstore/db/schema.cds
Normal file
35
packages/bookstore/db/schema.cds
Normal file
@@ -0,0 +1,35 @@
|
||||
namespace sap.capire.bookstore;
|
||||
|
||||
// We reuse Products, which are Books in our domain
|
||||
using { sap.capire.products.Products as Books } from '@sap/capire-products';
|
||||
using { Currency, managed } from '@sap/cds/common';
|
||||
|
||||
extend Books with {
|
||||
author : Association to Authors;
|
||||
}
|
||||
|
||||
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 : managed {
|
||||
key ID : Integer;
|
||||
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 {
|
||||
key ID : Integer;
|
||||
parent : Association to Orders;
|
||||
book : Association to Books;
|
||||
amount : Integer;
|
||||
netAmount : Decimal(9,2);
|
||||
}
|
||||
@@ -8,8 +8,8 @@
|
||||
"node": "^8.9"
|
||||
},
|
||||
"dependencies": {
|
||||
"@sap/cds": "^3.16.3",
|
||||
"@sap/cds-rest": "latest",
|
||||
"@sap/capire-products": "^1.0.0",
|
||||
"@sap/cds": "^3.17.4",
|
||||
"express": "^4.17.1"
|
||||
},
|
||||
"scripts": {
|
||||
|
||||
17
packages/bookstore/srv/services.cds
Normal file
17
packages/bookstore/srv/services.cds
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace sap.capire.bookstore;
|
||||
|
||||
using { sap.capire.products.Products, sap.capire.bookstore as my } from '../db/schema';
|
||||
|
||||
service CatalogService {
|
||||
@readonly entity Books as projection on Products {
|
||||
*, category.name as category, author.name
|
||||
} excluding { createdBy, modifiedBy };
|
||||
|
||||
@insertonly entity Orders as projection on my.Orders;
|
||||
}
|
||||
|
||||
// Reuse services from @sap/capire-products...
|
||||
using { sap.capire.products.AdminService } from '@sap/capire-products';
|
||||
extend service AdminService with {
|
||||
entity Authors as projection on my.Authors;
|
||||
}
|
||||
18
packages/bookstore/srv/services.js
Normal file
18
packages/bookstore/srv/services.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const cds = require('@sap/cds')
|
||||
module.exports = async (srv) => {
|
||||
|
||||
const { Books } = srv.entities
|
||||
// Check all amounts against stock before activating
|
||||
srv.before(['CREATE', 'UPDATE'], 'Orders', (req) => {
|
||||
const tx = cds.transaction(req), order = req.data
|
||||
return Promise.all(order.Items.map(each => tx.run(
|
||||
UPDATE(Books).where({ ID: each.book_ID })
|
||||
.and(`stock >=`, each.amount)
|
||||
.set(`stock -=`, each.amount)
|
||||
).then(affectedRows => {
|
||||
if (!affectedRows) {
|
||||
req.error(409, `${each.amount} exceeds stock for book #${each.book_ID}`)
|
||||
}
|
||||
})))
|
||||
})
|
||||
}
|
||||
@@ -3,6 +3,7 @@ namespace sap.capire.products;
|
||||
using { Currency, cuid, managed, sap.common.CodeList } from '@sap/cds/common';
|
||||
|
||||
entity Products : cuid, managed {
|
||||
key ID : String(36);
|
||||
title : localized String(111);
|
||||
descr : localized String(1111);
|
||||
stock : Integer;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"repository": "https://github.wdf.sap.corp/caps/products-service.git",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@sap/cds": "^3.16.3",
|
||||
"@sap/cds": "^3.17.4",
|
||||
"express": "^4.17.1"
|
||||
},
|
||||
"files": [
|
||||
|
||||
6
packages/reviews-service/.cdsrc.json
Normal file
6
packages/reviews-service/.cdsrc.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"build": {
|
||||
"target": ".",
|
||||
"tasks": []
|
||||
}
|
||||
}
|
||||
15
packages/reviews-service/.gitignore
vendored
Normal file
15
packages/reviews-service/.gitignore
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
.che/
|
||||
.gen/
|
||||
gen/
|
||||
mta_archives/
|
||||
node_modules/
|
||||
target/
|
||||
|
||||
.cds_gen.log
|
||||
connection.properties
|
||||
*.db
|
||||
.DS_Store
|
||||
*.orig
|
||||
_out
|
||||
default-*.json
|
||||
package-lock.json
|
||||
16
packages/reviews-service/.vscode/launch.json
vendored
Normal file
16
packages/reviews-service/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "cds run",
|
||||
"request": "launch",
|
||||
"type": "node", "runtimeExecutable": "npx","runtimeArgs": [ "-n" ],
|
||||
"args": [ "--","cds","run","--in-memory?" ], // the leading "--" arg ensures it works with as well as without debugging
|
||||
"console": "integratedTerminal",
|
||||
"skipFiles": [ "<node_internals>/**" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
30
packages/reviews-service/db/schema.cds
Normal file
30
packages/reviews-service/db/schema.cds
Normal file
@@ -0,0 +1,30 @@
|
||||
namespace sap.capire.reviews;
|
||||
using { User } from '@sap/cds/common';
|
||||
|
||||
// Reviewed subjects can be any entity that is uniquely identified
|
||||
type ReviewedSubject : String(111);
|
||||
|
||||
entity Reviews {
|
||||
key ID : String(36);
|
||||
subject : ReviewedSubject;
|
||||
reviewer : User;
|
||||
rating : Rating;
|
||||
title : String(111);
|
||||
text : String(1111);
|
||||
date : DateTime;
|
||||
likes : Composition of many Likes on likes.review = $self;
|
||||
liked : Integer default 0; // counter for likes as helpful review (count of all _likes belonging to this review)
|
||||
}
|
||||
|
||||
type Rating : Integer enum {
|
||||
Best = 5;
|
||||
Good = 4;
|
||||
Avg = 3;
|
||||
Poor = 2;
|
||||
Worst = 1;
|
||||
}
|
||||
|
||||
entity Likes {
|
||||
key review : Association to Reviews;
|
||||
key user : User;
|
||||
}
|
||||
1
packages/reviews-service/index.cds
Normal file
1
packages/reviews-service/index.cds
Normal file
@@ -0,0 +1 @@
|
||||
using from './srv/reviews-service';
|
||||
19
packages/reviews-service/package.json
Normal file
19
packages/reviews-service/package.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "reviews-service",
|
||||
"version": "1.0.0",
|
||||
"description": "Generated by cds init",
|
||||
"repository": "<Add your repository here>",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@sap/cds": "^3.17.4",
|
||||
"express": "^4.17.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": "^8.9"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "cds build/all --clean",
|
||||
"deploy": "cds deploy",
|
||||
"start": "cds run"
|
||||
}
|
||||
}
|
||||
26
packages/reviews-service/srv/reviews-service.cds
Normal file
26
packages/reviews-service/srv/reviews-service.cds
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace sap.capire.reviews;
|
||||
using { sap.capire.reviews as my } from '../db/schema';
|
||||
|
||||
service ReviewsService {
|
||||
|
||||
event reviewed : { subject:String; rating: Decimal(2,1) };
|
||||
|
||||
// API
|
||||
entity Reviews as projection on my.Reviews excluding { likes }
|
||||
action like (review:Reviews.ID);
|
||||
action unlike (review:Reviews.ID);
|
||||
|
||||
// Input validation
|
||||
annotate Reviews with {
|
||||
subject @mandatory;
|
||||
title @mandatory;
|
||||
rating @mandatory @assert.enum;
|
||||
}
|
||||
|
||||
// Auto-fill reviewers and review dates
|
||||
annotate Reviews with {
|
||||
reviewer @cds.on.insert:$user;
|
||||
date @cds.on.insert:$now;
|
||||
date @cds.on.update:$now;
|
||||
}
|
||||
}
|
||||
40
packages/reviews-service/srv/reviews-service.js
Normal file
40
packages/reviews-service/srv/reviews-service.js
Normal file
@@ -0,0 +1,40 @@
|
||||
const cds = require('@sap/cds')
|
||||
module.exports = cds.service.impl((srv) => {
|
||||
|
||||
// Get the CSN definition for Reviews from the db schema for sub-sequent queries
|
||||
// ( Note: we explicitly specify the namespace to support embedded reuse )
|
||||
const { Reviews, Likes } = cds.entities('sap.capire.reviews')
|
||||
|
||||
// Increment counter for reviews considered helpful
|
||||
srv.on ('like', (req) => {
|
||||
if (!req.user) return req.reject(400, 'You must be identified to like a review')
|
||||
const {review} = req.data, {user} = req
|
||||
const tx = cds.transaction(req)
|
||||
return tx.run ([
|
||||
INSERT.into (Likes) .entries ({review_ID: review, user: user.id}),
|
||||
UPDATE (Reviews) .set({liked: {'+=': 1}}) .where({ID:review})
|
||||
]).catch(() => req.reject(400, 'You already liked that review'))
|
||||
})
|
||||
|
||||
// Delete a former like by the same user
|
||||
srv.on('unlike', async (req) => {
|
||||
if (!req.user) return req.reject(400, 'You must be identified to remove a former like of yours')
|
||||
const { review } = req.data, { user } = req
|
||||
const tx = cds.transaction(req)
|
||||
const affectedRows = await tx.run(DELETE.from(Likes).where({ review_ID: review, user: user.id }))
|
||||
if (affectedRows === 1) return tx.run(UPDATE(Reviews).set({ liked: { '-=': 1 } }).where({ ID: review }))
|
||||
})
|
||||
|
||||
// Emit an event to inform subscribers about new avg ratings for reviewed subjects
|
||||
// ( Note: req.on.succeeded ensures we only do that if there's no error )
|
||||
srv.after(['CREATE', 'UPDATE', 'DELETE'], 'Reviews', async (_, req) => {
|
||||
const { subject } = req.data
|
||||
const { rating } = await cds.transaction(req).run(
|
||||
SELECT.one(['avg(rating) as rating']).from(Reviews).where({ subject })
|
||||
)
|
||||
req.on('succeeded', () => {
|
||||
srv.emit('reviewed', { subject, rating })
|
||||
console.log(`Reviewed event was emitted for book "${subject}" with rating ${rating}.`)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -2,7 +2,7 @@
|
||||
"name": "dev-env",
|
||||
"author": "daniel.hutzel@sap.com",
|
||||
"dependencies": {
|
||||
"@sap/cds": "^3.16.3"
|
||||
"@sap/cds": "^3.17.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"sqlite3": "^4.0.7"
|
||||
|
||||
Reference in New Issue
Block a user