Skip to content

Commit

Permalink
Explain how to handle /$count requests in CAP Java custom ON handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
beckermarc authored Oct 23, 2023
1 parent 426d1de commit 8119c6d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions java/application-services.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ In case an order item is directly created (for example through a containment nav

`READ` event handlers must return the data that was read, either as an `Iterable<Map>` or [Result](https://javadoc.io/doc/com.sap.cds/cds4j-api/latest/com/sap/cds/Result.html) object created via the [ResultBuilder](#result-builder-read). For queries with inline count, a `Result` object _must_ be used as the inline count is obtained from the `Result` interface.

#### /$count Results

`READ` event handlers are also called, for OData `/$count` requests. These requests determine the total amount of entity instances of a specific entity. When handling these requests in a custom `@On` event handler a `Map` with a single key `count` needs to be returned as a result:

```java
@On(entity = MyEntity_.CDS_NAME)
List<Map<String, Object>> readMyEntity(CdsReadEventContext context) {
if (CqnAnalyzer.isCountQuery(context.getCqn())) {
int count = 100; // determine correct count value
return List.of(Collections.singletonMap("count", 100));
}
// handle non /$count requests
}
```

### UPDATE and DELETE Results

`UPDATE` and `DELETE` statements have an optional filter condition (where clause) which determines the entities to be updated/deleted. Handlers _must_ return a `Result` object with the number of entities that match this filter condition and have been updated/deleted. Use the [ResultBuilder](#result-builder) to create the `Result` object.
Expand Down

0 comments on commit 8119c6d

Please sign in to comment.