-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgift-card.controller.ts
97 lines (91 loc) · 2.61 KB
/
gift-card.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
Logger,
HttpException,
HttpStatus,
} from '@nestjs/common';
import {
CancelGiftCardCommand,
IssueGiftCardCommand,
RedeemGiftCardCommand,
} from '../api/gift-card.commands';
import { GiftCardEvent } from '../api/gift-card.events';
import { GiftCardSummary } from '../query/gift-card.event-handler';
import { GiftCardCommandGateway } from '../command/gift-card.command-gateway';
import { GiftCardQueryGateway } from '../query/gift-card.query-gateway';
/**
* *** ADAPTER LAYER ***
* ___
* A controller - the entry point for all gift card commands/requests and queries, facing the users of the gift card service
*/
@Controller('gift-card')
export class GiftCardController {
private readonly logger = new Logger(GiftCardController.name);
constructor(
private readonly giftCardCommandGateway: GiftCardCommandGateway,
private readonly giftCardQueryGateway: GiftCardQueryGateway,
) {}
@Post()
async issue(
@Body() request: IssueGiftCardCommand,
): Promise<readonly [GiftCardEvent, number][]> {
this.logger.log(
`issuing gift card ${request.id} with amount ${request.amount}`,
);
try {
return await this.giftCardCommandGateway.publishCommand(request);
} catch (e) {
throw new HttpException(e, HttpStatus.BAD_REQUEST);
}
}
@Patch('redeem')
async redeem(
@Body() request: RedeemGiftCardCommand,
): Promise<readonly [GiftCardEvent, number][]> {
this.logger.log(
`redeeming gift card ${request.id} with amount ${request.amount}`,
);
try {
return await this.giftCardCommandGateway.publishCommand(request);
} catch (e) {
throw new HttpException(e, HttpStatus.BAD_REQUEST);
}
}
@Delete(':id')
async cancel(
@Param('id') id: string,
): Promise<readonly [GiftCardEvent, number][]> {
const command: CancelGiftCardCommand = {
kind: 'CancelGiftCardCommand',
id: id,
};
this.logger.log(`canceling gift card ${command.id}`);
try {
return await this.giftCardCommandGateway.publishCommand(command);
} catch (e) {
throw new HttpException(e, HttpStatus.BAD_REQUEST);
}
}
@Get()
async find(): Promise<readonly GiftCardSummary[] | GiftCardSummary | null> {
return await this.giftCardQueryGateway.publishQuery(
{ kind: 'FindAllQuery' },
'single',
);
}
@Get(':id')
async findOne(
@Param('id') id: string,
): Promise<readonly GiftCardSummary[] | GiftCardSummary | null> {
return await this.giftCardQueryGateway.publishQuery(
{ kind: 'FindByIdQuery', id: id },
'single',
);
}
}