src/Infrastructure/Task/Controller/EditTaskController.ts   A
last analyzed

Complexity

Total Complexity 3
Complexity/F 1.5

Size

Lines of Code 63
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 57
mnd 1
bc 1
fnc 2
dl 0
loc 63
bpm 0.5
cpm 1.5
noi 0
c 0
b 0
f 0
rs 10

2 Functions

Rating   Name   Duplication   Size   Complexity  
A EditTaskController.get 0 9 1
A EditTaskController.post 0 15 2
1
import {
2
  BadRequestException,
3
  Body,
4
  Controller,
5
  Get,
6
  Inject,
7
  Param,
8
  Post,
9
  Render,
10
  Res,
11
  UseGuards
12
} from '@nestjs/common';
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 { IQueryBus } from 'src/Application/IQueryBus';
17
import { Response } from 'express';
18
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO';
19
import { TaskDTO } from '../DTO/TaskDTO';
20
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver';
21
import { GetTaskByIdQuery } from 'src/Application/Task/Query/GetTaskByIdQuery';
22
import { UpdateTaskCommand } from 'src/Application/Task/Command/UpdateTaskCommand';
23
24
@Controller('app/tasks/edit')
25
@UseGuards(IsAuthenticatedGuard)
26
export class EditTaskController {
27
  constructor(
28
    @Inject('ICommandBus')
29
    private readonly commandBus: ICommandBus,
30
    @Inject('IQueryBus')
31
    private readonly queryBus: IQueryBus,
32
    private readonly resolver: RouteNameResolver
33
  ) {}
34
35
  @Get(':id')
36
  @WithName('crm_tasks_edit')
37
  @Render('pages/tasks/edit.njk')
38
  public async get(@Param() idDto: IdDTO) {
39
    const task = await this.queryBus.execute(new GetTaskByIdQuery(idDto.id));
40
41
    return {
42
      task
43
    };
44
  }
45
46
  @Post(':id')
47
  public async post(
48
    @Param() idDto: IdDTO,
49
    @Body() dto: TaskDTO,
50
    @Res() res: Response
51
  ) {
52
    const { name } = dto;
53
54
    try {
55
      await this.commandBus.execute(new UpdateTaskCommand(idDto.id, name));
56
57
      res.redirect(303, this.resolver.resolve('crm_tasks_list'));
58
    } catch (e) {
59
      throw new BadRequestException(e.message);
60
    }
61
  }
62
}
63