| Total Complexity | 3 |
| Complexity/F | 3 |
| Lines of Code | 32 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import { CommandHandler } from '@nestjs/cqrs'; |
||
| 2 | import { Inject } from '@nestjs/common'; |
||
| 3 | import { IEventRepository } from 'src/Domain/FairCalendar/Repository/IEventRepository'; |
||
| 4 | import { DeleteEventCommand } from './DeleteEventCommand'; |
||
| 5 | import { EventDoesntBelongToUserException } from 'src/Domain/FairCalendar/Exception/EventDoesntBelongToUserException'; |
||
| 6 | import { EventNotFoundException } from 'src/Domain/FairCalendar/Exception/EventNotFoundException'; |
||
| 7 | import { DoesEventBelongToUser } from 'src/Domain/FairCalendar/Specification/DoesEventBelongToUser'; |
||
| 8 | |||
| 9 | @CommandHandler(DeleteEventCommand) |
||
| 10 | export class DeleteEventCommandHandler { |
||
| 11 | constructor( |
||
| 12 | @Inject('IEventRepository') |
||
| 13 | private readonly eventRepository: IEventRepository, |
||
| 14 | private readonly doesEventBelongToUser: DoesEventBelongToUser |
||
| 15 | ) {} |
||
| 16 | |||
| 17 | public async execute(command: DeleteEventCommand): Promise<void> { |
||
| 18 | const { id, user } = command; |
||
| 19 | const event = await this.eventRepository.findOneById(id); |
||
| 20 | |||
| 21 | if (!event) { |
||
| 22 | throw new EventNotFoundException(); |
||
| 23 | } |
||
| 24 | |||
| 25 | if (false === this.doesEventBelongToUser.isSatisfiedBy(event, user)) { |
||
| 26 | throw new EventDoesntBelongToUserException(); |
||
| 27 | } |
||
| 28 | |||
| 29 | await this.eventRepository.delete(event); |
||
| 30 | } |
||
| 31 | } |
||
| 32 |