Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 29 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { Inject } from '@nestjs/common'; |
||
2 | import { CommandHandler } from '@nestjs/cqrs'; |
||
3 | import { CreateTaskCommand } from './CreateTaskCommand'; |
||
4 | import { ITaskRepository } from 'src/Domain/Task/Repository/ITaskRepository'; |
||
5 | import { IsTaskAlreadyExist } from 'src/Domain/Task/Specification/IsTaskAlreadyExist'; |
||
6 | import { TaskAlreadyExistException } from 'src/Domain/Task/Exception/TaskAlreadyExistException'; |
||
7 | import { Task } from 'src/Domain/Task/Task.entity'; |
||
8 | |||
9 | @CommandHandler(CreateTaskCommand) |
||
10 | export class CreateTaskCommandHandler { |
||
11 | constructor( |
||
12 | @Inject('ITaskRepository') |
||
13 | private readonly taskRepository: ITaskRepository, |
||
14 | private readonly isTaskAlreadyExist: IsTaskAlreadyExist |
||
15 | ) {} |
||
16 | |||
17 | public async execute(command: CreateTaskCommand): Promise<string> { |
||
18 | const { name } = command; |
||
19 | |||
20 | if (true === (await this.isTaskAlreadyExist.isSatisfiedBy(name))) { |
||
21 | throw new TaskAlreadyExistException(); |
||
22 | } |
||
23 | |||
24 | const task = await this.taskRepository.save(new Task(name)); |
||
25 | |||
26 | return task.getId(); |
||
27 | } |
||
28 | } |
||
29 |