Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
chgeo and danjoa committed Feb 24, 2021
1 parent d368eb2 commit 65c8c82
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 4 deletions.
1 change: 1 addition & 0 deletions fiori/app/_i18n/i18n.properties
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions fiori/app/_i18n/i18n_de.properties
Expand Up @@ -6,6 +6,7 @@ Authors = Autoren
Author = Autor
AuthorID = ID des Autors
AuthorName = Name des Autors
Age = Alter
Name = Name
Stock = Bestand
Order = Bestellung
Expand Down
23 changes: 22 additions & 1 deletion fiori/app/admin/fiori-service.cds
@@ -1,4 +1,4 @@
using AdminService from '@capire/bookshop';
using { AdminService } from '../../db';

////////////////////////////////////////////////////////////////////////////
//
Expand Down Expand Up @@ -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}'},
]
},
}
);



////////////////////////////////////////////////////////////
Expand Down
10 changes: 10 additions & 0 deletions fiori/db/hana/index.cds
@@ -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 changes: 8 additions & 0 deletions fiori/db/index.cds
@@ -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 changes: 10 additions & 0 deletions fiori/db/sqlite/index.cds
@@ -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
}
12 changes: 9 additions & 3 deletions fiori/package.json
Expand Up @@ -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?",
Expand All @@ -23,8 +23,14 @@
"kind": "odata", "model": "@capire/orders"
},
"db": {
"kind": "sql"
"kind": "sql",
"[development]": {
"model": "db/sqlite"
},
"[production]": {
"model": "db/hana"
}
}
}
}
}
}
12 changes: 12 additions & 0 deletions fiori/test/requests.http
Expand Up @@ -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"
}

0 comments on commit 65c8c82

Please sign in to comment.