2023 Jul 19 9:24 AM - edited 2023 Aug 02 2:14 PM
Please note that this challenge is closed. The deadline for receiving a badge upon successful completion has passed. Check out this new challenge for the month of August.
Welcome to the third week of this month's SAP Developer Challenge. This week we are going to learn how to use event handlers with the SAP Cloud Application Programming Model.
After moving to the SAP Business Application Studio and adding some sample data to our application last week, it's now time to explore some more features and capabilities of the SAP Cloud Application Programming Model. One of those is event handling, which will be our focus for this week.
Let's say we would like to enhance our application by automatically populating a new data field for the entity Holes. We would like to know what score a player scored on a hole, but not as a number, rather as one of the following strings: "birdie", "par", "bogey" (and a few more, see table below). These terms are frequently used in golf to describe the score a player scored on a hole in relation to the hole's par (par = expected score from a professional player). Let's say it took the player 4 shots to get the ball into the hole on a par 4 - that is a par. If it took the player only 3 shots on that same hole - that would be a birdie. See the table below for all mappings.
The described requirement is a perfect use case for event handling in CAP. As described in the documentation, calculated fields should be pre-calculated whenever a new entry is written into the database (instead of calculating on every read operation). This means we need a custom event handler for the CREATE operation on the Holes entity.
This is what you have to do to successfully complete this week's challenge:
1. Create a new property result: String; in the schema definition of the Holes entity. This is where we will store "par", "birdie", "bogey" etc..
Score in relation to par | String |
score - par = -3 | albatross |
score - par = -2 | eagle |
score - par = -1 | birdie |
score - par = 0 | par |
score - par = +1 | bogey |
score - par = +2 | double bogey |
score - par = +3 | triple bogey |
score = 1 | hole in one |
POST http://localhost:4004/browse/Holes
Content-Type: application/json
{
"score": 3,
"par": 4
}
We have gathered a few helpful resources for this week's challenge. Feel free to use the comments sections if you have question or need help.
Good luck and happy learning!
2023 Jul 21 10:50 AM - edited 2023 Jul 21 11:16 AM
@nicoschoenteich Week 3 challenge submission.
Added mocked authentication as well to make sure of the created by data in the Rounds entity
2023 Jul 21 11:15 AM
2023 Jul 24 8:55 AM
2023 Jul 21 12:26 PM
2023 Jul 24 8:55 AM
2023 Jul 21 1:24 PM
2023 Jul 24 8:55 AM
2023 Jul 21 5:07 PM
The biggest problem for me was copying that test.http kept bringing in bogus characters which resulted in deserialization errors...
2023 Jul 24 8:54 AM
2023 Jul 21 11:01 PM
Pfew, it was a pain to find a solution for making the event handler code work.
I had troubles like others with the http test file and with point 6, returning the next() function. Don't ask me how I managed to find that I need to put the "next" in the handler function...
2023 Jul 21 11:16 PM
2023 Jul 24 8:53 AM
2023 Jul 24 8:59 AM
Is there example code somewhere for the use of "on", "CREATE" and "next()", with a loop? The documentation is a bit too abstract for me.
2023 Jul 24 9:10 AM
You can read more about next() here: https://cap.cloud.sap/docs/node.js/core-services#interceptor-stack-with-next
It know what you mean with "abstract". Here is an example of another handler. This is using the slightly different class syntax: https://github.com/SAP-samples/cloud-cap-samples/blob/main/bookshop/srv/cat-service.js
2023 Jul 22 12:07 PM
Hi Nico,
Here is my submission for Week#3.
A screen shot of a post call.
A new record created in Holes entity.
if the provided round ID is correct and exists in Rounds entity, this new created hole data will be associated to the given round.
Thanks,
Wises K.
2023 Jul 24 8:51 AM
2023 Jul 23 1:06 PM
2023 Jul 24 8:51 AM
2023 Jul 23 5:14 PM
2023 Jul 24 8:50 AM
2023 Jul 24 9:03 AM
2023 Jul 26 11:14 AM
2023 Jul 24 10:59 AM
2023 Jul 26 11:14 AM
2023 Jul 24 12:14 PM
2023 Jul 26 11:14 AM
2023 Jul 24 10:09 PM
2023 Jul 26 11:14 AM
2023 Jul 24 11:44 PM
2023 Jul 26 11:14 AM
2023 Jul 25 5:28 AM
Here is my submission for Week3
PS: For some reason, REST Client VS Code extension was not working for me, requests were getting rejected (localhost:4004 requests were getting changed to 127.0.0.1:443). No proxy set in my laptop. chaning this setting for extension also did not help. Therefore, I used Postman for testing.
Thank you
Dhananjay
2023 Jul 26 11:13 AM
2023 Jul 25 8:01 AM
2023 Jul 26 11:13 AM
2023 Jul 25 10:39 AM
2023 Jul 26 11:13 AM
2023 Jul 25 10:48 AM
2023 Jul 26 11:13 AM
2023 Jul 25 10:53 AM
2023 Jul 26 11:13 AM