Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit bfc0f16

Browse files
committed
formatting
1 parent dcb0c03 commit bfc0f16

File tree

3 files changed

+95
-68
lines changed

3 files changed

+95
-68
lines changed

bookshop/db/schema.cds

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
1-
using { Currency, managed, sap } from '@sap/cds/common';
1+
using {
2+
Currency,
3+
managed,
4+
sap
5+
} from '@sap/cds/common';
6+
27
namespace sap.capire.bookshop;
38

49
entity Books : managed {
5-
key ID : Integer;
6-
title : localized String(111) @mandatory;
7-
descr : localized String(1111);
8-
author : Association to Authors @mandatory;
9-
genre : Association to Genres;
10-
stock : Integer;
11-
price : Price;
12-
currency : Currency;
13-
image : LargeBinary @Core.MediaType: 'image/png';
10+
key ID : Integer;
11+
title : localized String(111) @mandatory;
12+
descr : localized String(1111);
13+
author : Association to Authors @mandatory;
14+
genre : Association to Genres;
15+
stock : Integer;
16+
price : Price;
17+
currency : Currency;
18+
image : LargeBinary @Core.MediaType: 'image/png';
1419
}
1520

1621
entity Authors : managed {
17-
key ID : Integer;
18-
name : String(111) @mandatory;
19-
dateOfBirth : Date;
20-
dateOfDeath : Date;
21-
placeOfBirth : String;
22-
placeOfDeath : String;
23-
books : Association to many Books on books.author = $self;
22+
key ID : Integer;
23+
name : String(111) @mandatory;
24+
dateOfBirth : Date;
25+
dateOfDeath : Date;
26+
placeOfBirth : String;
27+
placeOfDeath : String;
28+
books : Association to many Books
29+
on books.author = $self;
2430
}
2531

2632
/** Hierarchically organized Code List for Genres */
2733
entity Genres : sap.common.CodeList {
28-
key ID : UUID;
29-
parent : Association to Genres;
30-
children : Composition of many Genres on children.parent = $self;
34+
key ID : UUID;
35+
parent : Association to Genres;
36+
children : Composition of many Genres
37+
on children.parent = $self;
3138
}
3239

33-
type Price : Decimal(9,2);
40+
type Price : Decimal(9, 2);
3441

3542

3643
// ------------------------------------------------------------------

bookshop/srv/cat-service.cds

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
1-
using { sap.capire.bookshop as my } from '../db/schema';
2-
service CatalogService @(path:'/browse') {
1+
using {sap.capire.bookshop as my} from '../db/schema';
2+
3+
service CatalogService @(path: '/browse') {
34

45
/** For displaying lists of Books */
5-
@readonly entity ListOfBooks as projection on Books
6-
excluding { descr };
6+
@readonly
7+
entity ListOfBooks as
8+
projection on Books
9+
excluding {
10+
descr
11+
};
712

813
/** For display in details pages */
9-
@readonly entity Books as projection on my.Books { *,
10-
author.name as author
11-
} excluding { createdBy, modifiedBy };
14+
@readonly
15+
entity Books as
16+
projection on my.Books {
17+
*,
18+
author.name as author
19+
}
20+
excluding {
21+
createdBy,
22+
modifiedBy
23+
};
1224

1325
@requires: 'authenticated-user'
14-
action submitOrder ( book: Books:ID, quantity: Integer ) returns { stock: Integer };
15-
event OrderedBook : { book: Books:ID; quantity: Integer; buyer: String };
26+
action submitOrder(book : Books:ID, quantity : Integer) returns {
27+
stock : Integer
28+
};
29+
30+
event OrderedBook : {
31+
book : Books:ID;
32+
quantity : Integer;
33+
buyer : String
34+
};
1635
}

bookshop/srv/cat-service.js

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1-
const cds = require('@sap/cds')
2-
3-
class CatalogService extends cds.ApplicationService { init() {
4-
5-
const { Books } = cds.entities('sap.capire.bookshop')
6-
const { ListOfBooks } = this.entities
7-
8-
// Add some discount for overstocked books
9-
this.after('each', ListOfBooks, book => {
10-
if (book.stock > 111) book.title += ` -- 11% discount!`
11-
})
12-
13-
// Reduce stock of ordered books if available stock suffices
14-
this.on('submitOrder', async req => {
15-
let { book:id, quantity } = req.data
16-
let book = await SELECT.from (Books, id, b => b.stock)
17-
18-
// Validate input data
19-
if (!book) return req.error (404, `Book #${id} doesn't exist`)
20-
if (quantity < 1) return req.error (400, `quantity has to be 1 or more`)
21-
if (quantity > book.stock) return req.error (409, `${quantity} exceeds stock for book #${id}`)
22-
23-
// Reduce stock in database and return updated stock value
24-
await UPDATE (Books, id) .with ({ stock: book.stock -= quantity })
25-
return book
26-
})
27-
28-
// Emit event when an order has been submitted
29-
this.after('submitOrder', async (_,req) => {
30-
let { book, quantity } = req.data
31-
await this.emit('OrderedBook', { book, quantity, buyer: req.user.id })
32-
})
33-
34-
// Delegate requests to the underlying generic service
35-
return super.init()
36-
}}
37-
38-
module.exports = CatalogService
1+
const cds = require("@sap/cds");
2+
3+
class CatalogService extends cds.ApplicationService {
4+
init() {
5+
const { Books } = cds.entities("sap.capire.bookshop");
6+
const { ListOfBooks } = this.entities;
7+
8+
// Add some discount for overstocked books
9+
this.after("each", ListOfBooks, (book) => {
10+
if (book.stock > 111) book.title += ` -- 11% discount!`;
11+
});
12+
13+
// Reduce stock of ordered books if available stock suffices
14+
this.on("submitOrder", async (req) => {
15+
let { book: id, quantity } = req.data;
16+
let book = await SELECT.from(Books, id, (b) => b.stock);
17+
18+
// Validate input data
19+
if (!book) return req.error(404, `Book #${id} doesn't exist`);
20+
if (quantity < 1) return req.error(400, `quantity has to be 1 or more`);
21+
if (quantity > book.stock) return req.error(409, `${quantity} exceeds stock for book #${id}`);
22+
23+
// Reduce stock in database and return updated stock value
24+
await UPDATE(Books, id).with({ stock: (book.stock -= quantity) });
25+
return book;
26+
});
27+
28+
// Emit event when an order has been submitted
29+
this.after("submitOrder", async (_, req) => {
30+
let { book, quantity } = req.data;
31+
await this.emit("OrderedBook", { book, quantity, buyer: req.user.id });
32+
});
33+
34+
// Delegate requests to the underlying generic service
35+
return super.init();
36+
}
37+
}
38+
39+
module.exports = CatalogService;

0 commit comments

Comments
 (0)