Passed
Pull Request — master (#75)
by Mathieu
01:46
created

server/src/Application/FairCalendar/Query/GetEventByIdQueryHandler.ts   A

Complexity

Total Complexity 4
Complexity/F 4

Size

Lines of Code 37
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 32
mnd 3
bc 3
fnc 1
dl 0
loc 37
rs 10
bpm 3
cpm 4
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A GetEventByIdQueryHandler.execute 0 18 4
1
import {QueryHandler} from '@nestjs/cqrs';
2
import {Inject} from '@nestjs/common';
3
import {GetEventByIdQuery} from './GetEventByIdQuery';
4
import {IEventRepository} from 'src/Domain/FairCalendar/Repository/IEventRepository';
5
import {EventView} from '../View/EventView';
6
import {EventNotFoundException} from 'src/Domain/FairCalendar/Exception/EventNotFoundException';
7
import {ProjectView} from 'src/Application/Project/View/ProjectView';
8
import {TaskView} from 'src/Application/Task/View/TaskView';
9
10
@QueryHandler(GetEventByIdQuery)
11
export class GetEventByIdQueryHandler {
12
  constructor(
13
    @Inject('IEventRepository')
14
    private readonly eventRepository: IEventRepository
15
  ) {}
16
17
  public async execute(query: GetEventByIdQuery): Promise<EventView> {
18
    const event = await this.eventRepository.findOneById(query.id);
19
    if (!event) {
20
      throw new EventNotFoundException();
21
    }
22
23
    const project = event.getProject();
24
    const task = event.getTask();
25
26
    return new EventView(
27
      event.getId(),
28
      event.getType(),
29
      event.getTime(),
30
      event.getDate(),
31
      event.getSummary(),
32
      project ? new ProjectView(project.getId(), project.getName()) : null,
33
      task ? new TaskView(task.getId(), task.getName()) : null
34
    );
35
  }
36
}
37