2023 Sep 13 7:45 AM - edited 2023 Sep 14 2:55 PM
Welcome to the second week of this month’s SAP Developer Challenge. If you have not yet read the initial blog post, please do so and complete the first week’s task as we will build upon it.
With a robust back-end architecture and initial data records in place, we proceed to automatically update our models with data. In week one, we modeled our database and manually added records to our entities using csv files.
We now want to implement an API endpoint that will allow a user to provide the number of questions they wish to associate with a test, from the browser. We will then process that by selecting the given number of questions (yet to be associated with a test) from our currently existing questions model and associate/link them to a test automatically.
Note: A question cannot be used in more than one test.
Below is a process flow diagram of the API endpoint to be implemented:
Our goal is to implement an API layer that will later connect our back-end to the front-end. After implementation, we need to expose the API to allow front-end components seamlessly access it.
What is an action?
An action can be defined as an operation to be performed on one or several entities within a service, which allows for definition of custom behavior or functionality based on the business rules. There are two main types of actions in OData: Bound and Unbound.
Difference between bound and unbound actions
Bound actions are associated with a specific entity type and can only be invoked on instances of that entity type. Unbound actions on the other hand are not tied to any specific entity type and can be invoked independently on any particular entity instance. Unbound actions are mostly used to perform operations that do not require a specific entity context.
We are therefore interested in a bound action as we would like to perform operations related to the Tests entity instance.
const cds = require('@sap/cds')
module.exports = class DevChallengeService extends cds.ApplicationService {
init() {
return super.init()
}
// TODO: Implement the bound action: assignQuestionsToTest
}
2. Implement a bound action named assignQuestionsToTest in the cat-service.js file we just created. This action should contain the logic explained and illustrated in the API endpoint Process Flow diagram above.
Note: the questionsCount value is passed as a parameter to the assignQuestionsToTest action.
3. In the action implementation, return a string with the message you want conveyed to the user based on the result of the action execution. Example: “2 questions successfully added to the test”.
4. Define the bound action - assignQuestionsToTest in srv/cat-service.cds file.
Read more on bound and unbound actions (11.5.4.1 Invoking an Action)
Create a file named test.http in the root directory of our project. Copy and paste the following code that will enable you to make an API call. Feel free to use other tools to test the endpoint.
Replace YOUR_TEST_ID in the link with one of the test IDs you manually populated in week 1.
POST http://localhost:4004/dev-challenge/Tests(ID=YOUR_TEST_ID,IsActiveEntity=true)/DevChallengeService.assignQuestionsToTest
Content-Type: application/json
{"questionsCount":3}
Click on Send Request to make a call to the implemented endpoint. The response should return a HTTP 200 status code with a message (the message can vary depending on the number of questions requested, the state of your data and validation status).
2023 Sep 13 1:17 PM
2023 Sep 13 1:21 PM
2023 Sep 13 9:30 PM
2023 Sep 13 9:44 PM - edited 2023 Sep 13 9:44 PM
This blog post helped a lot on how to do select and update, https://answers.sap.com/questions/13165150/select-inside-action.html.
I think you guys should have provide more resources honestly, for someone that never did something like this, I still don't know how to return an error if I wanted instead of a string like in that blog post.
2023 Sep 14 2:45 PM
2023 Sep 14 2:48 PM - edited 2023 Sep 14 2:50 PM
You ask a valuable question about throwing the error. Just this little change in handler code (throwing instead of returning the message) will later impact the UI. Returning a message will result in an informational dialog:
Whereas throwing the message as an error will result in an error dialog on the frontend.
2023 Sep 14 3:31 PM
Really cool! 🤘 Thanks
2023 Sep 15 8:35 AM
well I am using req.reject() to return error. Will that also have the similar impact on UI ??
2023 Sep 15 3:36 PM
Yes that will result in the same. req.reject (or req.error) Events and Requests | CAPire (cloud.sap) - will result in the error dialog in the Fiori UI as well. When you throw any error from within a handler the CAP framework will catch that and build the request for you the same as directly calling req.reject or req.error.
If you inhert from cds.Service or cds.ApplicationService you can directly hook your handlers without having the the whole this.on... construct. Then you don't use the req... calls but just throw or return the results.
Events and Requests | CAPire (cloud.sap)
2023 Sep 14 3:34 AM
2023 Sep 14 7:54 AM
2023 Sep 14 9:44 AM
2023 Sep 14 2:43 PM
2023 Sep 14 3:11 PM
2023 Sep 15 8:29 AM
2023 Sep 15 8:32 AM
2023 Sep 15 10:36 AM
2023 Sep 16 8:59 AM
Data Setup : 5 Q's available for assignment
Linking - 3 Q's
Trying to Link 3 more Q's
Again, trying to add 3 Q's
2023 Sep 16 9:06 PM - last edited on 2023 Sep 17 4:09 PM by Former Member
2023 Sep 17 12:01 AM
2023 Sep 17 1:41 PM - last edited on 2023 Sep 17 1:42 PM by Former Member
2023 Sep 17 3:43 PM
2023 Sep 17 4:36 PM
2023 Sep 17 5:10 PM
2023 Sep 17 8:57 PM
2023 Sep 18 2:14 PM
2023 Sep 18 2:52 PM
2023 Sep 18 7:41 PM - edited 2023 Sep 18 7:42 PM
2023 Sep 19 11:00 AM
Hi Dinah, my submission for week 2.
Thanks!
2023 Sep 19 3:21 PM
2023 Sep 19 4:12 PM
2023 Sep 19 9:00 PM
1. negative Test: Test not found
2. negative Test: invalid input
3. negative Test: no request covered, no free Questions left
4. positive Test: all requests covered
5. positive Test: requests partially covered
2023 Sep 20 2:27 AM
2023 Sep 20 9:56 AM
2023 Sep 20 10:43 AM - edited 2023 Sep 20 10:48 AM
2023 Sep 20 5:49 PM
2023 Sep 20 10:17 PM
2023 Sep 21 6:05 AM
2023 Sep 21 10:50 AM - edited 2023 Sep 21 10:55 AM