Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

readme.md

Chapter 02 - Preparing the CAP Server

By the end of this chapter we will have prepared our CAP server for the scenario we want to built, which is a supermarket application, that allows user to browse a list of products. This chapter has little (nothing) to do with UI5, but is necessary to get the backend ready for the UI5 app.

Steps

1. Replace the data model

Our CAP server by default was generated including a bookshop data model. We will replace it with a supermarket data model.

➡️ Replace the content of the codejam.supermarket/server/db/schema.cds file with the following code:

namespace supermarket;

using { cuid, managed } from '@sap/cds/common';

entity Products {
    key ID       : Integer;
        title    : String;
        category : Association to one Categories;
        stock    : Integer;
        position : String;
        image    : String;
}

entity Categories {
    key name     : String;
        products : Association to many Products on products.category = $self;
}

entity Ratings: cuid, managed {
	rating: Integer @assert.range: [1,5];
}

2. Add sample data

We will add sample data to our CAP server, so that we can test the UI5 app later on.

➡️ Delete all files inside the codejam.supermarket/server/db/data/ directory and create the following new files inside that directory (naming is important!):

supermarket-Products.csv

ID,title,category_name,stock,position,image
1,Coca Cola,Beverages,100,"[{""x"": 9.9,""y"": 1.99,""z"": -5.92},{""x"":9.27,""y"":0.73,""z"":-0.31}]",https://codejam-supermarket-image-server.cfapps.us10.hana.ondemand.com/1.png
2,Cheerios,Cereal,500,"[{""x"":-1.76,""y"":1.72,""z"":-5.09},{""x"":-4.66,""y"":2.47,""z"":-0.08}]",https://codejam-supermarket-image-server.cfapps.us10.hana.ondemand.com/2.png

supermarket-Categories.csv

name
Beverages
Cereal

supermarket-Ratings.csv

ID,rating
83195589-b250-4657-95b6-4b95570eb0e0,4
cff42eb2-ca6d-4d8a-bca8-f50d0d7c5eb4,5

3. Add a service definition

We want to expose the data model via a service, so that we can consume it in our UI5 app.

➡️ Replace the current content of the codejam.supermarket/server/srv/cat-service.cds file with the following code:

using supermarket from '../db/schema';

service CatalogService {
    @readonly
    entity Products as projection on supermarket.Products;

    @readonly
    entity Ratings  as projection on supermarket.Ratings;

	action createRating(rating : Integer) returns Ratings;
    function getAvgRating() returns Decimal;
}

4. Add service implementation

➡️ Create a new file codejam.supermarket/server/srv/cat-service.js and add the following code:

module.exports = function CatalogService() {
	this.on("getAvgRating", async () => {
		const ratings = await SELECT("rating").from("Ratings")
		const avg = ratings.map(r => r.rating).reduce((a,b) => a + b)/ratings.length
		return avg.toFixed(2)
	})

	this.on("createRating", async ({ data: { rating } }) => {
		const result = await INSERT({ rating }).into("Ratings")
		const entries = [...result]
		return await SELECT.one.from("Ratings").where({ ID: entries[0].ID })
	})
}

We added a service implementation for the defined action and function from the previous step. We will need these in a later chapter.

5. Test the CAP server

➡️ Refresh your browser window at http://localhost:4004/ and inspect the service enpoints. In case you closed your server, restart it with the following command from the project root:

npm run dev:server

The CAP server is now serving the products data via the defined service and underlying supermarket data model. By default, the service protocol is OData V4.

updated-cds-server products-data

Continue to Chapter 03 - Adding Content to the UI5 Application