Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved samples for new capire #778

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bookshop/app/vue/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ const books = Vue.createApp ({
search: ({target:{value:v}}) => books.fetch(v && '&$search='+v),

async fetch (etc='') {
const {data} = await GET(`/ListOfBooks?$expand=genre($select=name),currency($select=symbol)${etc}`)
const {data} = await GET(`/Books?${etc}`)
books.list = data.value
},

async inspect (eve) {
const book = books.book = books.list [eve.currentTarget.rowIndex-1]
const res = await GET(`/Books/${book.ID}?$select=descr,stock,image`)
const res = await GET(`/Book/${book.ID}?$select=descr,stock,image`)
Object.assign (book, res.data)
books.order = { quantity:1 }
setTimeout (()=> $('form > input').focus(), 111)
Expand Down
4 changes: 2 additions & 2 deletions bookshop/app/vue/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ <h1> Capire Books </h1>
<tr v-for="book in list" v-bind:id="book.ID" v-on:click="inspect">
<td>{{ book.title }}</td>
<td>{{ book.author }}</td>
<td>{{ book.genre.name }}</td>
<td>{{ book.genre }}</td>
<td class="rating-stars">
{{ ('★'.repeat(Math.round(book.rating))+'☆☆☆☆☆').slice(0,5) }} ({{ book.numberOfReviews }})
</td>
<td>{{ book.currency && book.currency.symbol }} {{ book.price }}</td>
<td>{{ book.currency }} {{ book.price }}</td>
</tr>
</table>

Expand Down
2 changes: 1 addition & 1 deletion bookshop/srv/admin-service.cds
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using { sap.capire.bookshop as my } from '../db/schema';
service AdminService @(requires:'admin', path:'/admin') {
entity Books as projection on my.Books;
entity Authors as projection on my.Authors;
// entity Authors as projection on my.Authors;
}
19 changes: 12 additions & 7 deletions bookshop/srv/cat-service.cds
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ using { sap.capire.bookshop as my } from '../db/schema';
service CatalogService @(path:'/browse') {

/** For displaying lists of Books */
@readonly entity ListOfBooks as projection on Books
excluding { descr };
@readonly entity Books as projection on Book excluding { descr };

/** For display in details pages */
@readonly entity Books as projection on my.Books { *,
author.name as author
} excluding { createdBy, modifiedBy };
@readonly entity Book as projection on my.Books { *,
currency.name as currencyName, // flattened
currency.symbol as currency, // flattened
author.name as author, // flattened
genre.name as genre, // flattened
} excluding {
createdBy, modifiedBy, // as end users shouldn't see them
localized, texts, // as end users don't need them
};

@requires: 'authenticated-user'
action submitOrder ( book: Books:ID, quantity: Integer ) returns { stock: Integer };
event OrderedBook : { book: Books:ID; quantity: Integer; buyer: String };
action submitOrder ( book: Book:ID, quantity: Integer ) returns { stock: Integer };
event OrderedBook : { book: Book:ID; quantity: Integer; buyer: String };
}
4 changes: 2 additions & 2 deletions bookshop/srv/cat-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const cds = require('@sap/cds')
class CatalogService extends cds.ApplicationService { init() {

const { Books } = cds.entities('sap.capire.bookshop')
const { ListOfBooks } = this.entities
const { Books:Book } = this.entities

// Add some discount for overstocked books
this.after('each', ListOfBooks, book => {
this.after('each', Book, book => {
if (book.stock > 111) book.title += ` -- 11% discount!`
})

Expand Down
13 changes: 7 additions & 6 deletions bookshop/test/requests.http
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@server = http://localhost:4004
@me = Authorization: Basic {{$processEnv USER}}:
@alice = Authorization: Basic alice:


### ------------------------------------------------------------------------
Expand All @@ -16,11 +17,11 @@ GET {{server}}/browse/$metadata

### ------------------------------------------------------------------------
# Browse Books as any user
GET {{server}}/browse/ListOfBooks?
GET {{server}}/admin/Books?
# &$select=title,stock
&$expand=genre
# &sap-language=de
{{me}}
{{alice}}


### ------------------------------------------------------------------------
Expand All @@ -30,13 +31,13 @@ GET {{server}}/admin/Authors?
# &$expand=books($select=title;$expand=currency)
# &$filter=ID eq 101
# &sap-language=de
Authorization: Basic alice:
{{alice}}

### ------------------------------------------------------------------------
# Create Author
POST {{server}}/admin/Authors
Content-Type: application/json;IEEE754Compatible=true
Authorization: Basic alice:
{{alice}}

{
"ID": 112,
Expand All @@ -49,7 +50,7 @@ Authorization: Basic alice:
# Create book
POST {{server}}/admin/Books
Content-Type: application/json;IEEE754Compatible=true
Authorization: Basic alice:
{{alice}}

{
"ID": 2,
Expand All @@ -67,7 +68,7 @@ Authorization: Basic alice:
# Put image to books
PUT {{server}}/admin/Books(2)/image
Content-Type: image/png
Authorization: Basic alice:
{{alice}}

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANwAAADcCAYAAAAbWs+BAAAGwElEQVR4Ae3cwZFbNxBFUY5rkrDTmKAUk5QT03Aa44U22KC7NHptw+DRikVAXf8fzC3u8Hj4R4AAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAgZzAW26USQT+e4HPx+Mz+RRvj0e0kT+SD2cWAQK1gOBqH6sEogKCi3IaRqAWEFztY5VAVEBwUU7DCNQCgqt9rBKICgguymkYgVpAcLWPVQJRAcFFOQ0jUAsIrvaxSiAqILgop2EEagHB1T5WCUQFBBflNIxALSC42scqgaiA4KKchhGoBQRX+1glEBUQXJTTMAK1gOBqH6sEogKCi3IaRqAWeK+Xb1z9iN558fHxcSPS9p2ezx/ROz4e4TtIHt+3j/61hW9f+2+7/+UXbifjewIDAoIbQDWSwE5AcDsZ3xMYEBDcAKqRBHYCgtvJ+J7AgIDgBlCNJLATENxOxvcEBgQEN4BqJIGdgOB2Mr4nMCAguAFUIwnsBAS3k/E9gQEBwQ2gG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UBOQHA5S5MItAKCa4lsIJATEFzO0iQCrYDgWiIbCOQEBJezNIlAKyC4lsgGAjkBweUsTSLQCgiuJbKBQE5AcDlLkwi0Akff//Dz6U+/I6U1/sUNr3bnytl3kPzi4bXb/cK1RDYQyAkILmdpEoFWQHAtkQ0EcgKCy1maRKAVEFxLZAOBnIDgcpYmEWgFBNcS2UAgJyC4nKVJBFoBwbVENhDICQguZ2kSgVZAcC2RDQRyAoLLWZpEoBUQXEtkA4GcgOByliYRaAUE1xLZQCAnILicpUkEWgHBtUQ2EMgJCC5naRKBVkBwLZENBHIC/4M7TXIv+3PS22d24qvdQfL3C/7N5P5i/MLlLE0i0AoIriWygUBOQHA5S5MItAKCa4lsIJATEFzO0iQCrYDgWiIbCOQEBJezNIlAKyC4lsgGAjkBweUsTSLQCgiuJbKBQE5AcDlLkwi0AoJriWwgkBMQXM7SJAKtgOBaIhsI5AQEl7M0iUArILiWyAYCOQHB5SxNItAKCK4lsoFATkBwOUuTCBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIDAvyrwDySEJ2VQgUSoAAAAAElFTkSuQmCC

Expand Down
16 changes: 9 additions & 7 deletions fiori/app/browse/fiori-service.cds
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using CatalogService from '@capire/bookstore';
//
// Books Object Page
//
annotate CatalogService.Books with @(UI : {
annotate CatalogService.Book with @(UI : {
HeaderInfo : {
TypeName : '{i18n>Book}',
TypeNamePlural : '{i18n>Books}',
Expand All @@ -24,7 +24,7 @@ annotate CatalogService.Books with @(UI : {
FieldGroup #Price : {Data : [
{Value : price},
{
Value : currency.symbol,
Value : currencyName,
Label : '{i18n>Currency}'
},
]},
Expand All @@ -35,11 +35,11 @@ annotate CatalogService.Books with @(UI : {
//
// Books List Page
//
annotate CatalogService.Books with @(UI : {
annotate CatalogService.Book with @(UI : {
SelectionFields : [
ID,
price,
currency_code
currencyName
],
LineItem : [
{
Expand All @@ -50,8 +50,10 @@ annotate CatalogService.Books with @(UI : {
Value : author,
Label : '{i18n>Author}'
},
{Value : genre.name},
{Value : genre},
{Value : price},
{Value : currency.symbol},
{Value : currencyName},
]
}, );
}) {
currencyName @Common.Label : '{i18n>Currency}';
};
4 changes: 1 addition & 3 deletions fiori/app/common.cds
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ annotate my.Books with @(
ID,
author_ID,
price,
currency_code
],
LineItem : [
{ Value: ID, Label: '{i18n>Title}' },
{ Value: author.ID, Label: '{i18n>Author}' },
{ Value: genre.name },
{ Value: stock },
{ Value: price },
{ Value: currency.symbol },
]
}
) {
Expand Down Expand Up @@ -63,7 +61,7 @@ annotate my.Books with {
title @title: '{i18n>Title}';
genre @title: '{i18n>Genre}' @Common: { Text: genre.name, TextArrangement: #TextOnly };
author @title: '{i18n>Author}' @Common: { Text: author.name, TextArrangement: #TextOnly };
price @title: '{i18n>Price}' @Measures.ISOCurrency : currency_code;
price @title: '{i18n>Price}' @Measures.ISOCurrency : currency;
stock @title: '{i18n>Stock}';
descr @title: '{i18n>Description}' @UI.MultiLineText;
image @title: '{i18n>Image}';
Expand Down
12 changes: 6 additions & 6 deletions test/localized-data/services.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ describe('cap/samples - Localized Data', () => {
})

it('supports queries with $expand', async () => {
const { data } = await GET(`/browse/Books?&$select=title,author&$expand=currency`, {
const { data } = await GET(`/browse/Book?&$select=title,author,currencyName`, {
headers: { 'Accept-Language': 'de' },
})
expect(data.value).to.containSubset([
{ title: 'Sturmhöhe', author: 'Emily Brontë', currency: { name: 'Pfund' } },
{ title: 'Jane Eyre', author: 'Charlotte Brontë', currency: { name: 'Pfund' } },
{ title: 'The Raven', author: 'Edgar Allen Poe', currency: { name: 'US-Dollar' } },
{ title: 'Eleonora', author: 'Edgar Allen Poe', currency: { name: 'US-Dollar' } },
{ title: 'Catweazle', author: 'Richard Carpenter', currency: { name: 'Yen' } },
{ title: 'Sturmhöhe', author: 'Emily Brontë', currencyName: 'Pfund' },
{ title: 'Jane Eyre', author: 'Charlotte Brontë', currencyName: 'Pfund' },
{ title: 'The Raven', author: 'Edgar Allen Poe', currencyName: 'US-Dollar' },
{ title: 'Eleonora', author: 'Edgar Allen Poe', currencyName: 'US-Dollar' },
{ title: 'Catweazle', author: 'Richard Carpenter', currencyName: 'Yen' },
])
})

Expand Down
12 changes: 6 additions & 6 deletions test/odata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ describe('cap/samples - Bookshop APIs', () => {
'odata-version': '4.0',
})
expect(headers['content-type']).to.match(/application\/xml/)
expect(data).to.contain('<EntitySet Name="Books" EntityType="CatalogService.Books">')
expect(data).to.contain('<Annotation Term="Common.Label" String="Currency"/>')
expect(data).to.contain('<EntitySet Name="Books" EntityType="CatalogService.Books"/>')
expect(data).to.contain('<Annotation Term="Common.Label" String="Currency Symbol"/>')
})

it('serves ListOfBooks?$expand=genre,currency', async () => {
it('serves Books?$expand=genre,currency', async () => {
const Mystery = { ID: 16, name: 'Mystery', descr: null, parent_ID: 10 }
const Romance = { ID: 15, name: 'Romance', descr: null, parent_ID: 10 }
const USD = { code: 'USD', name: 'US Dollar', descr: null, symbol: '$' }
const { data } = await GET `/browse/ListOfBooks ${{
const { data } = await GET `/admin/Books ${{
params: { $search: 'Po', $select: `title,author`, $expand:`genre,currency` },
}}`
expect(data.value).to.containSubset([
{ ID: 251, title: 'The Raven', author: 'Edgar Allen Poe', genre:Mystery, currency:USD },
{ ID: 252, title: 'Eleonora', author: 'Edgar Allen Poe', genre:Romance, currency:USD },
{ ID: 251, title: 'The Raven', author_ID: 150, genre:Mystery, currency:USD },
{ ID: 252, title: 'Eleonora', author_ID: 150, genre:Romance, currency:USD },
])
})

Expand Down
Loading