Skip to Content
Technical Articles

CRUD operations in Cloud Application Programming model

In this blog post will see how to do CRUD operations after defining service in CAPM

Please follow steps given in the below link to create database with sample entities and sample data till step “Querying thru OData out-of-box”

https://cap.cloud.sap/docs/get-started/in-a-nutshell

Now go to menu -> select New Terminal -> and run the command

cds run

Will get below screen in browser

Now preview Authors entity by clicking on Authors link. Will get below data

READ

Go to project root folder and create new folder called “tests”. We can create http file in root folder as well. If we are having multiple entities, it’s easy to keep it in common folder and separate .http file for each entity for easy access.

Create new file ‘authors.http’ under tests folder .

Now in authors.http type GET. we can find auto suggestions for operations and services as well. I am selecting authors

 

Click on Send Request to test GET. Other Pane will be opened  and response is displayed.

Successfully tested READ.

Comments can be added  using ### in http file.

 

CREATE

It’s time to add another author to this service. Type POST and select Authors again. Provide content-type as ‘application/json’. Entity has two properties ID, Name.

Will create sample payload with those properties. Click send request to post the data.

Post response is displayed in right pane. Response is  ‘201 Created’ new author ‘Robert’ added into entity. We can check the same from browser by refreshing

 

 

UPDATE

Let’s update authors name. Type PATCH and service name then author ID here it’s 121 and payload which has changes to be updated. Will update name from Robert to Robert Pattinson.

Click Send Request to see response

Same updates reflect in browser data also.

 

DELETE

In the http file type DELETE and choose service from autosuggestion. We are going to delete author whose ID is 121

So the http file will be like this

 

Click on Send Request to execute delete. Content was removed. If we check authors again, will reflect old data .

We have learned how to  execute create , read ,update ,delete operations  in  CAP project locally in business application studio.

Happy learning.

 

Be the first to leave a comment
You must be Logged on to comment or reply to a post.