Skip to content

Commit

Permalink
refactor: 불필요한 Fetch 제거 및 메서드 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
nuyh99 committed Aug 17, 2023
1 parent 3767589 commit 865c438
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static wooteco.prolog.common.exception.BadRequestCode.CURRICULUM_NOT_FOUND_EXCEPTION;

@RequiredArgsConstructor
Expand All @@ -40,37 +42,43 @@ public KeywordsResponse findAllKeywordsWithProgress(final Long curriculumId, fin
final Curriculum curriculum = curriculumRepository.findById(curriculumId)
.orElseThrow(() -> new BadRequestException(CURRICULUM_NOT_FOUND_EXCEPTION));

final Set<Long> sessionIds = sessionRepository.findAllByCurriculumId(curriculum.getId())
.stream()
.map(Session::getId)
.collect(Collectors.toSet());
final List<Keyword> keywordsInCurriculum = getKeywordsInCurriculum(curriculum);

final List<Keyword> keywords = keywordRepository.findBySessionIdIn(sessionIds);
final Map<Keyword, Set<Quiz>> quizzesInKeywords = quizRepository.findAll().stream()
.collect(groupingBy(Quiz::getKeyword, toSet()));

return createResponsesWithProgress(keywordsInCurriculum, quizzesInKeywords, getDoneQuizzes(memberId));
}

final Set<Quiz> doneQuizzes = essayAnswerRepository.findAllByMemberId(memberId).stream()
private Set<Quiz> getDoneQuizzes(final Long memberId) {
return essayAnswerRepository.findAllByMemberId(memberId).stream()
.map(EssayAnswer::getQuiz)
.collect(Collectors.toSet());
.collect(toSet());
}

final Map<Keyword, Set<Quiz>> quizzesPerKeyword = quizRepository.findAll().stream()
.collect(Collectors.groupingBy(Quiz::getKeyword, Collectors.toSet()));
private List<Keyword> getKeywordsInCurriculum(final Curriculum curriculum) {
final Set<Long> sessionIds = sessionRepository.findAllByCurriculumId(curriculum.getId())
.stream()
.map(Session::getId)
.collect(toSet());

return createWithProgress(keywords, quizzesPerKeyword, doneQuizzes);
return keywordRepository.findBySessionIdIn(sessionIds);
}

private KeywordsResponse createWithProgress(final List<Keyword> keywords,
final Map<Keyword, Set<Quiz>> quizzesPerKeyword,
final Set<Quiz> doneQuizzes) {
private KeywordsResponse createResponsesWithProgress(final List<Keyword> keywords,
final Map<Keyword, Set<Quiz>> quizzesPerKeyword,
final Set<Quiz> doneQuizzes) {
final List<KeywordResponse> keywordResponses = keywords.stream()
.filter(Keyword::isRoot)
.map(keyword -> createWithProgress(keyword, quizzesPerKeyword, doneQuizzes))
.collect(Collectors.toList());
.map(keyword -> createResponseWithProgress(keyword, quizzesPerKeyword, doneQuizzes))
.collect(toList());

return new KeywordsResponse(keywordResponses);
}

private KeywordResponse createWithProgress(final Keyword keyword,
final Map<Keyword, Set<Quiz>> quizzesPerKeyword,
final Set<Quiz> doneQuizzes) {
private KeywordResponse createResponseWithProgress(final Keyword keyword,
final Map<Keyword, Set<Quiz>> quizzesPerKeyword,
final Set<Quiz> doneQuizzes) {
final int totalQuizCount = quizzesPerKeyword.get(keyword).size();
final Set<Quiz> quizzes = quizzesPerKeyword.get(keyword);
quizzes.retainAll(doneQuizzes);
Expand All @@ -93,7 +101,7 @@ private Set<KeywordResponse> createChildrenWithProgress(final Set<Keyword> child
final Map<Keyword, Set<Quiz>> quizzesPerKeyword,
final Set<Quiz> userAnswers) {
return children.stream()
.map(child -> createWithProgress(child, quizzesPerKeyword, userAnswers))
.collect(Collectors.toSet());
.map(child -> createResponseWithProgress(child, quizzesPerKeyword, userAnswers))
.collect(toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public interface EssayAnswerRepository extends JpaRepository<EssayAnswer, Long>,

@Query("SELECT e FROM EssayAnswer e " +
"LEFT JOIN FETCH e.quiz q " +
"LEFT JOIN FETCH q.keyword " +
"WHERE e.member.id = :memberId")
Set<EssayAnswer> findAllByMemberId(@Param("memberId") Long memberId);
}

0 comments on commit 865c438

Please sign in to comment.