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
17,719

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

thomas_jung
Developer Advocate
Developer Advocate
9,781

Here are my results:
2023-07-19_11-25-47.png

I decided to use a CodeList to make the result description translatable.  So, in the HTTP Post response you only see the code.  However, any read with an $expand on that result will also show the description.

2023-07-19_11-26-28.png

9,440

Well done

Interestingly, these terms never get translated and are pretty universal - good to know how it would be done though!

stickman_0x00
Participant
9,784

stickman_0x00_0-1689781190145.png

Done 🙂

0 Kudos
9,444

Well done

dinah
Product and Topic Expert
Product and Topic Expert
9,680

My results:

dinah_0-1689797376382.png

0 Kudos
9,449

Well done Dinah

9,677

My submission:

romil_agrawal_0-1689798124223.png

 

0 Kudos
9,450

Well done

sabarna17
Contributor
9,659

I have used POSTMAN here. I will deploy this in BAS and test it with .http file later->
Here is my submission for Week 3. Please check ->

1. Create Round
1.png

 

2. Create Hole Entry ->
2.png

0 Kudos
9,450

Well done

choujiacheng
Explorer
9,604

choujiacheng_0-1689817413964.png

Hi, this is my output for this week's exercise.

0 Kudos
9,450

Well done

Phil-L
Explorer
0 Kudos
9,535

Here are my results for this weeks exercise.

P41l_0-1689827337627.png

 

0 Kudos
9,451

Well done

ajos
Explorer
9,528

I am receiving this error when posting the json:

, "message": "Deserialization Error: Unexpected token ​ in JSON at position 33." 

 

Here's link to gitrepo- https://github.com/abapalways/cap-developer-challenge1/blob/main/srv/cat-service.js

 

Can anyone help?

 

 

9,521

Check your http file.

I also faced this similar issue, due to additional space after the curly {} brackets

 

9,479

Thanks. That helped.

9,452

Well done Love the community spirit!

ajos
Explorer
9,486

My results:

 

ajos_1-1689833962436.png

 

0 Kudos
9,456

0 Kudos
8,589

Hi @nicoschoenteich ,

Please find below my submission for Week 3.

stephanie_senit_0-1690113838798.png

 

Thanks & regards

Stephanie

1joandre
Participant
9,465

Hello team

Here is my result

1joandre_0-1689838680857.png

Regards

Ignacio

 

0 Kudos
9,330

Well done

UtkanGiray
Explorer
9,395

Hello

My results are :

UtkanGiray_0-1689849672824.png

 

0 Kudos
9,332

Well done

ZhongpinWang
Advisor
Advisor
9,325

My submission for Java:

Screenshot 2023-07-20 163542.png

 

0 Kudos
8,489

Well done Very cool to have done it with Java!

D_Pietroniro
Product and Topic Expert
Product and Topic Expert
9,321

Here is my result.

D_Pietroniro_0-1689865046308.png

 

0 Kudos
8,497

Well done

alessandraarm
Explorer
9,204

Hello!

Here is my submission for week 3:

alessandraarm_0-1689881549220.png

Thanks! 😊

0 Kudos
8,498

Well done

aslan1906
Participant
9,057

keep learning and practicing

aslan1906_0-1689924159123.png

 

0 Kudos
8,497

Well done

govardhansahil
Explorer
9,052

Hi @nicoschoenteich 

Here is my submission for week 3. Thank you for the challenge!

govardhansahil_0-1689927278395.png

 

0 Kudos
8,512

Well done

9,054

Hi, here's my submission for the week, I hope that it's OK. thanks for organising this!

benw_0-1689927879580.png

 

0 Kudos
8,516

Well done

nmkarthikeya
Active Participant
9,036

My Submissionnmkarthikeya_0-1689932112897.png

 

0 Kudos
8,516

Well done