Skip to content

Commit

Permalink
[Fix] 챕터 모두 조회 메서드 N+1 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
NARUBROWN committed Nov 29, 2024
1 parent 4aa776a commit 599b8b2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

import com.codingland.domain.chapter.entity.Chapter;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface ChapterRepository extends JpaRepository<Chapter, Long> {
@Query("""
SELECT DISTINCT c FROM Chapter c
LEFT JOIN FETCH c.quizzes
""")
List<Chapter> findAllWithQuizzes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Optional;

public interface IsChapterClearedRepository extends JpaRepository<IsChapterCleared, Long> {
List<IsChapterCleared> findAllByUserAndChapterIn(User user, List<Chapter> chapters);
List<IsChapterCleared> findByUser(User user);
Optional<IsChapterCleared> findByChapterAndUser(Chapter chapter, User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,33 @@ public ResponseChapterDto getChapter(Long chapter_id, Long user_id) {
*/
@Transactional(readOnly = true)
public ResponseChapterListDto getChapterList(Long user_id) {
List<Chapter> foundChapterList = chapterRepository.findAll();
List<Chapter> foundChapterList = chapterRepository.findAllWithQuizzes();
User foundUser = userRepository.findById(user_id)
.orElseThrow(() -> new UserException(UserErrorCode.No_USER_INFO));
List<IsChapterCleared> isChapterClearedList = isChapterClearedRepository.findAllByUserAndChapterIn(foundUser, foundChapterList);
Map<Long, IsChapterCleared> isChapterClearedMap = new HashMap<>();
for (IsChapterCleared isChapterCleared : isChapterClearedList) {
isChapterClearedMap.put(isChapterCleared.getChapter().getId(), isChapterCleared);
}
List<ResponseChapterDto> responseChapterDtoList = new ArrayList<>();
List<Quiz> allQuizzes = new ArrayList<>();
for (Chapter chapter : foundChapterList) {
allQuizzes.addAll(chapter.getQuizzes());
}
List<IsQuizCleared> isQuizClearedList = isQuizClearedRepository.findAllByUserAndQuizIn(foundUser, allQuizzes);
Map<Long, IsQuizCleared> isQuizClearedMap = new HashMap<>();
for (IsQuizCleared isQuizCleared : isQuizClearedList) {
isQuizClearedMap.put(isQuizCleared.getQuiz().getId(), isQuizCleared);
}
for (Chapter chapter : foundChapterList) {
IsChapterCleared foundIsChapterCleared = isChapterClearedRepository.findByChapterAndUser(chapter, foundUser)
.orElse(null);
IsChapterCleared isChapterCleared = isChapterClearedMap.get(chapter.getId());
List<ResponseFindByChapter> responseFindByChapterList = new ArrayList<>();
if (!chapter.getQuizzes().isEmpty()) {
for (Quiz quiz : chapter.getQuizzes()) {
IsQuizCleared foundIsQuizCleared = isQuizClearedRepository.findByQuizAndUser(quiz, foundUser)
.orElse(null);
IsQuizCleared isQuizCleared = isQuizClearedMap.get(quiz.getId());
responseFindByChapterList.add(
ResponseFindByChapter.builder()
.isCleared(foundIsQuizCleared != null && foundIsQuizCleared.isCleared())
.isCleared(isQuizCleared != null && isQuizCleared.isCleared())
.quizId(quiz.getId())
.level(quiz.getDifficulty().getLevel())
.title(quiz.getTitle())
Expand All @@ -157,7 +169,7 @@ public ResponseChapterListDto getChapterList(Long user_id) {
ResponseChapterDto.builder()
.id(chapter.getId())
.name(chapter.getName())
.isCleared(foundIsChapterCleared != null && foundIsChapterCleared.isCleared())
.isCleared(isChapterCleared != null && isChapterCleared.isCleared())
.quizzes(responseFindByChapterList)
.build()
);
Expand Down

0 comments on commit 599b8b2

Please sign in to comment.