Exercise: Implementing CRUD Operations in a TypeScript NestJS Application

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Implementing CRUD operations in a TypeScript NestJS application

Implementing CRUD operations in a TypeScript NestJS application

Implementing CRUD operations in SAP Cloud SDK application

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:

  1. Execute the following previous exercises:
    • Exercise Creating your Pay-As-You-Go Account in SAP BTP, so that you have a SAP BTP account.
    • Exercise Setting up the mock server. You need to have the mock server running on port 8081, eventually start it via the following terminal commands:
      Code snippet
      cd ~/projects/cloud-s4-sdk-book
      npm run start
      Expand
  2. 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 snippet
    cd ~/projects
    git clone https://github.com/SAP-samples/cloud-sdk-learning-journey.git
    Expand

Steps

  1. Checkout the branch tsnestjs_v3.0_crud in SAP Business Application Studio

    1. Open SAP Business Application Studio.

    2. Run the following commands in the terminal.

      Code snippet
      cd ~/projects/cloud-sdk-learning-journey
      git checkout tsnestjs_v3.0_crud
      Expand
  2. Build and run the project.

    1. Run the application by executing the following commands in the terminal

      Code snippet
      cd cloudsdk_bp_ts
      npm i
      npm run build
      npm run start:dev
      Expand
  3. Query the Business Partner API of SAP S/4HANA Cloud

    1. Open the requests.http file

    2. Click on Send Request in the requests.http file to submit the GET request for business partners

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

    4. Click on the various Send Request in the requests.http file to submit GET, PUT, POST requests

    5. Verify that the response is correct for all the requests

  4. Optional: Review code in the following source files

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

      Code walk-through

      File nameComments on code changes
      business-partner.controller.ts

      Code logic is as follows...

      1. Annotate the class BusinessPartnerController with @Controller to mark it as a Controller class
      2. Create 5 methods getBusinessPartners, getBusinessPartnerById, createAddress, updateBusinessPartnerAddress, deleteBusinessPartnerAddress to handle the GET, GET/{id}, POST, PUT, DELETE requests
      3. Annotate each method with the respective @Get(), @Get('/:id'), @Post('/:businessPartnerId/address'), @Put('/:businessPartnerId/address/:addressId'), @Delete('/:businessPartnerId/address/:addressId') respectively
      4. 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...

      1. Implement logic for each of the methods in the Controller class
      2. Use the SAP Cloud SDK fluent API method chaining to provide additional parameters
      requests.httpProvides various HTTP requests that can be submitted to query the Business Partner API
      commands.txtProvides various commands that can be run on the terminal
  5. Optional: Manually create the project, without using the git branch, starting from the application you already created in the folder ~/projects/cloudsdk_bp_ts, within the previous exercise (Implementing resilience in a TypeScript NestJS application).

    1. Choose FileOpen Folder... and open projects/cloudsdk_bp_ts.

    2. Create or update the following files in the table below

      File nameTypeContents
      business-partner.controller.ts

      Path: ~/projects/cloudsdk_bp_ts/src/business-partner

      Modified

      Copy contents from here...

      business-partner.service.ts

      Path: ~/projects/cloudsdk_bp_ts/src/business-partner

      Modified

      Copy contents from here...

    3. Build and run the project in projects/cloudsdk_bp_ts in the same exact way you did in the previous steps with project in projects/cloud-sdk-learning-journey/cloudsdk_bp_ts.

      Code snippet
      cd ~/projects/cloudsdk_bp_ts
      npm i
      npm run build
      npm run start:dev
      Expand

Log in to track your progress & complete quizzes