Skip to content

Commit

Permalink
feat(core): Enhance doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Gaudek authored and Simon Gaudek committed Jun 25, 2021
1 parent 7554a25 commit ac5ec0d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"scripts": {
"start": "node ./srv/server.js",
"dev": "npm run build:cds && nodemon",
"watch": "cds watch",
"deploy": "cds deploy --to sqlite:bookshop.db",
"build": "npm run build:cds && npm run build:ts",
"build:cds": "echo 'Compiling cds sources...' && cds2types --cds ./srv/cat-service.cds --output ./src/entities.ts --prefix I -f && cds build && cp .cdsrc.json gen/srv",
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ npm install

### Local Development

For local development, nodemon is installed, which restarts the node service when changes are made to the TypeScript sources. Here the sources are recompiled and the service is restarted.
For local development, **[nodemon](https://nodemon.io/)** is used, which restarts the node service when changes are made to the TypeScript sources. Here the sources are recompiled and the service is restarted.

```
npm run dev
Expand Down
15 changes: 11 additions & 4 deletions src/entities/BookHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Handler, Req, AfterRead, Entities} from "cds-routing-handlers";
import { Handler, Req, AfterRead, Entities, BeforeRead } from "cds-routing-handlers";
import { CatalogService } from "../entities";

/**
Expand All @@ -9,9 +9,16 @@ import { CatalogService } from "../entities";
*/
@Handler(CatalogService.SanitizedEntity.Book)
export class BookHandler {
/**
* @memberof BookHandler
*/
// You can also decorate multiple methods with the same docorator (@BeforRead in this example)
// This way you can structure the code which should be executed on the beforRead hook and break it down into smaller pieces
@BeforeRead()
public async checkAuth() {}
@BeforeRead()
public async doSomeOtherThings() {}

// The @Entities decorator imports the read entities
// The @Req decorator imports the current request object
// And there are lots of more decorators (@Jwt, @Data, @User, @Locale, @Param ...)
@AfterRead()
public async addDiscount(@Entities() book: CatalogService.IBook[], @Req() req: any): Promise<void> {
console.log("Read Books");
Expand Down
11 changes: 5 additions & 6 deletions src/functions/FunctionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ import { CatalogService } from "../entities";
*/
@Handler()
export class FunctionHandler {
/**
* @memberof FunctionHandler
*/
// The @Param decorator imports a url-parameter defined in your function definition
// The @Req decorator import the current request object
@Func(CatalogService.ActionSubmitOrder.name)
public async submitOrder(
@Param(CatalogService.ActionSubmitOrder.paramBook) book: CatalogService.IBook,
@Param(CatalogService.ActionSubmitOrder.paramBook) bookId: CatalogService.IBook["ID"],
@Param(CatalogService.ActionSubmitOrder.paramAmount) amount: number,
@Req() req: any
): Promise<void> {
const n = await cds
.update(CatalogService.SanitizedEntity.Book)
.with({ stock: { "-=": amount } })
.where({ ID: book.ID, stock: { ">=": amount } });
n > 0 || req.error(409, `${amount} exceeds stock for book #${book}`);
.where({ ID: bookId, stock: { ">=": amount } });
n > 0 || req.error(409, `${amount} exceeds stock for book #${bookId}`);
}
}

0 comments on commit ac5ec0d

Please sign in to comment.