Skip to content
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
merged 4 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
@Slf4j
public class InvalidTextAnswerLengthException extends BadRequestException {

public InvalidTextAnswerLengthException(int answerLength, int minLength, int maxLength) {
public InvalidTextAnswerLengthException(long questionId, int answerLength, int minLength, int maxLength) {
super("답변의 길이는 %d자 이상 %d자 이하여야 해요.".formatted(minLength, maxLength));
log.warn("AnswerLength is out of bound - answerLength: {}, minLength: {}, maxLength: {}",
answerLength, minLength, maxLength, this);
log.warn("AnswerLength is out of bound - questionId: {}, answerLength: {}, minLength: {}, maxLength: {}",
questionId, answerLength, minLength, maxLength, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Comment on lines +31 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 좋네요 이 함수 가독성 짱짱~

throw new InvalidTextAnswerLengthException(question.getId(), answerLength, ZERO_LENGTH, MAX_LENGTH);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

로그에서만 사용되는 것이라면 Exception(QuestionId, length, maxLength) 생성자를 만들어 min이 0일 때로 사용하면 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

조하요 반영했습니다~

}
}
}