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

AddEventAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 29
dl 0
loc 31
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A index 0 21 2
1
import {
2
  Body,
3
  Post,
4
  Controller,
5
  Inject,
6
  BadRequestException,
7
  UseGuards
8
} from '@nestjs/common';
9
import {AuthGuard} from '@nestjs/passport';
10
import {ApiUseTags, ApiBearerAuth, ApiOperation} from '@nestjs/swagger';
11
import {ICommandBus} from 'src/Application/ICommandBus';
12
import {LoggedUser} from 'src/Infrastructure/User/Decorator/LoggedUser';
13
import {User} from 'src/Domain/User/User.entity';
14
import {AddEventCommand} from 'src/Application/FairCalendar/Command/AddEventCommand';
15
import {EventDTO} from './DTO/EventDTO';
16
17
@Controller('events')
18
@ApiUseTags('Event')
19
@ApiBearerAuth()
20
@UseGuards(AuthGuard('bearer'))
21
export class AddEventAction {
22
  constructor(
23
    @Inject('ICommandBus')
24
    private readonly commandBus: ICommandBus
25
  ) {}
26
27
  @Post()
28
  @ApiOperation({title: 'Add new event'})
29
  public async index(@Body() dto: EventDTO, @LoggedUser() user: User) {
30
    try {
31
      const {type, date, projectId, taskId, summary, time} = dto;
32
      const id = await this.commandBus.execute(
33
        new AddEventCommand(
34
          type,
35
          user,
36
          Number(time),
37
          new Date(date),
38
          projectId,
39
          taskId,
40
          summary
41
        )
42
      );
43
44
      return {id};
45
    } catch (e) {
46
      throw new BadRequestException(e.message);
47
    }
48
  }
49
}
50