Skip to content

Commit

Permalink
feat(srv): Adapt new versin of cds2types
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Perstorfer authored and Simon Perstorfer committed May 18, 2020
1 parent b26441d commit 607931b
Show file tree
Hide file tree
Showing 14 changed files with 474 additions and 721 deletions.
Binary file added bookshop.db
Binary file not shown.
Binary file removed db/bookshop.db
Binary file not shown.
1 change: 0 additions & 1 deletion index.js

This file was deleted.

721 changes: 198 additions & 523 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
"version": "1.0.0",
"description": "A simple self-contained bookshop service.",
"dependencies": {
"@sap/cds": "^3.33.1",
"@sap/cds": "^3.34.1",
"cds-routing-handlers": "^2.1.1",
"express": "^4.17.1",
"reflect-metadata": "^0.1.13"
},
"devDependencies": {
"@types/express": "^4.17.6",
"cds2types": "^1.1.3",
"cds2types": "^2.1.1",
"prettier": "^2.0.5",
"sqlite3": "^4.1.1",
"typescript": "^3.7.5"
"sqlite3": "^4.2.0",
"typescript": "^3.9.2"
},
"scripts": {
"start": "node --inspect=9230 ./srv/server.js",
"deploy": "cds deploy db --to sqlite:db/bookshop.db",
"build": "rm -rf ./srv/** && cp ./src/*.cds srv/ && cds build/all --clean && tsc",
"deploy": "cds deploy --to sqlite:bookshop.db",
"build": "npm run build:types && rm -rf ./srv/** && cp ./src/*.cds srv/ && cds build/all --clean && tsc",
"build:types": "cds2types --cds ./srv/cat-service.cds --output ./src/entities.ts --prefix I"
},
"cds": {
Expand All @@ -28,9 +28,8 @@
"requires": {
"db": {
"kind": "sqlite",
"model": "db",
"credentials": {
"database": "db/bookshop.db"
"database": "bookshop.db"
}
}
}
Expand Down
233 changes: 123 additions & 110 deletions src/entities.ts
Original file line number Diff line number Diff line change
@@ -1,131 +1,144 @@
export enum ActionSubmitOrder {
name = "submitOrder",
paramBook = "book",
paramAmount = "amount",
export namespace sap.capire.bookshop {
export interface IAuthor extends IManaged {
ID: number;
name: string;
dateOfBirth: Date;
dateOfDeath: Date;
placeOfBirth: string;
placeOfDeath: string;
books?: IBook[];
}
export interface IBook extends IManaged {
ID: number;
title: string;
descr: string;
author?: IAuthor;
author_ID?: number;
genre?: IGenre;
genre_ID?: number;
stock: number;
price: number;
currency: unknown;
currency_code?: string;
}
export interface IGenre extends sap.common.ICodeList {
ID: number;
parent?: IGenre;
parent_ID?: number;
children: unknown;
}
export enum Entity {
Author = "sap.capire.bookshop.Author",
Book = "sap.capire.bookshop.Book",
Genre = "sap.capire.bookshop.Genre"
}
export enum SanitizedEntity {
Author = "Author",
Book = "Book",
Genre = "Genre"
}
}

export interface IActionSubmitOrderParams {
book: unknown;
amount: number;
export namespace sap.common {
export interface ICodeList {
name: string;
descr: string;
}
export interface ICountries extends sap.common.ICodeList {
code: string;
}
export interface ICurrencies extends sap.common.ICodeList {
code: string;
symbol: string;
}
export interface ILanguages extends sap.common.ICodeList {
code: string;
}
export enum Entity {
CodeList = "sap.common.CodeList",
Countries = "sap.common.Countries",
Currencies = "sap.common.Currencies",
Languages = "sap.common.Languages"
}
export enum SanitizedEntity {
CodeList = "CodeList",
Countries = "Countries",
Currencies = "Currencies",
Languages = "Languages"
}
}

export interface ICurrencies_texts {
locale: string;
name: string;
descr: string;
code: string;
export namespace CatalogService {
export enum ActionSubmitOrder {
name = "submitOrder",
paramBook = "book",
paramAmount = "amount"
}
export interface IActionSubmitOrderParams {
book: unknown;
amount: number;
}
export interface IBook {
createdAt?: Date;
modifiedAt?: Date;
ID: number;
title: string;
descr: string;
author: string;
genre?: IGenre;
genre_ID?: number;
stock: number;
price: number;
currency: unknown;
currency_code?: string;
}
export interface ICurrencies {
name: string;
descr: string;
code: string;
symbol: string;
}
export interface IGenre {
name: string;
descr: string;
ID: number;
parent?: IGenre;
parent_ID?: number;
children: unknown;
}
export enum Entity {
Book = "CatalogService.Book",
Currencies = "CatalogService.Currencies",
Genre = "CatalogService.Genre"
}
export enum SanitizedEntity {
Book = "Book",
Currencies = "Currencies",
Genre = "Genre"
}
}

export interface ICurrencies {
name?: string;
descr?: string;
code: string;
symbol: string;
texts: unknown;
localized?: ICurrencies_texts;
export interface IUser {
}

export interface IServiceBooks {
createdAt?: Date;
modifiedAt?: Date;
ID: number;
title?: string;
descr?: string;
author: string;
genre?: IGenre;
genre_ID?: number;
stock: number;
price: number;
currency: unknown;
currency_code?: string;
texts: unknown;
localized?: IBook_texts;
export interface ICuid {
ID: string;
}

export interface IManaged {
createdAt?: Date;
createdBy?: string;
modifiedAt?: Date;
modifiedBy?: string;
}

export interface IAuthor extends IManaged {
ID: number;
name: string;
dateOfBirth: Date;
dateOfDeath: Date;
placeOfBirth: string;
placeOfDeath: string;
books?: IBook[];
}

export interface IBook extends IManaged {
ID: number;
title: string;
descr: string;
author?: IAuthor;
author_ID?: number;
genre?: IGenre;
genre_ID?: number;
stock: number;
price: number;
currency: unknown;
texts: unknown;
localized?: IBook_texts;
}

export interface IBook_texts {
locale: string;
ID: number;
title: string;
descr: string;
}

export interface IGenre {
name: string;
descr: string;
ID: number;
parent?: IGenre;
parent_ID?: number;
children: unknown;
texts: unknown;
localized?: IGenre_texts;
}

export interface IGenre_texts {
locale: string;
name: string;
descr: string;
ID: number;
}

export interface ITemporal {
validFrom: Date;
validTo: Date;
}

export enum Entity {
Book_texts = "sap.capire.bookshop.Book_texts",
Currencies = "localized.CatalogService.Currencies",
Currencies_texts = "CatalogService.Currencies_texts",
Genre = "sap.capire.bookshop.Genre",
Genre_texts = "sap.capire.bookshop.Genre_texts",
ServiceBooks = "localized.CatalogService.ServiceBooks",
Author = "sap.capire.bookshop.Author",
Book = "sap.capire.bookshop.Book",
User = "User",
Cuid = "cuid",
Managed = "managed",
Temporal = "temporal",
Temporal = "temporal"
}

export enum SanitizedEntity {
Book_texts = "Book_texts",
Currencies = "Currencies",
Currencies_texts = "Currencies_texts",
Genre = "Genre",
Genre_texts = "Genre_texts",
ServiceBooks = "ServiceBooks",
Author = "Author",
Book = "Book",
User = "User",
Cuid = "Cuid",
Managed = "Managed",
Temporal = "Temporal",
Temporal = "Temporal"
}
10 changes: 6 additions & 4 deletions src/entities/BookHandler.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { Handler, Req, AfterRead, BeforeCreate, BeforeUpdate, Entities, Jwt, Data, Locale } from "cds-routing-handlers";
import { SanitizedEntity, IBook } from "./../entities";
import { Handler, Req, AfterRead, Entities} from "cds-routing-handlers";
import { CatalogService } from "../entities";

/**
* Book handler.
*
* @export
* @class BookHandler
*/
@Handler(SanitizedEntity.Book)
@Handler(CatalogService.SanitizedEntity.Book)
export class BookHandler {
/**
* @memberof BookHandler
*/
@AfterRead()
public async addDiscount(@Entities() book: IBook[], @Jwt() jwt: string): Promise<void> {
public async addDiscount(@Entities() book: CatalogService.IBook[], @Req() req: any): Promise<void> {
console.log("Read Books");

for (const each of book) {
if (each.stock > 111) each.title += ` -- 11% discount!`;
}
Expand Down
11 changes: 6 additions & 5 deletions src/functions/FunctionHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cds from "@sap/cds";
import { Handler, Req, Func, Param } from "cds-routing-handlers";
import { SanitizedEntity, IBook, ActionSubmitOrder } from "../entities";
import { CatalogService } from "../entities";

/**
* Function handler.
Expand All @@ -12,14 +13,14 @@ export class FunctionHandler {
/**
* @memberof FunctionHandler
*/
@Func(ActionSubmitOrder.name)
@Func(CatalogService.ActionSubmitOrder.name)
public async submitOrder(
@Param(ActionSubmitOrder.paramBook) book: IBook,
@Param(ActionSubmitOrder.paramAmount) amount: number,
@Param(CatalogService.ActionSubmitOrder.paramBook) book: CatalogService.IBook,
@Param(CatalogService.ActionSubmitOrder.paramAmount) amount: number,
@Req() req: any
): Promise<void> {
const n = await cds
.update(SanitizedEntity.Book)
.update(CatalogService.SanitizedEntity.Book)
.with({ stock: { "-=": amount } })
.where({ ID: book.ID, stock: { ">=": amount } });
n > 0 || req.error(409, `${amount} exceeds stock for book #${book}`);
Expand Down
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Server {

await cds.connect("db");
await cds
.serve(__dirname + "/gen/csn.json")
.serve("./srv/gen/")
.at("odata")
.in(app)
.with(hdl);
Expand Down
Loading

0 comments on commit 607931b

Please sign in to comment.