cancel
Showing results for 
Search instead for 
Did you mean: 

How to accept request param inside a custom CDS service entity

vinay1210
Discoverer
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;
}
 
BooksService.cds
 
service BookService{
 
  entity Book as projection on Books;
 
  entity BookList as 
select 
title,
author,
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
nlaenger
Explorer
0 Kudos

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

https://cap.cloud.sap/docs/guides/localized-data#declaring-localized-data

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

https://cap.cloud.sap/docs/guides/localized-data#user-locale

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.

also

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