Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 56 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { |
||
2 | Controller, |
||
3 | Inject, |
||
4 | Post, |
||
5 | Body, |
||
6 | BadRequestException, |
||
7 | UseGuards |
||
8 | } from '@nestjs/common'; |
||
9 | import { AuthGuard } from '@nestjs/passport'; |
||
10 | import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'; |
||
11 | import { ICommandBus } from 'src/Application/ICommandBus'; |
||
12 | import { CreateUserCommand } from 'src/Application/User/Command/CreateUserCommand'; |
||
13 | import { UserView } from 'src/Application/User/View/UserView'; |
||
14 | import { UserDTO } from '../DTO/UserDTO'; |
||
15 | import { IQueryBus } from 'src/Application/IQueryBus'; |
||
16 | import { GetUserByIdQuery } from 'src/Application/User/Query/GetUserByIdQuery'; |
||
17 | import { Roles } from '../Decorator/Roles'; |
||
18 | import { RolesGuard } from '../Security/RolesGuard'; |
||
19 | import { UserRole } from 'src/Domain/User/User.entity'; |
||
20 | |||
21 | @Controller('users') |
||
22 | @ApiTags('User') |
||
23 | @ApiBearerAuth() |
||
24 | @UseGuards(AuthGuard('bearer'), RolesGuard) |
||
25 | export class CreateUserAction { |
||
26 | constructor( |
||
27 | @Inject('ICommandBus') |
||
28 | private readonly commandBus: ICommandBus, |
||
29 | @Inject('IQueryBus') |
||
30 | private readonly queryBus: IQueryBus |
||
31 | ) {} |
||
32 | |||
33 | @Post() |
||
34 | @Roles(UserRole.PHOTOGRAPHER) |
||
35 | @ApiOperation({summary: 'Create new user'}) |
||
36 | public async index(@Body() userDto: UserDTO): Promise<UserView> { |
||
37 | try { |
||
38 | const { firstName, lastName, email, password, role } = userDto; |
||
39 | |||
40 | const id = await this.commandBus.execute( |
||
41 | new CreateUserCommand( |
||
42 | firstName, |
||
43 | lastName, |
||
44 | email, |
||
45 | password, |
||
46 | role |
||
47 | ) |
||
48 | ); |
||
49 | |||
50 | return await this.queryBus.execute(new GetUserByIdQuery(id)); |
||
51 | } catch (e) { |
||
52 | throw new BadRequestException(e.message); |
||
53 | } |
||
54 | } |
||
55 | } |
||
56 |