Showcase how to integrate DB-specific functions (#201)
Augment `AdminService.Author` by 2 fields `age` and `lifetime` whose values are computed by SQLite and HANA-specific functions. Don't do this in bookshop directly, to keep it simple. Instead, use the fiori module, which also allows to integrate the fields in the UI. Co-authored-by: Daniel <daniel.hutzel@sap.com>
This commit is contained in:
@@ -11,6 +11,7 @@ DateOfBirth = Date of Birth
|
||||
DateOfDeath = Date of Death
|
||||
PlaceOfBirth = Place of Birth
|
||||
PlaceOfDeath = Place of Death
|
||||
Age = Age
|
||||
Authors = Authors
|
||||
Order = Order
|
||||
Orders = Orders
|
||||
|
||||
@@ -6,6 +6,7 @@ Authors = Autoren
|
||||
Author = Autor
|
||||
AuthorID = ID des Autors
|
||||
AuthorName = Name des Autors
|
||||
Age = Alter
|
||||
Name = Name
|
||||
Stock = Bestand
|
||||
Order = Bestellung
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using AdminService from '@capire/bookshop';
|
||||
using { AdminService } from '../../db';
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
@@ -39,6 +39,27 @@ annotate AdminService.Books with @(
|
||||
}
|
||||
);
|
||||
|
||||
annotate AdminService.Authors with @(
|
||||
UI: {
|
||||
HeaderInfo: {
|
||||
Description: {Value: lifetime}
|
||||
},
|
||||
Facets: [
|
||||
{$Type: 'UI.ReferenceFacet', Label: '{i18n>Details}', Target: '@UI.FieldGroup#Details'},
|
||||
{$Type: 'UI.ReferenceFacet', Label: '{i18n>Books}', Target: 'books/@UI.LineItem'},
|
||||
],
|
||||
FieldGroup#Details: {
|
||||
Data: [
|
||||
{Value: placeOfBirth},
|
||||
{Value: placeOfDeath},
|
||||
{Value: dateOfBirth},
|
||||
{Value: dateOfDeath},
|
||||
{Value: age, Label: '{i18n>Age}'},
|
||||
]
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
10
fiori/db/hana/index.cds
Normal file
10
fiori/db/hana/index.cds
Normal file
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// Add Author.age and .lifetime with a DB-specific function
|
||||
//
|
||||
|
||||
using { AdminService } from '..';
|
||||
|
||||
extend projection AdminService.Authors with {
|
||||
YEARS_BETWEEN(dateOfBirth, dateOfDeath) as age: Integer,
|
||||
YEAR(dateOfBirth) || ' – ' || YEAR(dateOfDeath) as lifetime : String
|
||||
}
|
||||
8
fiori/db/index.cds
Normal file
8
fiori/db/index.cds
Normal file
@@ -0,0 +1,8 @@
|
||||
using { sap.capire.bookshop } from '@capire/bookshop';
|
||||
|
||||
// Forward-declare calculated fields to be filled in database-specific ways
|
||||
// TODO find a better way to have 'default' fields that still can be overwritten.
|
||||
extend bookshop.Authors with {
|
||||
virtual age: Integer;
|
||||
virtual lifetime: String;
|
||||
}
|
||||
10
fiori/db/sqlite/index.cds
Normal file
10
fiori/db/sqlite/index.cds
Normal file
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// Add Author.age and .lifetime with a DB-specific function
|
||||
//
|
||||
|
||||
using { AdminService } from '..';
|
||||
|
||||
extend projection AdminService.Authors with {
|
||||
strftime('%Y',dateOfDeath)-strftime('%Y',dateOfBirth) as age: Integer,
|
||||
strftime('%Y',dateOfBirth) || ' – ' || strftime('%Y',dateOfDeath) as lifetime : String
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
"@capire/common": "*",
|
||||
"@sap/cds": "^4",
|
||||
"express": "^4.17.1",
|
||||
"passport": "0.4.1"
|
||||
"passport": "^0.4.1"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "cds run --in-memory?",
|
||||
@@ -23,8 +23,14 @@
|
||||
"kind": "odata", "model": "@capire/orders"
|
||||
},
|
||||
"db": {
|
||||
"kind": "sql"
|
||||
"kind": "sql",
|
||||
"[development]": {
|
||||
"model": "db/sqlite"
|
||||
},
|
||||
"[production]": {
|
||||
"model": "db/hana"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,3 +63,15 @@ Content-Type: application/json
|
||||
|
||||
### Get active order
|
||||
GET {{bookshop}}/orders/Orders(ID={{newOrderID}},IsActiveEntity=true)
|
||||
|
||||
### Create author
|
||||
POST {{bookshop}}/admin/Authors
|
||||
Content-Type: application/json
|
||||
Authorization: Basic alice:
|
||||
|
||||
{
|
||||
"ID": 200,
|
||||
"name": "William Shakespeare",
|
||||
"dateOfBirth": "1564-04-26",
|
||||
"dateOfDeath": "1616-04-23"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user