-
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: 서비스 리팩터링 #540
Changes from 1 commit
1e6204b
d62a842
de03539
2fc94e1
7561c72
d3842d2
3cfe514
daecbdc
9cc53a9
b60807f
ebc281c
e42293c
357e9ef
bb153e5
646d8b7
20d6ffc
bd4b455
fd2f730
14e1819
76a01c7
08e5707
7e15fa3
8726e0c
cfe4fd9
c351eb2
4ff1bb4
f494164
b0e20bc
0d63f18
d93f943
6363b82
0062fb4
1359b10
8caa2b0
b90aa47
1f18e77
0071013
7022bcb
c280809
d9b4519
caa0716
eca9491
591f8cb
d3a615c
610b785
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package reviewme.review.service.module; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import reviewme.question.domain.Question; | ||
import reviewme.question.domain.QuestionType; | ||
import reviewme.question.repository.QuestionRepository; | ||
import reviewme.review.domain.CheckboxAnswer; | ||
import reviewme.review.domain.Review; | ||
import reviewme.review.domain.TextAnswer; | ||
import reviewme.review.domain.exception.ReviewGroupNotFoundByReviewRequestCodeException; | ||
import reviewme.review.service.dto.request.ReviewAnswerRequest; | ||
import reviewme.review.service.dto.request.ReviewRegisterRequest; | ||
import reviewme.review.service.exception.SubmittedQuestionNotFoundException; | ||
import reviewme.reviewgroup.domain.ReviewGroup; | ||
import reviewme.reviewgroup.repository.ReviewGroupRepository; | ||
import reviewme.template.domain.Template; | ||
import reviewme.template.domain.exception.TemplateNotFoundByReviewGroupException; | ||
import reviewme.template.repository.TemplateRepository; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ReviewMapper { | ||
|
||
private final ReviewGroupRepository reviewGroupRepository; | ||
private final TemplateRepository templateRepository; | ||
private final QuestionRepository questionRepository; | ||
|
||
public Review mapToReview(ReviewRegisterRequest request, | ||
TextAnswerValidator textAnswerValidator, | ||
CheckBoxAnswerValidator checkBoxAnswerValidator | ||
) { | ||
ReviewGroup reviewGroup = findReviewGroupByRequestCodeOrThrow(request.reviewRequestCode()); | ||
Template template = findTemplateByReviewGroupOrThrow(reviewGroup); | ||
|
||
|
||
List<TextAnswer> textAnswers = new ArrayList<>(); | ||
List<CheckboxAnswer> checkboxAnswers = new ArrayList<>(); | ||
for (ReviewAnswerRequest answerRequest : request.answers()) { | ||
Question question = questionRepository.findById(answerRequest.questionId()) | ||
.orElseThrow(() -> new SubmittedQuestionNotFoundException(answerRequest.questionId())); | ||
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. 저도 같은 생각입니다 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. 반영 완료👍 |
||
|
||
if (question.getQuestionType() == QuestionType.TEXT) { | ||
TextAnswer textAnswer = mapToTextAnswer(answerRequest); | ||
textAnswerValidator.validate(textAnswer); | ||
textAnswers.add(textAnswer); | ||
} | ||
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. textAnswer를 mapper로 생성함(null 검증은 mapper안에서 함) → 그 다음에 validator로 보내서 나머지 검증함 현재 검증이 1. mapper 2. validator 두 위치에 나눠져있는 부분이 고민됩니다. 현재는 textAnswer 객체를 생성하기 전에 해야하는 검증이 null만 있지만, private void validateNotIncludingOptions(CreateReviewAnswerRequest request) {
if (request.selectedOptionIds() != null) {
throw new TextAnswerIncludedOptionItemException();
}
}
private void validateQuestionRequired(Question question, CreateReviewAnswerRequest request) {
if (question.isRequired() && request.text() == null) {
throw new RequiredQuestionNotAnsweredException(question.getId());
}
} 객체 생성 전에 한 곳에서 검증을 싹 하고, 후에 객체를 생성하는 방식으로 풀어갈 수 있지 않을까요?(아직 안해봄.. 생각대로 될지는 해봐야합니다😂) 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. 같이 대화 나눈 내용 코멘트로 남겨놓겠습니다.
|
||
|
||
if (question.getQuestionType() == QuestionType.CHECKBOX) { | ||
CheckboxAnswer checkboxAnswer = mapToCheckboxAnswer(answerRequest); | ||
checkBoxAnswerValidator.validate(checkboxAnswer); | ||
checkboxAnswers.add(checkboxAnswer); | ||
} | ||
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. (개인 메모를 아래에 적어두겠습니다, 무시해 주세요)
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. 크크 이거 저랑 테드도 이야기했던 부분이네요. 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.
|
||
} | ||
|
||
return new Review(template.getId(), reviewGroup.getId(), textAnswers, checkboxAnswers); | ||
} | ||
|
||
private ReviewGroup findReviewGroupByRequestCodeOrThrow(String reviewRequestCode) { | ||
return reviewGroupRepository.findByReviewRequestCode(reviewRequestCode) | ||
.orElseThrow(() -> new ReviewGroupNotFoundByReviewRequestCodeException(reviewRequestCode)); | ||
} | ||
|
||
private Template findTemplateByReviewGroupOrThrow(ReviewGroup reviewGroup) { | ||
return templateRepository.findById(reviewGroup.getTemplateId()) | ||
.orElseThrow(() -> new TemplateNotFoundByReviewGroupException( | ||
reviewGroup.getId(), reviewGroup.getTemplateId())); | ||
} | ||
|
||
private TextAnswer mapToTextAnswer(ReviewAnswerRequest answerRequest) { | ||
return new TextAnswer(answerRequest.questionId(), answerRequest.text()); | ||
} | ||
|
||
private CheckboxAnswer mapToCheckboxAnswer(ReviewAnswerRequest answerRequest) { | ||
return new CheckboxAnswer(answerRequest.questionId(), answerRequest.selectedOptionIds()); | ||
} | ||
} |
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.
개행 두 줄이네요 👀
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.
하핫.. 반영 완료👍