Merge branch 'main' into ql-changes-typo-fix

This commit is contained in:
Dr. David A. Kunz
2021-10-26 11:56:15 +02:00
committed by GitHub
20 changed files with 273 additions and 8075 deletions

View File

@@ -5,9 +5,9 @@ name: CI
on: on:
push: push:
branches: [ master ] branches: [ main ]
pull_request: pull_request:
branches: [ master ] branches: [ main ]
jobs: jobs:
build: build:

View File

@@ -105,5 +105,5 @@
"description": "### Summary\n\nThat's it! You have seen: \n- How to integrate database-specific functions in a CDS model.\n- How to switch between the two implementations for SQLite and SAP HANA." "description": "### Summary\n\nThat's it! You have seen: \n- How to integrate database-specific functions in a CDS model.\n- How to switch between the two implementations for SQLite and SAP HANA."
} }
], ],
"ref": "master" "ref": "main"
} }

View File

@@ -17,7 +17,7 @@ Find here a collection of samples for the [SAP Cloud Application Programming Mod
### Download ### Download
If you've [Git](https://git-scm.com/downloads) installed, clone this repo as shown below, otherwise [download as ZIP file](archive/master.zip). If you've [Git](https://git-scm.com/downloads) installed, clone this repo as shown below, otherwise [download as ZIP file](archive/main.zip).
```sh ```sh
git clone https://github.com/sap-samples/cloud-cap-samples samples git clone https://github.com/sap-samples/cloud-cap-samples samples

View File

@@ -34,7 +34,7 @@
<td>{{ book.author }}</td> <td>{{ book.author }}</td>
<td>{{ book.genre.name }}</td> <td>{{ book.genre.name }}</td>
<td class="rating-stars"> <td class="rating-stars">
{{ ('★'.repeat(Math.round(book.rating))+'☆☆☆☆☆').slice(0,5) }} {{ ('★'.repeat(Math.round(book.rating))+'☆☆☆☆☆').slice(0,5) }} ({{ book.numberOfReviews }})
</td> </td>
<td>{{ book.currency && book.currency.symbol }} {{ book.price }}</td> <td>{{ book.currency && book.currency.symbol }} {{ book.price }}</td>
</tr> </tr>

View File

@@ -1 +1,2 @@
exports.CatalogService = require('./srv/cat-service') const { CatalogService } = require('./srv/cat-service')
module.exports = { CatalogService }

View File

@@ -6,11 +6,11 @@ using CatalogService from '@capire/bookshop';
// //
annotate CatalogService.Books with @( annotate CatalogService.Books with @(
UI: { UI: {
HeaderInfo: { HeaderInfo: {
TypeName: 'Book', TypeName: 'Book',
TypeNamePlural: 'Books', TypeNamePlural: 'Books',
Description: {Value: author} Description: {Value: author}
}, },
HeaderFacets: [ HeaderFacets: [
{$Type: 'UI.ReferenceFacet', Label: '{i18n>Description}', Target: '@UI.FieldGroup#Descr'}, {$Type: 'UI.ReferenceFacet', Label: '{i18n>Description}', Target: '@UI.FieldGroup#Descr'},
], ],
@@ -38,7 +38,7 @@ annotate CatalogService.Books with @(
// //
annotate CatalogService.Books with @( annotate CatalogService.Books with @(
UI: { UI: {
SelectionFields: [ ID, price, currency_code ], SelectionFields: [ ID, price, currency_code ],
LineItem: [ LineItem: [
{Value: title}, {Value: title},
{Value: author, Label:'{i18n>Author}'}, {Value: author, Label:'{i18n>Author}'},

View File

@@ -38,6 +38,11 @@
"[production]": { "[production]": {
"model": "db/hana" "model": "db/hana"
} }
},
"messaging": {
"[development]": { "kind": "file-based-messaging" },
"[hybrid]": { "kind": "enterprise-messaging-shared" },
"kind": "enterprise-messaging"
} }
} }
} }

View File

@@ -10,9 +10,11 @@ cds.once('bootstrap',(app)=>{
cds.once('served', require('./srv/mashup')) cds.once('served', require('./srv/mashup'))
// Swagger UI - see https://cap.cloud.sap/docs/advanced/openapi // Swagger UI - see https://cap.cloud.sap/docs/advanced/openapi
if (process.env.NODE_ENV !== 'production') { try {
const cds_swagger = require ('cds-swagger-ui-express') const cds_swagger = require ('cds-swagger-ui-express')
cds.once ('bootstrap', app => app.use (cds_swagger()) ) cds.once ('bootstrap', app => app.use (cds_swagger()) )
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') throw err
} }

View File

@@ -12,6 +12,7 @@ using { sap.capire.bookshop.Books } from '@capire/bookshop';
using { ReviewsService.Reviews } from '@capire/reviews'; using { ReviewsService.Reviews } from '@capire/reviews';
extend Books with { extend Books with {
reviews : Composition of many Reviews on reviews.subject = $self.ID; reviews : Composition of many Reviews on reviews.subject = $self.ID;
numberOfReviews : Integer;
rating : Decimal; rating : Decimal;
} }

View File

@@ -41,8 +41,8 @@ module.exports = async()=>{ // called by server.js
// //
ReviewsService.on ('reviewed', (msg) => { ReviewsService.on ('reviewed', (msg) => {
console.debug ('> received:', msg.event, msg.data) console.debug ('> received:', msg.event, msg.data)
const { subject, rating } = msg.data const { subject, count, rating } = msg.data
return UPDATE(Books,subject).with({rating}) return UPDATE(Books,subject).with({ numberOfReviews:count, rating })
// ^ Note: the framework will execute this and take care for db.tx // ^ Note: the framework will execute this and take care for db.tx
}) })

8215
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -12,14 +12,13 @@
"@capire/media": "./media", "@capire/media": "./media",
"@capire/orders": "./orders", "@capire/orders": "./orders",
"@capire/reviews": "./reviews", "@capire/reviews": "./reviews",
"@sap/cds": "^5.1.5" "@sap/cds": "^5.5.3"
}, },
"devDependencies": { "devDependencies": {
"cds-swagger-ui-express": "^0.2.0", "chai": "^4.3.4",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1", "chai-as-promised": "^7.1.1",
"chai-subset": "^1.6.0", "chai-subset": "^1.6.0",
"sqlite3": "^5.0.0" "sqlite3": "^5"
}, },
"scripts": { "scripts": {
"cleanup": "rm -rf node_modules && rm -rf */node_modules && rm -rf */*/node_modules", "cleanup": "rm -rf node_modules && rm -rf */node_modules && rm -rf */*/node_modules",
@@ -35,6 +34,7 @@
}, },
"jest": { "jest": {
"testEnvironment": "node", "testEnvironment": "node",
"testTimeout": 20000,
"testMatch": [ "testMatch": [
"**/*.test.js" "**/*.test.js"
] ]

View File

@@ -8,10 +8,11 @@ service ReviewsService {
action unlike (review: type of Reviews:ID); action unlike (review: type of Reviews:ID);
// Async API // Async API
event reviewed : { event reviewed : {
subject: type of Reviews:subject; subject : type of Reviews:subject;
rating: Decimal(2,1) count : Integer;
} rating : Decimal;
}
// Input validation // Input validation
annotate Reviews with { annotate Reviews with {

View File

@@ -12,11 +12,11 @@ module.exports = cds.service.impl (function(){
// Emit an event to inform subscribers about new avg ratings for reviewed subjects // Emit an event to inform subscribers about new avg ratings for reviewed subjects
this.after (['CREATE','UPDATE','DELETE'], 'Reviews', async function(_,req) { this.after (['CREATE','UPDATE','DELETE'], 'Reviews', async function(_,req) {
const {subject} = req.data const {subject} = req.data
const {rating} = await cds.tx(req) .run ( const { count, rating } = await cds.tx(req) .run (
SELECT.one (['round(avg(rating),2) as rating']) .from (Reviews) .where ({subject}) SELECT.one `round(avg(rating),2) as rating, count(*) as count` .from (Reviews) .where ({subject})
) )
global.it || console.log ('< emitting:', 'reviewed', { subject, rating }) global.it || console.log ('< emitting:', 'reviewed', { subject, count, rating })
await this.emit ('reviewed', { subject, rating }) await this.emit ('reviewed', { subject, count, rating })
}) })
// Increment counter for reviews considered helpful // Increment counter for reviews considered helpful

View File

@@ -0,0 +1,3 @@
<head>
<meta http-equiv="refresh" content="0;url=app/bookshop/index.html">
</head>

View File

@@ -0,0 +1,3 @@
<head>
<meta http-equiv="refresh" content="0;url=app/reviews/index.html">
</head>

17
reviews/test/package.json Normal file
View File

@@ -0,0 +1,17 @@
{
"name": "@capire/bookshop-with-reviews",
"version": "1.0.0",
"dependencies": {
"@capire/bookshop": "*",
"@capire/reviews": "*",
"@sap/cds": "^5",
"express": "^4.17.1"
},
"cds": {
"requires": {
"db": {
"kind": "sql"
}
}
}
}

View File

@@ -0,0 +1,13 @@
// Use enhanced implementation for CatalogService
using { CatalogService } from '@capire/bookshop';
annotate CatalogService with @impl:'srv/bookshop.js';
// Extend Books with access to Reviews and average ratings
using { sap.capire.bookshop.Books } from '@capire/bookshop';
using { ReviewsService.Reviews } from '@capire/reviews';
extend Books with {
reviews : Composition of many Reviews on reviews.subject = $self.ID;
rating : Decimal;
numberOfReviews : Integer;
}

View File

@@ -0,0 +1,27 @@
const { CatalogService } = require('@capire/bookshop')
const cds = require ('@sap/cds')
module.exports = class extends CatalogService {async init(){
const { Books } = cds.entities('sap.capire.bookshop')
// Connect to ReviewsService to receive `reviewed` events from it
const ReviewsService = await cds.connect.to ('ReviewsService')
ReviewsService.on ('reviewed', (msg) => {
console.debug ('> received:', msg.event, msg.data)
const { subject, count, rating } = msg.data
return UPDATE(Books,subject).with({ numberOfReviews:count, rating })
})
return super.init()
}}
// -----------------------------------------------------------------------
// Helper for serving static content from npm-installed packages
const {dirname,resolve} = require('path')
const {static} = require('express')
cds.once('listening',()=>{
cds.app.use ('/app/bookshop', static (dirname (require.resolve('@capire/bookshop'))+'/app/vue'))
cds.app.use ('/app/reviews', static (resolve (__dirname, '../../app/vue')))
})

View File

@@ -60,11 +60,11 @@ describe('Messaging', ()=>{
expect(M).equals(N) expect(M).equals(N)
expect(received.length).equals(N) expect(received.length).equals(N)
expect(received.map(m=>m.data)).to.deep.equal([ expect(received.map(m=>m.data)).to.deep.equal([
{ subject: '201', rating: 1 }, { count: 1, subject: '201', rating: 1 },
{ subject: '201', rating: 1.5 }, { count: 2, subject: '201', rating: 1.5 },
{ subject: '201', rating: 2 }, { count: 3, subject: '201', rating: 2 },
{ subject: '201', rating: 2.5 }, { count: 4, subject: '201', rating: 2.5 },
{ subject: '201', rating: 3 }, { count: 5, subject: '201', rating: 3 },
]) ])
}) })
}) })