| Total Complexity | 2 |
| Complexity/F | 2 |
| Lines of Code | 33 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import { QueryHandler } from '@nestjs/cqrs'; |
||
| 2 | import { Inject } from '@nestjs/common'; |
||
| 3 | import { GetContactByIdQuery } from './GetContactByIdQuery'; |
||
| 4 | import { IContactRepository } from 'src/Domain/Contact/Repository/IContactRepository'; |
||
| 5 | import { ContactView } from '../View/ContactView'; |
||
| 6 | import { ContactNotFoundException } from 'src/Domain/Contact/Exception/ContactNotFoundException'; |
||
| 7 | |||
| 8 | @QueryHandler(GetContactByIdQuery) |
||
| 9 | export class GetContactByIdQueryHandler { |
||
| 10 | constructor( |
||
| 11 | @Inject('IContactRepository') |
||
| 12 | private readonly contactRepository: IContactRepository |
||
| 13 | ) {} |
||
| 14 | |||
| 15 | public async execute(query: GetContactByIdQuery): Promise<ContactView> { |
||
| 16 | const contact = await this.contactRepository.findOneById(query.id); |
||
| 17 | |||
| 18 | if (!contact) { |
||
| 19 | throw new ContactNotFoundException(); |
||
| 20 | } |
||
| 21 | |||
| 22 | return new ContactView( |
||
| 23 | contact.getId(), |
||
| 24 | contact.getFirstName(), |
||
| 25 | contact.getLastName(), |
||
| 26 | contact.getCompany(), |
||
| 27 | contact.getEmail(), |
||
| 28 | contact.getPhoneNumber(), |
||
| 29 | contact.getNotes() |
||
| 30 | ); |
||
| 31 | } |
||
| 32 | } |
||
| 33 |