| Total Complexity | 3 |
| Complexity/F | 1.5 |
| Lines of Code | 51 |
| 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 { 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 |