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

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

dhegde
Participant

After Test 2

dhegde_0-1695309601351.png

After Test 3

dhegde_1-1695309626526.png

After Test 4

dhegde_2-1695309649813.png

 

 

geek
Participant

geek_0-1695658967961.png

Assign too many questions:

geek_1-1695659024532.png

 

MarcelloUrbani
Active Contributor

alessandraarm
Explorer
0 Kudos

Hello!

Here is my submission for week 2:

 

alessandraarm_0-1695947184285.png

Thanks!

nmkarthikeya
Active Participant

flo_conrad
Explorer

LucasPires230
Explorer

ziyangwong238
Discoverer

Aliaksandr_Ch
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi @dinah ,

Below are the screens of the different cases:

  1. Screenshot 2023-10-04 at 17.20.16.png
  2. Screenshot 2023-10-04 at 17.24.55.png
  3. Screenshot 2023-10-04 at 17.27.04.png
  4. Screenshot 2023-10-04 at 17.28.58.png

JJAIMES
Participant
0 Kudos

Hi!, 

These are my results and thanks for this challenge. It has been really producive.

1/5

SAP_CAP_Wk2_1_5.png

2/5

Inserting 2 questionsInserting 2 questions

3/5

Adding 2 more questions but only 1 was addedAdding 2 more questions but only 1 was added

4/5

All QUESTIONS queryAll QUESTIONS query

5/5

Trying to add 1 more questionTrying to add 1 more question

 

garyzuo
Explorer

ajos
Explorer

onFlow
Explorer