-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BE] refactor: 필수 질문이 아닌 경우 최소 글자수 제한 제거 #589
Merged
Kimprodp
merged 4 commits into
develop
from
be/refactor/453-optional-text-question-min-length
Sep 15, 2024
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6a29c0e
refactor: 필수 질문이 아닌 경우 최소 글자수 제한을 받지 않도록 변경
Kimprodp a4b385e
refactor: 필수 질문이 아닌 경우에 대한 검증 테스트 추가
Kimprodp 63da839
refactor: InvalidTextAnswerLengthException 에 min값 고정 생성자 추가
Kimprodp a74b3b3
refactor: InvalidTextAnswerLengthException 생성자 체이닝 적용
Kimprodp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import reviewme.question.domain.Question; | ||
import reviewme.question.repository.QuestionRepository; | ||
import reviewme.review.domain.TextAnswer; | ||
import reviewme.review.domain.exception.InvalidTextAnswerLengthException; | ||
|
@@ -11,26 +12,28 @@ | |
@RequiredArgsConstructor | ||
public class TextAnswerValidator { | ||
|
||
private static final int ZERO_LENGTH = 0; | ||
private static final int MIN_LENGTH = 20; | ||
private static final int MAX_LENGTH = 1_000; | ||
|
||
private final QuestionRepository questionRepository; | ||
|
||
public void validate(TextAnswer textAnswer) { | ||
validateExistQuestion(textAnswer); | ||
validateLength(textAnswer); | ||
} | ||
Question question = questionRepository.findById(textAnswer.getQuestionId()) | ||
.orElseThrow(() -> new SubmittedQuestionNotFoundException(textAnswer.getQuestionId())); | ||
|
||
private void validateExistQuestion(TextAnswer textAnswer) { | ||
if (!questionRepository.existsById(textAnswer.getQuestionId())) { | ||
throw new SubmittedQuestionNotFoundException(textAnswer.getQuestionId()); | ||
} | ||
validateLength(textAnswer, question); | ||
} | ||
|
||
private void validateLength(TextAnswer textAnswer) { | ||
private void validateLength(TextAnswer textAnswer, Question question) { | ||
int answerLength = textAnswer.getContent().length(); | ||
if (answerLength < MIN_LENGTH || answerLength > MAX_LENGTH) { | ||
throw new InvalidTextAnswerLengthException(answerLength, MIN_LENGTH, MAX_LENGTH); | ||
|
||
if (question.isRequired() && (answerLength < MIN_LENGTH || answerLength > MAX_LENGTH)) { | ||
throw new InvalidTextAnswerLengthException(question.getId(), answerLength, MIN_LENGTH, MAX_LENGTH); | ||
} | ||
|
||
if (!question.isRequired() && answerLength > MAX_LENGTH) { | ||
throw new InvalidTextAnswerLengthException(question.getId(), answerLength, ZERO_LENGTH, MAX_LENGTH); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로그에서만 사용되는 것이라면 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 조하요 반영했습니다~ |
||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 좋네요 이 함수 가독성 짱짱~