diff --git a/backend/src/main/java/wooteco/prolog/roadmap/domain/repository/KeywordRepository.java b/backend/src/main/java/wooteco/prolog/roadmap/domain/repository/KeywordRepository.java index db4207efe..8b5e412cd 100644 --- a/backend/src/main/java/wooteco/prolog/roadmap/domain/repository/KeywordRepository.java +++ b/backend/src/main/java/wooteco/prolog/roadmap/domain/repository/KeywordRepository.java @@ -1,35 +1,30 @@ package wooteco.prolog.roadmap.domain.repository; -import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import wooteco.prolog.roadmap.domain.Keyword; +import wooteco.prolog.roadmap.domain.repository.dto.KeywordIdAndDoneQuizCount; +import wooteco.prolog.roadmap.domain.repository.dto.KeywordIdAndTotalQuizCount; import java.util.List; -import java.util.Optional; -import java.util.Set; - -import static org.springframework.data.jpa.repository.EntityGraph.EntityGraphType.FETCH; public interface KeywordRepository extends JpaRepository { - @EntityGraph(attributePaths = "recommendedPosts", type = FETCH) - Optional findById(final Long id); + @Query("SELECT k.id AS keywordId, COUNT (q.id) as totalQuizCount " + + "FROM Keyword k " + + "JOIN Quiz q ON q.keyword.id = k.id " + + "GROUP BY k.id") + List findTotalQuizCount(); - @EntityGraph(attributePaths = "recommendedPosts", type = FETCH) - List findAll(); + @Query("SELECT k.id AS keywordId, COUNT (e.id) AS doneQuizCount " + + "FROM Keyword k " + + "JOIN Quiz q ON k.id = q.keyword.id " + + "JOIN EssayAnswer e ON e.quiz.id = q.id " + + "WHERE e.member.id = :memberId " + + "GROUP BY k.id ") + List findDoneQuizCountByMemberId(@Param("memberId") Long memberId); - @Query("SELECT k FROM Keyword k " - + "LEFT JOIN FETCH k.children c " - + "LEFT JOIN FETCH k.recommendedPosts " - + "LEFT JOIN FETCH k.parent p " - + "LEFT JOIN FETCH p.recommendedPosts " - + "LEFT JOIN FETCH c.recommendedPosts " - + "LEFT JOIN FETCH c.children lc " - + "LEFT JOIN FETCH lc.recommendedPosts " - + "LEFT JOIN FETCH lc.children " - + "WHERE k.id = :keywordId ORDER BY k.seq") Keyword findFetchByIdOrderBySeq(@Param("keywordId") Long keywordId); @Query("SELECT k FROM Keyword k " @@ -42,5 +37,9 @@ public interface KeywordRepository extends JpaRepository { + "WHERE k.parent IS NULL") List newFindByParentIsNull(); - List findBySessionIdIn(final Set sessionIds); + @Query("SELECT k FROM Keyword k " + + "JOIN Session s ON s.id = k.sessionId " + + "LEFT JOIN FETCH RecommendedPost r ON r.keyword.id = k.id " + + "WHERE s.curriculumId = :curriculumId ") + List findAllByCurriculumId(Long curriculumId); } diff --git a/backend/src/main/java/wooteco/prolog/roadmap/domain/repository/dto/KeywordIdAndDoneQuizCount.java b/backend/src/main/java/wooteco/prolog/roadmap/domain/repository/dto/KeywordIdAndDoneQuizCount.java new file mode 100644 index 000000000..d25aef114 --- /dev/null +++ b/backend/src/main/java/wooteco/prolog/roadmap/domain/repository/dto/KeywordIdAndDoneQuizCount.java @@ -0,0 +1,7 @@ +package wooteco.prolog.roadmap.domain.repository.dto; + +public interface KeywordIdAndDoneQuizCount { + long getKeywordId(); + + int getDoneQuizCount(); +} diff --git a/backend/src/main/java/wooteco/prolog/roadmap/domain/repository/dto/KeywordIdAndTotalQuizCount.java b/backend/src/main/java/wooteco/prolog/roadmap/domain/repository/dto/KeywordIdAndTotalQuizCount.java new file mode 100644 index 000000000..47836c550 --- /dev/null +++ b/backend/src/main/java/wooteco/prolog/roadmap/domain/repository/dto/KeywordIdAndTotalQuizCount.java @@ -0,0 +1,7 @@ +package wooteco.prolog.roadmap.domain.repository.dto; + +public interface KeywordIdAndTotalQuizCount { + long getKeywordId(); + + int getTotalQuizCount(); +} diff --git a/backend/src/test/java/wooteco/prolog/roadmap/repository/KeywordRepositoryTest.java b/backend/src/test/java/wooteco/prolog/roadmap/repository/KeywordRepositoryTest.java index 47c67b86b..104a08410 100644 --- a/backend/src/test/java/wooteco/prolog/roadmap/repository/KeywordRepositoryTest.java +++ b/backend/src/test/java/wooteco/prolog/roadmap/repository/KeywordRepositoryTest.java @@ -1,12 +1,5 @@ package wooteco.prolog.roadmap.repository; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertAll; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import javax.persistence.EntityManager; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -18,6 +11,13 @@ import wooteco.prolog.session.domain.repository.SessionRepository; import wooteco.support.utils.RepositoryTest; +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + @RepositoryTest class KeywordRepositoryTest { @@ -114,53 +114,6 @@ class KeywordRepositoryTest { assertThat(extractParentKeyword.size()).isEqualTo(1); } - @Test - @DisplayName("세션 ID 리스트로 키워드 리스트를 조회한다") - void findBySessionIdIn() { - //given - final Curriculum curriculum = curriculumRepository.save(new Curriculum("커리큘럼1")); - - final Session session1 = sessionRepository.save(new Session(curriculum.getId(), "세션1")); - final Session session2 = sessionRepository.save(new Session(curriculum.getId(), "세션2")); - final Session session3 = sessionRepository.save(new Session(curriculum.getId(), "세션3")); - final Session session4 = sessionRepository.save(new Session(curriculum.getId(), "세션4")); - final Session session5 = sessionRepository.save(new Session(curriculum.getId(), "세션5")); - - final Keyword keyword1 = keywordRepository.save( - Keyword.createKeyword("자바1", "자바 설명1", 1, 5, session1.getId(), null)); - final Keyword keyword2 = keywordRepository.save( - Keyword.createKeyword("자바2", "자바 설명2", 2, 5, session1.getId(), keyword1)); - final Keyword keyword3 = keywordRepository.save( - Keyword.createKeyword("자바3", "자바 설명3", 3, 5, session1.getId(), null)); - final Keyword keyword4 = keywordRepository.save( - Keyword.createKeyword("자바4", "자바 설명4", 4, 5, session1.getId(), keyword3)); - keywordRepository.save( - Keyword.createKeyword("자바5", "자바 설명5", 5, 5, session2.getId(), null)); - keywordRepository.save( - Keyword.createKeyword("자바6", "자바 설명6", 6, 5, session2.getId(), keyword1)); - keywordRepository.save( - Keyword.createKeyword("자바7", "자바 설명7", 7, 5, session2.getId(), null)); - keywordRepository.save( - Keyword.createKeyword("자바8", "자바 설명8", 8, 5, session3.getId(), keyword2)); - final Keyword keyword9 = keywordRepository.save( - Keyword.createKeyword("자바9", "자바 설명9", 9, 5, session4.getId(), keyword2)); - final Keyword keyword10 = keywordRepository.save( - Keyword.createKeyword("자바10", "자바 설명10", 10, 5, session5.getId(), null)); - - final HashSet sessionIds = new HashSet<>( - Arrays.asList(session1.getId(), session4.getId(), session5.getId()) - ); - - //when - final List keywords = keywordRepository.findBySessionIdIn(sessionIds); - - //then - assertThat(keywords) - .usingRecursiveComparison() - .ignoringFields("id", "parent.id") - .isEqualTo(Arrays.asList(keyword1, keyword2, keyword3, keyword4, keyword9, keyword10)); - } - @Test @DisplayName("newFindParentIsNull() : 모든 상위 키워드들을 조회할 수 있다.") void newFindParentIsNull() {