Showing results for 
Search instead for 
Did you mean: 

How to accept request param inside a custom CDS service entity

0 Kudos
entity description{
language : String;
shortDescription : String;
longDescription  : String;
deleted : Boolean default false;
Books   : Association to one Books;
entity Books : cuid ,managed {
     title : String;
     author : String;
     deleted : Boolean default false;
     to_description : Composition of many description
on to_description.books = $self;
service BookService{
  entity Book as projection on Books;
  entity BookList as 
to_description.shortDescription as shortDescription
from Book 
where deleted = false; 
The current implementation of the BooksService.cds returns all descriptions associated with a book when the list service is invoked. However, the requirement is to modify the service to accept a parameter, Accept-Language, which specifies the preferred language for the description. Based on this language parameter, the service should filter and return only the description that matches the specified language.
To achieve this, the service needs to be updated to accept the Accept-Language parameter as a request parameter. Upon receiving the request, the service should extract the language preference from the Accept-Language header and use it to filter the descriptions associated with the books. Only the description that matches the specified language should be included in the response.
Please provide a solution that demonstrates how to modify the BooksService to accept the Accept-Language parameter and filter descriptions based on the specified language preference.



View Entire Topic
0 Kudos

CAP can do a lot for you when you "only need" the titel in the user language:

For more complex examples I think you are looking for this CAP chapter:

Determining $user.locale from Inbound Requests

The user's preferred locale is determined from request parameters, user settings, or the accept-language header of inbound requests as explained in the Localization guide.


Programmatic Access to $user.locale

The resulting normalized locale is available programmatically, in your event handlers.

  • Node.js: req.locale
  • Java: eventContext.getParameterInfo().getLocale()

I hope this is helping you