In this exercise, you will learn how to implement CRUD operations in SAP Cloud SDK application. We will use the Mock Server instead of the actual SAP S/4HANA Cloud.
Prerequisites
For the complete execution of current exercise, you must execute the following activities first, using SAP Business Application Studio:
- Execute the following exercises:
- Creating your free trial account in SAP BTP
- Setting up the mock server, so that you have the Mock server program available in the cloud-s4-sdk-book folder.
- Run the following commands in a terminal, in case you didn't do it already in previous exercises, to clone the Git repository for the course:Code Snippet12cd ~/projects git clone https://github.com/SAP-samples/cloud-sdk-learning-journey.git
- Choose File → Open Folder and open the /home/user/projects/cloud-sdk-learning-journey/ folder.
Steps
Checkout the branch tsnestjs_v3.0_crud in SAP Business Application Studio
Open SAP Business Application Studio.
Run the following commands in the terminal.
Code Snippet12cd ~/projects/cloud-sdk-learning-journey git checkout -f tsnestjs_v3.0_crud
Build and run the project.
Run the application by executing the following commands in the terminal
Code Snippet123cd cloudsdk_bp_ts npm i npm run start:dev
Query the Business Partner API of SAP S/4HANA Cloud
Open the requests.http file in the cloudsdk_bp_ts folder
Click on Send Request in the requests.http file to submit the GET request for business partners
Verify that the list of business partners are returned from the Mock Server. Note: If the Mock Server is not running, make sure to run the Mock Server on port 8081 by following the prerequisites at the top
Click on the various Send Request in the requests.http file to submit GET, PUT, POST requests
Verify that the response is correct for all the requests
Stop the running application using Ctrl + C in the corresponding terminal window.
Optional: code walk-through.
Review code in the following source files:
File name Comments on code changes business-partner.controller.ts Code logic is as follows...
- Annotate the class BusinessPartnerController with @Controller to mark it as a Controller class
- Create 5 methods getBusinessPartners, getBusinessPartnerById, createAddress, updateBusinessPartnerAddress, deleteBusinessPartnerAddress to handle the GET, GET/{id}, POST, PUT, DELETE requests
- Annotate each method with the respective @Get(), @Get('/:id'), @Post('/:businessPartnerId/address'), @Put('/:businessPartnerId/address/:addressId'), @Delete('/:businessPartnerId/address/:addressId') respectively
- Create an instance of the BusinessPartnerService class to handle each of the method implementation by passing in the required values
business-partner.service.ts Code logic is as follows...
- Implement logic for each of the methods in the Controller class
- Use the SAP Cloud SDK fluent API method chaining to provide additional parameters
requests.http Provides various HTTP requests that can be submitted to query the Business Partner API commands.txt Provides various commands that can be run on the terminal Note
Check out the SAP Cloud SDK documentation on Building an Address Manager Application with SAP Cloud SDK. https://sap.github.io/cloud-sdk/docs/js/tutorials/address-manager/change