Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 37 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import {QueryHandler} from '@nestjs/cqrs'; |
||
2 | import {Inject} from '@nestjs/common'; |
||
3 | import {GetCustomerByIdQuery} from './GetCustomerByIdQuery'; |
||
4 | import {ICustomerRepository} from 'src/Domain/Customer/Repository/ICustomerRepository'; |
||
5 | import {CustomerView} from '../View/CustomerView'; |
||
6 | import {CustomerNotFoundException} from 'src/Domain/Customer/Exception/CustomerNotFoundException'; |
||
7 | import {AddressView} from '../View/AddressView'; |
||
8 | |||
9 | @QueryHandler(GetCustomerByIdQuery) |
||
10 | export class GetCustomerByIdQueryHandler { |
||
11 | constructor( |
||
12 | @Inject('ICustomerRepository') |
||
13 | private readonly customerRepository: ICustomerRepository |
||
14 | ) {} |
||
15 | |||
16 | public async execute(query: GetCustomerByIdQuery): Promise<CustomerView> { |
||
17 | const customer = await this.customerRepository.findOneById(query.id); |
||
18 | if (!customer) { |
||
19 | throw new CustomerNotFoundException(); |
||
20 | } |
||
21 | |||
22 | const address = customer.getAddress(); |
||
23 | |||
24 | return new CustomerView( |
||
25 | customer.getId(), |
||
26 | customer.getName(), |
||
27 | new AddressView( |
||
28 | address.getId(), |
||
29 | address.getStreet(), |
||
30 | address.getCity(), |
||
31 | address.getZipCode(), |
||
32 | address.getCountry() |
||
33 | ) |
||
34 | ); |
||
35 | } |
||
36 | } |
||
37 |