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 - Full-Stack - Back-end Development – API Exposure (Week 2)

dinah
Product and Topic Expert
Product and Topic Expert
18,317

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. 

Task Description 

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: 

Api endpoint process flow(2).png

Task Outline 

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.  

 

Steps 

  1. In the srv directory, create a file named cat-service.js. Copy the following lines of code and paste them in the file: 

 

 

 

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. 

 

Resources:  

Bound vs Unbound 

Read more on bound and unbound actions (11.5.4.1 Invoking an Action) 

Modeling in CDS 

Querying in JavaScript

 

Task Checklist – post screenshot in the reply section 

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

dinah_1-1694581027064.png

 

 

 

53 REPLIES 53

Trulov
Participant

harsh_itaverma
Participant

MioYasutake
Active Contributor

stickman_0x00
Participant
15,381

stickman_0x00_0-1694637745907.png

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.

15,055

If you want to return an error instead of the message:

 

        if (!questionsCount || questionsCount < 1){
            throw new Error(`You asked for zero questions. Nothing to do.`)
        }

15,052

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:

thomas_jung_2-1694699276545.png

Whereas throwing the message as an error will result in an error dialog on the frontend. 

thomas_jung_3-1694699324631.png

 

0 Kudos
15,012

Really cool! 🤘 Thanks

0 Kudos
14,726

well I am  using req.reject() to return error. Will that also have the similar impact on UI ??

14,599

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)

choujiacheng
Explorer

jens_borau
Explorer

SandipAgarwalla
Active Contributor

thomas_jung
Developer Advocate
Developer Advocate

Erdaleser
Explorer

priyankaG
Participant

ruben_alvarez
Explorer

Nicolas
Active Contributor

agnissh
Product and Topic Expert
Product and Topic Expert
14,461

Data Setup : 5 Q's available for assignment

agnissh_0-1694850328390.png

Linking - 3 Q's

agnissh_1-1694850373212.png

Trying to Link 3 more Q's

agnissh_2-1694851048534.png

Again, trying to add 3 Q's

agnissh_4-1694851092066.png

 

 

 

 

Alpesa1990
Participant

Ruthiel
Product and Topic Expert
Product and Topic Expert

Ashok459
Participant

bztoy
Participant

mrpz
Explorer

StanislawKubiak
Discoverer

axitycesarfelce
Participant

tobiasz_h
Active Participant

nex
Explorer

MeriemSouissi
Participant

andrew_mak
Explorer
13,662

Hi Dinah, my submission for week 2.
Thanks!

Week-2-SAP_CAP_Challenge-case-1.png

 

Week-2-SAP_CAP_Challenge-case-2.png

 

Week-2-SAP_CAP_Challenge-case-3.png

 

PavanKandalam
Newcomer

PriyankaChak
Active Contributor

Laco
Explorer
13,510

1. negative Test: Test not found

llapsansky_0-1695152775184.png

2. negative Test: invalid input

llapsansky_1-1695152936640.png

3. negative Test: no request covered, no free Questions left

llapsansky_4-1695153508299.png

4. positive Test: all requests covered

llapsansky_2-1695153137677.png

5. positive Test: requests partially covered

llapsansky_3-1695153282772.png

 

JohnPaulLiberal
Explorer

Yauheni_Kalodka
Discoverer

kiryl-haupt
Explorer

arief_nkn
Explorer

v-tryputsina
Product and Topic Expert
Product and Topic Expert

spassaro
Participant

JérémyChabert
Participant