forked from Sairyss/domain-driven-hexagon
-
Notifications
You must be signed in to change notification settings - Fork 3
/
create-user.http.controller.ts
56 lines (52 loc) · 1.8 KB
/
create-user.http.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
import {
Body,
ConflictException as ConflictHttpException,
Controller,
HttpStatus,
Post,
} from '@nestjs/common';
import { routesV1 } from '@config/app.routes';
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
import { CommandBus } from '@nestjs/cqrs';
import { match, Result } from 'oxide.ts';
import { CreateUserCommand } from './create-user.command';
import { CreateUserRequestDto } from './create-user.request.dto';
import { UserAlreadyExistsError } from '@modules/user/domain/user.errors';
import { IdResponse } from '@libs/api/id.response.dto';
import { AggregateID } from '@libs/ddd';
import { ApiErrorResponse } from '@src/libs/api/api-error.response';
@Controller(routesV1.version)
export class CreateUserHttpController {
constructor(private readonly commandBus: CommandBus) {}
@ApiOperation({ summary: 'Create a user' })
@ApiResponse({
status: HttpStatus.OK,
type: IdResponse,
})
@ApiResponse({
status: HttpStatus.CONFLICT,
description: UserAlreadyExistsError.message,
type: ApiErrorResponse,
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
type: ApiErrorResponse,
})
@Post(routesV1.user.root)
async create(@Body() body: CreateUserRequestDto): Promise<IdResponse> {
const command = new CreateUserCommand(body);
const result: Result<AggregateID, UserAlreadyExistsError> =
await this.commandBus.execute(command);
// Deciding what to do with a Result (similar to Rust matching)
// if Ok we return a response with an id
// if Error decide what to do with it depending on its type
return match(result, {
Ok: (id: string) => new IdResponse(id),
Err: (error: Error) => {
if (error instanceof UserAlreadyExistsError)
throw new ConflictHttpException(error.message);
throw error;
},
});
}
}