Passed
Pull Request — master (#216)
by
unknown
01:43
created

server/src/Application/Contact/Query/GetContactByIdQueryHandler.ts   A

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 33
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 2
mnd 1
bc 1
fnc 1
bpm 1
cpm 2
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A GetContactByIdQueryHandler.execute 0 16 2
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