Passed
Pull Request — master (#406)
by
unknown
01:44
created

AddCustomerController.get   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 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 { CreateCustomerCommand } from 'src/Application/Customer/Command/CreateCustomerCommand';
17
import { CustomerDTO } from '../DTO/CustomerDTO';
18
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver';
19
20
@Controller('app/customers/add')
21
@UseGuards(IsAuthenticatedGuard)
22
export class AddCustomerController {
23
  constructor(
24
    @Inject('ICommandBus')
25
    private readonly commandBus: ICommandBus,
26
    private readonly resolver: RouteNameResolver
27
  ) {}
28
29
  @Get()
30
  @WithName('crm_customers_add')
31
  @Render('pages/customers/add.njk')
32
  public async get() {
33
    return {};
34
  }
35
36
  @Post()
37
  public async post(@Body() customerDto: CustomerDTO, @Res() res: Response) {
38
    const { street, city, zipCode, country, name } = customerDto;
39
40
    try {
41
      await this.commandBus.execute(
42
        new CreateCustomerCommand(name, street, city, zipCode, country)
43
      );
44
45
      res.redirect(303, this.resolver.resolve('crm_customers_list'));
46
    } catch (e) {
47
      throw new BadRequestException(e.message);
48
    }
49
  }
50
}
51