Total Complexity | 3 |
Complexity/F | 1.5 |
Lines of Code | 49 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { |
||
2 | BadRequestException, |
||
3 | Body, |
||
4 | Controller, |
||
5 | Get, |
||
6 | Inject, |
||
7 | Post, |
||
8 | Render, |
||
9 | Res, |
||
10 | UseGuards |
||
11 | } from '@nestjs/common'; |
||
12 | import { Response } from 'express'; |
||
13 | import { ICommandBus } from 'src/Application/ICommandBus'; |
||
14 | import { IsAuthenticatedGuard } from 'src/Infrastructure/HumanResource/User/Security/IsAuthenticatedGuard'; |
||
15 | import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName'; |
||
16 | import { CreateTaskCommand } from 'src/Application/Task/Command/CreateTaskCommand'; |
||
17 | import { TaskDTO } from '../DTO/TaskDTO'; |
||
18 | import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver'; |
||
19 | |||
20 | @Controller('app/tasks/add') |
||
21 | @UseGuards(IsAuthenticatedGuard) |
||
22 | export class AddTaskController { |
||
23 | constructor( |
||
24 | @Inject('ICommandBus') |
||
25 | private readonly commandBus: ICommandBus, |
||
26 | private readonly resolver: RouteNameResolver |
||
27 | ) {} |
||
28 | |||
29 | @Get() |
||
30 | @WithName('crm_tasks_add') |
||
31 | @Render('pages/tasks/add.njk') |
||
32 | public async get() { |
||
33 | return {}; |
||
34 | } |
||
35 | |||
36 | @Post() |
||
37 | public async post(@Body() taskDto: TaskDTO, @Res() res: Response) { |
||
38 | const { name } = taskDto; |
||
39 | |||
40 | try { |
||
41 | await this.commandBus.execute(new CreateTaskCommand(name)); |
||
42 | |||
43 | res.redirect(303, this.resolver.resolve('crm_tasks_list')); |
||
44 | } catch (e) { |
||
45 | throw new BadRequestException(e.message); |
||
46 | } |
||
47 | } |
||
48 | } |
||
49 |