Skip to content

Commit e5f6c9f

Browse files
committed
Adjust docs to changed content in @capire/reviews
1 parent f233726 commit e5f6c9f

1 file changed

Lines changed: 24 additions & 27 deletions

File tree

guides/messaging/index.md

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -116,53 +116,51 @@ The following explanations walk us through a books review example from cap/sampl
116116
![This graphic is explained in the accompanying text.](assets/cap-samples.drawio.svg)
117117

118118
::: tip
119-
Follow the instructions in [*cap/samples/readme*](https://github.com/capire) for getting the samples and exercising the following steps.
119+
Follow the instructions in [*cap/samples/readme*](https://github.com/capire/samples) for getting the samples and exercising the following steps.
120120
:::
121121

122122
### Declaring Events in CDS
123123

124-
Package `@capire/reviews` essentially provides a `ReviewsService`, [declared like that](https://github.com/capire/reviews/tree/main/srv/reviews-service.cds):
124+
Package `@capire/reviews` provides a `ReviewsService` API, [declared like that](https://github.com/capire/reviews/tree/main/srv/reviews-api.cds):
125125

126126
```cds
127-
service ReviewsService {
128-
129-
// Sync API
130-
entity Reviews as projection on my.Reviews excluding { likes }
131-
action like (review: Reviews:ID);
132-
action unlike (review: Reviews:ID);
133-
134-
// Async API
135-
event reviewed : {
136-
subject : Reviews:subject;
137-
count : Integer;
138-
rating : Decimal; // new avg rating
139-
}
140-
127+
service ReviewsService @(path:'reviews/api') {
128+
129+
/**
130+
* Summary of average ratings per subject.
131+
*/
132+
@readonly entity AverageRatings as projection on my.Reviews {
133+
key subject,
134+
round(avg(rating),2) as rating : my.Rating,
135+
count(*) as reviews : Integer,
136+
} group by subject;
137+
138+
/**
139+
* Informs about changes in a subject's average rating.
140+
*/
141+
event AverageRatings.Changed : AverageRatings; // [!code focus]
141142
}
142143
```
143144

144145
[Learn more about declaring events in CDS.](../../cds/cdl#events){.learn-more}
145146

146-
As you can read from the definitions, the service's synchronous API allows to create, read, update, and delete user `Reviews` for arbitrary review subjects. In addition, the service's asynchronous API declares the `reviewed` event that shall be emitted whenever a subject's average rating changes.
147+
As we can read from the definition, the service's synchronous API allows to read average ratings per subject; the service's asynchronous API declares the `AverageRatings.Changed` event that shall be emitted whenever a subject's average rating changes.
147148

148149
::: tip
149150
**Services in CAP** combine **synchronous** *and* **asynchronous** APIs. Events are declared on conceptual level focusing on domain, instead of low-level wire protocols.
150151
:::
151152

152153
### Emitting Events
153154

154-
Find the code to emit events in *[@capire/reviews/srv/reviews-service.js](https://github.com/capire/reviews/tree/main/srv/reviews-service.js#L12-L20)*:
155+
Find the code to emit events in *[@capire/reviews/srv/reviews-service.js](https://github.com/capire/reviews/tree/main/srv/reviews-service.js#L32-L37)*:
155156

156157
```js
157-
class ReviewsService extends cds.ApplicationService { async init() {
158-
159-
// Emit a `reviewed` event whenever a subject's avg rating changes
160-
this.after (['CREATE','UPDATE','DELETE'], 'Reviews', (_, req) => {
161-
let { subject } = req.data, count, rating //= ...
162-
return this.emit ('reviewed', { subject, count, rating })
158+
// Inform API event subscribers about new avg ratings for reviewed subjects
159+
const api = await cds.connect.to ('sap.capire.reviews.api.ReviewsService')
160+
this.after (['CREATE','UPDATE','DELETE'], 'Reviews', async function(_,req) {
161+
const { subject, rating, reviews } = await api.get ('AverageRatings', { subject: req.data.subject })
162+
return api.emit ('AverageRatings.Changed', { subject, rating, reviews }) // [!code focus]
163163
})
164-
165-
}}
166164
```
167165
[Learn more about `srv.emit()` in Node.js.](../../node.js/core-services#srv-emit-event){.learn-more}
168166
[Learn more about `srv.emit()` in Java.](../../java/services#an-event-based-api){.learn-more}
@@ -181,7 +179,6 @@ Find the code to receive events in *[@capire/bookstore/srv/mashup.js](https://gi
181179
// Update Books' average ratings when ReviewsService signals updated reviews
182180
ReviewsService.on ('AverageRatings.Changed', (msg) => {
183181
console.debug ('> received:', msg.event, msg.data)
184-
const { subject, count, rating } = msg.dataReviewsService.on ('reviewed', (msg) => {
185182
const { subject, count, rating } = msg.data
186183
// ...
187184
})

0 commit comments

Comments
 (0)