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] Mapper에 있는 DB에서 일괄로 데이터 가져오는 로직 개선 #663

Open
skylar1220 opened this issue Sep 22, 2024 · 0 comments

Comments

@skylar1220
Copy link
Contributor

skylar1220 commented Sep 22, 2024

🔍 설명

  • Mapper에서 db 접근을 최소화하기위해 가장 상위 메서드에서 db에 접근해 데이터를 한꺼번에 가져오는 로직이 구현되어있습니다.
  • db에서 필요한 데이터 왕창 가져와서 선언해놓기라는 작업이 mapToReviewDetailResponse의 주요 로직과 섞여 가독성이 좋지 않고 헷갈립니다. 해결할 수 있는 방법을 고민해보려고 합니다~
public class ReviewDetailMapper {

    public ReviewDetailResponse mapToReviewDetailResponse(Review review, ReviewGroup reviewGroup) {
        long templateId = review.getTemplateId();

        // 이 부분은 다 db에서 필요한 데이터 가져오기 부분
        List<Section> sections = sectionRepository.findAllByTemplateId(templateId);
        List<Question> questions = questionRepository.findAllByTemplatedId(templateId);
        List<Long> questionIds = questions.stream()
                .map(Question::getId)
                .toList();
        Map<Long, OptionGroup> optionGroupsByQuestion = optionGroupRepository.findAllByQuestionIds(questionIds)
                .stream()
                .collect(Collectors.toMap(OptionGroup::getQuestionId, optionGroup -> optionGroup));
        Map<Long, List<OptionItem>> optionItemsByOptionGroup = optionItemRepository.findAllByQuestionIds(questionIds)
                .stream()
                .collect(Collectors.groupingBy(OptionItem::getOptionGroupId));

        // 이 메서드의 진짜 역할은 여기
        List<SectionAnswerResponse> sectionResponses = sections.stream()
                .map(section -> mapToSectionResponse(review, reviewGroup, section, questions,
                        optionGroupsByQuestion, optionItemsByOptionGroup))
                .filter(sectionResponse -> !sectionResponse.questions().isEmpty())
                .toList();

        return new ReviewDetailResponse(
                templateId,
                reviewGroup.getReviewee(),
                reviewGroup.getProjectName(),
                review.getCreatedDate(),
                sectionResponses
        );
    }

🔥 할 일

  • 분리 방법 고민하기

⏰ 예상 시간

  • 1~2h? 고민에 따라 달라질듯ㅎㅎ

🐴 할 말

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

1 participant