Skip to content

Commit 3ac871e

Browse files
committed
docs(either-errors): base book rental application
1 parent 4e8a27d commit 3ac871e

18 files changed

+156
-2
lines changed

articles/either-errors/package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

articles/either-errors/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121
"test:e2e": "jest --config ./test/jest-e2e.json"
2222
},
2323
"dependencies": {
24+
"@nestjs-architects/typed-cqrs": "0.1.1",
2425
"@nestjs/common": "^8.0.0",
2526
"@nestjs/core": "^8.0.0",
27+
"@nestjs/cqrs": "8.0.0",
2628
"@nestjs/platform-express": "^8.0.0",
2729
"fp-ts": "2.11.1",
2830
"reflect-metadata": "^0.1.13",
2931
"rimraf": "^3.0.2",
30-
"rxjs": "^7.2.0"
32+
"rxjs": "^7.2.0",
33+
"uuid": "8.3.2"
3134
},
3235
"devDependencies": {
3336
"@nestjs/cli": "^8.0.0",

articles/either-errors/src/app.controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { AppService } from './app.service';
33

44
@Controller()
55
export class AppController {
6+
// TODO add get / rent / return
7+
// TODO show mapper and symbols unions
68
constructor(private readonly appService: AppService) {}
79

810
@Get()

articles/either-errors/src/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { Module } from '@nestjs/common';
22
import { AppController } from './app.controller';
33
import { AppService } from './app.service';
4+
import { BookCatalogueModule } from './books-catalogue';
5+
import { BookRentalModule } from './book-rental';
46

57
@Module({
6-
imports: [],
8+
imports: [BookCatalogueModule, BookRentalModule],
79
controllers: [AppController],
810
providers: [AppService],
911
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Module } from '@nestjs/common';
2+
import { CqrsModule } from '@nestjs/cqrs';
3+
4+
import { BorrowBookHandler } from './borrow-book.handler';
5+
import { ReturnBookHandler } from './return-book.handler';
6+
7+
@Module({
8+
imports: [CqrsModule],
9+
providers: [BorrowBookHandler, ReturnBookHandler],
10+
})
11+
export class BookRentalModule {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Command } from '@nestjs-architects/typed-cqrs';
2+
import { Either } from 'fp-ts/Either';
3+
import { BorrowError } from './borrow-errors';
4+
5+
export class BorrowBook extends Command<Either<BorrowError, void>> {
6+
constructor(public readonly isbn: string) {
7+
super();
8+
}
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { CommandHandler, IInferredCommandHandler } from '@nestjs/cqrs';
2+
import { Either, left, right } from 'fp-ts/Either';
3+
import { BorrowBook } from './borrow-book.command';
4+
import { BorrowError, OutOfStock } from './borrow-errors';
5+
6+
import { outOfStockIsbn, limitReachedIsbn } from '../cheat-codes';
7+
8+
@CommandHandler(BorrowBook)
9+
export class BorrowBookHandler implements IInferredCommandHandler<BorrowBook> {
10+
async execute({ isbn }: BorrowBook): Promise<Either<BorrowError, void>> {
11+
if (isbn === outOfStockIsbn) {
12+
return left(OutOfStock);
13+
}
14+
if (isbn === limitReachedIsbn) {
15+
return left(OutOfStock);
16+
}
17+
18+
return right(undefined);
19+
}
20+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const OutOfStock = Symbol(`book out of stock`);
2+
export const BooksLimitReached = Symbol(`books limit reached`);
3+
4+
export type BorrowError = typeof OutOfStock | typeof BooksLimitReached;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export { BorrowBook } from './borrow-book.command';
2+
export { ReturnBook } from './return-book.command';
3+
4+
export { BookRentalModule } from './book-rental.module';
5+
6+
export { BorrowError, BooksLimitReached, OutOfStock } from './borrow-errors';
7+
export { ReturnError } from './return-error';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Command } from '@nestjs-architects/typed-cqrs';
2+
import { Either } from 'fp-ts/Either';
3+
import { ReturnError } from './return-error';
4+
5+
export class ReturnBook extends Command<Either<ReturnError, void>> {
6+
constructor(public readonly isbn: string) {
7+
super();
8+
}
9+
}

0 commit comments

Comments
 (0)