-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from mapeveri/feat/112-add-validations
Added validations #112
- Loading branch information
Showing
4 changed files
with
131 additions
and
6 deletions.
There are no files selected for viewing
26 changes: 25 additions & 1 deletion
26
src/languages/application/term/command/create/addLikeTermCommandHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,33 @@ | ||
import { CommandHandler, ICommandHandler } from '@src/shared/domain/bus/commandBus/commandHandler'; | ||
import AddLikeTermCommand from '@src/languages/application/term/command/create/addLikeTermCommand'; | ||
import TermId from '@src/languages/domain/term/termId'; | ||
import UserId from '@src/languages/domain/user/userId'; | ||
import TermType from '@src/languages/domain/term/termType'; | ||
import { Inject } from '@src/shared/domain/injector/inject.decorator'; | ||
import TermRepository, { TERM_REPOSITORY } from '@src/languages/domain/term/termRepository'; | ||
import Term from '@src/languages/domain/term/term'; | ||
import TermDoesNotExistsException from '@src/languages/domain/term/termDoesNotExistsException'; | ||
|
||
@CommandHandler(AddLikeTermCommand) | ||
export default class AddLikeTermCommandHandler implements ICommandHandler<AddLikeTermCommand> { | ||
execute(_command: AddLikeTermCommand): Promise<any> { | ||
constructor(@Inject(TERM_REPOSITORY) private readonly termRepository: TermRepository) {} | ||
|
||
async execute(command: AddLikeTermCommand): Promise<any> { | ||
const termId = TermId.of(command.termId); | ||
TermType.of(command.type); | ||
UserId.of(command.userId); | ||
|
||
await this.getTerm(termId); | ||
|
||
return Promise.resolve(undefined); | ||
} | ||
|
||
private async getTerm(termId: TermId): Promise<Term> { | ||
const term = await this.termRepository.findById(termId); | ||
if (!term) { | ||
throw new TermDoesNotExistsException(termId.toString()); | ||
} | ||
|
||
return term; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import NotFoundException from '@src/shared/domain/exceptions/notFoundException'; | ||
|
||
export default class TermDoesNotExistsException extends NotFoundException { | ||
constructor(termId: string) { | ||
super(`Term ${termId} does not exists`, 'term_does_not_exists'); | ||
} | ||
} |
83 changes: 78 additions & 5 deletions
83
test/unit/languages/application/term/command/create/addLikeTermCommandHandler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
test/unit/languages/application/term/command/create/addLikeTermCommandMother.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import faker from 'faker'; | ||
import AddLikeTermCommand from '@src/languages/application/term/command/create/addLikeTermCommand'; | ||
import { TermTypeEnum } from '@src/languages/domain/term/termType'; | ||
|
||
interface AddLikeTermCommandProps { | ||
termId?: string; | ||
type?: string; | ||
userId?: string; | ||
} | ||
|
||
export class AddLikeTermCommandMother { | ||
static random(props?: AddLikeTermCommandProps): AddLikeTermCommand { | ||
const { termId, type, userId } = props ?? {}; | ||
|
||
return new AddLikeTermCommand( | ||
termId ?? faker.datatype.uuid(), | ||
type ?? TermTypeEnum.WORD, | ||
userId ?? faker.datatype.uuid(), | ||
); | ||
} | ||
} |