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

cguttikonda24
Participant
0 Kudos

 

@nicoschoenteich  Week 3 challenge submission.

cguttikonda24_0-1689932920071.png

Added mocked authentication as well to make sure of the created by data in the Rounds entity

cguttikonda24_0-1689934516225.png

 

 

 

With added authentication 

cguttikonda24_1-1689934415353.png

 

0 Kudos

Well done

Trulov
Participant

week 3 submission 🙂

cap-dev-challenge-3.png

0 Kudos

Well done

v-tryputsina
Product and Topic Expert
Product and Topic Expert

Thanks 😁

vtryputsina_0-1689942231176.png

 

0 Kudos

Well done

geek
Participant

geek_0-1689955540806.png

The biggest problem for me was copying that test.http kept bringing in bogus characters which resulted in deserialization errors... 

0 Kudos

Well done nonetheless There is something weird about the way the code block is being rendered in this page.

former_member150823
Product and Topic Expert
Product and Topic Expert

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

radubigu_0-1689976869680.png

 

Which exit type are you using?  Are you overriding the standard handler? I used the Before handler and didn't have to call next()

0 Kudos

I believe next() has to be returned when using "on" handler.

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.

0 Kudos

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

bztoy
Participant

Hi Nico,

Here is my submission for Week#3.

A screen shot of a post call.

cap-code-challength-week-03-created.png

A new record created in Holes entity.

cap-code-challength-week-03-now-hole-added.png

if the provided round ID is correct and exists in Rounds entity, this new created hole data will be associated to the given round.

cap-code-challength-week-03-query-with-condition-and-associated-data.png

Thanks,

Wises K.

0 Kudos

Well done

antothomasraja
Explorer

Hi @nicoschoenteich

Below is my submission for Week 3,

antothomasraja_0-1690113964230.png

 Best Regards,

Anto

0 Kudos

Well done

MarcelloUrbani
Active Contributor

Done:

MarcelloUrbani_0-1690128851591.png

 

0 Kudos

Well done

steph_senita
Explorer

Hi @nicoschoenteich ,

Please find my submission for the Week 3 assignment.

steph_senita_0-1690185784081.png

 

Thanks & regards

Stephanie

0 Kudos

Well done

marcinb
Explorer

hello,
this is my submission for week 3:

marcinb_0-1690192778224.png

 

Well done

VenugopalA
Explorer

Hi @nicoschoenteich 

respective week task, good stuff. 

VenugopalA_0-1690197274459.png

 

thank you.

venu

 

 

0 Kudos

Well done

Afonso_Pimentel
Explorer

This is my submission:

Afonso_Pimentel_0-1690232948815.png

0 Kudos

Well done

Aliaksandr_Ch
Product and Topic Expert
Product and Topic Expert

0 Kudos

Well done

dhegde
Participant

Hi @nicoschoenteich 

Here is my submission for Week3

dhegde_0-1690259146348.png

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.  

dhegde_1-1690259270091.png

Thank you

Dhananjay

 

Well done, despite the challenges you had

MioYasutake
Active Contributor

My week 3 submission!

miyasuta_0-1690268488261.png

 

0 Kudos

Well done

Aashish28
Contributor

Hello @nicoschoenteich 

Here is my submission for #Week 3:

Aashish28_0-1690277885019.png

0 Kudos

Well done

Ashok459
Participant

Here are my results:

Ashok_Easa_0-1690278454835.png

 

0 Kudos

Well done

Here are my results.

HarshalN_0-1690278757936.pngHarshalN_1-1690278802758.png

 

0 Kudos

Well done