Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

SAP Developer Challenge - SAP Cloud Application Programming Model (Week 3)

nicoschoenteich
Developer Advocate
Developer Advocate

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.


If you haven't read the announcement blog post for this challenge, please head over and do so. This week's challenge builds on top of previous two challenges, which you should have completed before starting with this one.

The Challenge

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..

2. Read about custom logic and event handlers  in the CAP documentation. Also read about implementing services in CAP Node.js.
3. Create a new file cat-service.js in the srv/ directory, or give this new file another name and use the @impl annotation.
4. Register an event handler on the CREATE operation on the Holes entity.
5. In the event handler, read the req.data.score and req.data.par properties. Then add some custom logic that calculates and fills the new req.data.result property so it represents the following mappings:
 
Score in relation to parString
score - par = -3albatross
score - par = -2eagle
score - par = -1birdie
score - par = 0par
score - par = +1bogey
score - par = +2double bogey
score - par = +3triple bogey
score = 1hole in one
 
6. Don't forget to return next()  at the end of your event handler to resume the standard event processing of CAP.
7.  Test your custom event handler by creating a new test.http file and pasting the following code:

 

POST http://localhost:4004/browse/Holes
Content-Type: application/json

{
    "score": 3,
    "par": 4
}​

 

 

8. Your editor (SAP Business Application Studio or VS Code) should recognize the .http file ending and display a little "Send Request" button (if not, you might have to install this extension).
9. Send the test request and inspect the result - hopefully it contains "birdie" as a result.
10. Feel free to test and play around with other requests and values.
11. Share a screen shot of your editor so that we can see the test request being sent and the results field being populated. It should look something like this:
 
2023-07-18_15-14-21.png
 

Resources

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!

112 REPLIES 112

vmecko
Discoverer

Hello, here is my result:

vmecko_0-1690281397668.png

 

0 Kudos

Well done

alina-balysh
Discoverer

Hello! This is my submission  for this week:Screenshot 2023-07-25 140940.png

0 Kudos

Well done

JohnPaulLiberal
Explorer

Hi all!

My submission for this week's challenge:

JohnPaulLiberal_0-1690300763211.png

Thanks for this challenge, @nicoschoenteich.

0 Kudos

Well done

MadhavKumar
Explorer

Hi @nicoschoenteich,

Sharing Week3 Submission snapshot-

MadhavKumar_0-1690355559164.png

 

Thank You,

Madhav Kumar

0 Kudos

Well done

jdm
Explorer

Week 3 submission:

jdm_0-1690355693996.png

 

 

nicoschoenteich
Developer Advocate
Developer Advocate
0 Kudos

Well done

Geeth
Product and Topic Expert
Product and Topic Expert

Hi Nicolai, 

I hope this is fine.

Geeth_0-1690368084745.png

 

0 Kudos

Well done

Cmdd
Participant

Third week submission

Cmdd_0-1690379020758.png

 

0 Kudos

Well done

Ruthiel
Product and Topic Expert
Product and Topic Expert

Hello!
Here my submission for week 3:

Ruthiel_0-1690409699249.png

 

0 Kudos

Well done

Here is my week 3 submission.

santoshmurmu_0-1690439563855.png

 

0 Kudos

Well done

johna69
Product and Topic Expert
Product and Topic Expert

 Here are my results:

Screenshot 2023-07-28 at 12.13.29 AM.png

0 Kudos

Well done

MeriemSouissi
Participant

My submission week 3

MeriemSouissi_0-1690584360000.png

 

0 Kudos

Well done

andrew_chiam
Explorer

Week 3 submission for your verification 😃

Week 3 Challenge.PNG

0 Kudos

Well done

wawowrt
Explorer

This is my submission for Week 3. 😊CAP_Week3.png

 

0 Kudos

Well done

cd_winc
Explorer

Hi Nico, 

Here is my submission:

M3.PNG

but why the request doesn't work for 

it only works for 
POST http://localhost:4004/catalog/Holes

0 Kudos

Well done

The path depends on what you specified for your service in the service definition file. The template had /browse as a path, but in case you removed that line the service CatalogService gets resolved to /catalog automatically (removing "service" from the name).

harsh_itaverma
Participant

Got "Birdie" 🕊

verma_0-1690877649699.png

 

Well done

andrew_mak
Explorer

Hi, my week 3 submission.

browse Holesbrowse Holes2023-08-01 Week-3.png

Thanks.

0 Kudos

Well done

satya-dev
Participant
0 Kudos

My submission.

satyadev_0-1691227025880.png