Hi guys, this week we will start series discuss about SAP CAP.
The SAP Cloud Application Programming Model (CAP) is a framework of languages, libraries, and tools for building enterprise-grade services and applications. It guides developers along a ‘golden path’ of proven best practices and a great wealth of out-of-the-box solutions to recurring tasks.
about CAP
We can use Visual Code with SAP CDS Language Support extension and Node.JS, or we also use SAP Business Application Studio as well to develop CAP project.
Kindly reference some resouces
In this first article, we will take a look
- Exploring ODATA V4 base on Northwind Example
- Exploring CDS base on Hello-world project
- Build your first ODATA-BASED V4 backend service
Ok, let’s go !
Exploring ODATA base on Northwind Example
Take a look metada of Northwind example we will have
Entity Type

Asscociation

Explore the navigation properties from a product



Next, we will working with entity Products with some syntax of ODATA v4
GET all products
https://services.odata.org/V4/Northwind/Northwind.svc/Products

GET top 3 products – $top
https://services.odata.org/V4/Northwind/Northwind.svc/Products?$top=3

GET some properties – $select
https://services.odata.org/V4/Northwind/Northwind.svc/Products?$top=1&$select=ProductID,ProductName,CategoryID,UnitsInStock

GET detail navigation – $expand
Default, data of navigation properties not display in query . If we want get details of navigation properties, we can use syntax $expand($select <property name>)
https://services.odata.org/V4/Northwind/Northwind.svc/Products?$select=ProductID,ProductName&$expand=Category($select=CategoryID,CategoryName)&$filter=ProductName eq 'CHAI'

GET data with filer – $filter
https://services.odata.org/V4/Northwind/Northwind.svc/Products?$select=ProductID,ProductName&$expand=Category($select=CategoryID,CategoryName)&$filter=ProductName eq 'CHAI'

Exploring CDS base on Hello-world project
Initial project CAP
In this section, we will expore first CAP project with Hello-word.
- Go to folder which create project
- Open bash
- Run
cds initial <folder-name>
- Open visual code
- Open folder
Take a look folder structure CAP Project

- Folder db: Define entity
- Folder srv: Define service and custom logic
Define entity
- Go to folder db
- Create file with extension is CDS
- Add code create entity Products
namespace db;
entity Products {
key ProductID :Integer;
ProductName:String;
UnitsInStock:Integer
}

Define service
- Go to folder srv
- Create file with extension is CDS
- Add code service
using db from '../db/schema';
service say @(impl:'./main.js') {
entity Products as projection on db.Products;
function hello (name:String) returns String;
function hello_vietnam (name:String) returns String;
function hello_japan (name:String) returns String;
}

Implement service
- Go to folder srv
- Create file name with extension JS in the same folder with file extension CDS
NOTE
If you want put file JS in any where in folder srv, you have to use anotation @imp to detemine this file JS
Way 01: Using Express.JS

Way02: Using ES6

Way03

class say {
async init(){
this.on('hello',async req=>{
return `Hello ${req.data.name}!!!!`;
})
this.on('hello_vietnam',async req=>{
return `Xin chào ${req.data.name}!!!!`;
})
this.on('hello_japan',async req=>{
return `Ohayo ${req.data.name}!!!!`;
})
return super.init();
}
}
module.exports = say;
Run project
After add code, we will use command : cds watch to run project


View metadata, we will have

Test case
Test function hello
http://localhost:4004/say/hello(name='JOSEPH')

Build your first ODATA-BASED V4 backend service
In this section, we will build our first ODATA-BASED V4 backend service. We will:
- Create two entities: Products and Categories and relationship
- Create data by file CSV
- Processing CRUD on entity Products
- Add custome logic code
- Define and implement function import
Create Products and Categories and Relationship
- Go to file with extention CDS in folder db
- Add code
namespace db;
entity Products {
key ProductID : Integer;
ProductName:String;
UnitsInStock:Integer;
Status:String;
Category : Association to Categories;
}
entity Categories {
key CategoryID : Integer;
CategoryName : String;
Description : String;
Products : Association to many Products
on Products.Category =$self;
}

Create data by file CSV
- Go to folder db
- Create new folder data
- Create file CSV with name syntax: <name space>-<entity name>
NOTE
- CSV file location:
- csv or data subfolder related to a CDS model file, including db/csv and db/data folders
- db/src/** folder for CSV files and corresponding hdbtabledata files. These files will be treated as native SAP HANA artifacts and deployed as they are.
- Each file contains data for one entity.
- File names must follow the pattern namespace-entity.csv.
Pattern for nested entities: namespace-entity.nestedEntity.csv.
Examples: my.bookshop-Books.csv, or my.bookshop-Books.ISBN.csv.- They must start with a header line that lists the needed element names.


Processing CRUD on Products entity
Get all products
http://localhost:4004/main/Products

Create new product
http://localhost:4004/main/Products

Edit product
http://localhost:4004/main/Products(20)

Delete Product
http://localhost:4004/main/Products(20)

Add custom logic code
Example we want to check stock of product if it greater than 20 then data of field status will display OK. To do this, we will add custom logic code into service by file JS
Open file JS in folder SRV and add code
srv.after('READ', 'Products', items => {
return items.map(item => {
if (item.UnitsInStock > 20) {
item.Status = ' OK !'
}
return item
})
})
Check on POSTMAN we will see that data of field Status is updated.

Define and implement function import
In case we want to add function into ODATA, example add function Total Stock count look like

To do this, very simple, just add one function into service and implement it on JS file.


Test on POSTMAN
http://localhost:4004/main/TotalStockCount()

SUMMARY
In this article I shared my understand about ODATA V4 and How to create ODATA backend by using CDS in CAP Project. CDS is the backbone of the SAP Cloud Application Programming Model (CAP). Kindly follow step by step in section Hello-World project for get some concept about CDS and from there we will discuss more and more on next article in this blog. Thanks for your reading and any advise kindy leave your comment on this.
Many thanks.
JOSHEPH.
2 comments