Skip to content

Commit

Permalink
refactor: Template으로부터 중간 데이터 뽑아오도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
donghoony committed Jan 5, 2025
1 parent d6b65dc commit 5d32c50
Showing 1 changed file with 23 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import reviewme.template.domain.OptionGroup;
import reviewme.template.domain.OptionItem;
import reviewme.template.domain.Question;
import reviewme.template.repository.OptionGroupRepository;
import reviewme.template.repository.OptionItemRepository;
import reviewme.template.repository.QuestionRepository;
import reviewme.review.domain.CheckboxAnswer;
import reviewme.review.domain.CheckboxAnswerSelectedOption;
import reviewme.review.domain.Review;
Expand All @@ -23,32 +16,38 @@
import reviewme.review.service.dto.response.detail.ReviewDetailResponse;
import reviewme.review.service.dto.response.detail.SectionAnswerResponse;
import reviewme.reviewgroup.domain.ReviewGroup;
import reviewme.template.domain.OptionGroup;
import reviewme.template.domain.OptionItem;
import reviewme.template.domain.Question;
import reviewme.template.domain.Section;
import reviewme.template.repository.SectionRepository;
import reviewme.template.domain.Template;
import reviewme.template.repository.TemplateRepository;
import reviewme.template.service.exception.TemplateNotFoundByReviewGroupException;

@Component
@RequiredArgsConstructor
public class ReviewDetailMapper {

private final SectionRepository sectionRepository;
private final QuestionRepository questionRepository;
private final OptionGroupRepository optionGroupRepository;
private final OptionItemRepository optionItemRepository;
private final TemplateRepository templateRepository;

/*
TODO:
조회 전용 로직을 만드는 게 좋겠다, Template + 리뷰 정보를 한 번에 내려줘야 한다.
Template에서 정보를 가져오는 건 쉽다 (연관관계 있음), 리뷰 관련 정보를 가져와서 어떻게 섞을지 고민하자.
*/
public ReviewDetailResponse mapToReviewDetailResponse(Review review, ReviewGroup reviewGroup) {
long templateId = review.getTemplateId();
Template template = templateRepository.findById(reviewGroup.getTemplateId())
.orElseThrow(() -> new TemplateNotFoundByReviewGroupException(reviewGroup.getId(),
reviewGroup.getTemplateId()));

List<Section> sections = sectionRepository.findAllByTemplateId(templateId);
List<Question> questions = questionRepository.findAllByTemplatedId(templateId);
List<Long> questionIds = questions.stream()
.map(Question::getId)
List<Section> sections = template.getSections();
List<Question> questions = sections.stream()
.flatMap(section -> section.getQuestions().stream())
.toList();
Map<Long, OptionGroup> optionGroupsByQuestion = optionGroupRepository.findAllByQuestionIds(questionIds)
.stream()
.collect(Collectors.toMap(OptionGroup::getQuestionId, Function.identity()));
Map<Long, List<OptionItem>> optionItemsByOptionGroup = optionItemRepository.findAllByQuestionIds(questionIds)
.stream()
.collect(Collectors.groupingBy(OptionItem::getOptionGroupId));
Map<Long, OptionGroup> optionGroupsByQuestion = questions.stream()
.collect(Collectors.toMap(Question::getId, Question::getOptionGroup));
Map<Long, List<OptionItem>> optionItemsByOptionGroup = optionGroupsByQuestion.values().stream()
.collect(Collectors.toMap(OptionGroup::getId, OptionGroup::getOptionItems));

List<SectionAnswerResponse> sectionResponses = sections.stream()
.map(section -> mapToSectionResponse(review, section, questions,
Expand All @@ -57,7 +56,7 @@ public ReviewDetailResponse mapToReviewDetailResponse(Review review, ReviewGroup
.toList();

return new ReviewDetailResponse(
templateId,
template.getId(),
reviewGroup.getReviewee(),
reviewGroup.getProjectName(),
review.getCreatedDate(),
Expand Down

0 comments on commit 5d32c50

Please sign in to comment.