Skip to content

Commit

Permalink
feat: 유저 ID로 답변 개수 조회 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
nuyh99 committed Nov 1, 2023
1 parent d0ec8fc commit dc4fae9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -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<Keyword, Long> {

@EntityGraph(attributePaths = "recommendedPosts", type = FETCH)
Optional<Keyword> 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<KeywordIdAndTotalQuizCount> findTotalQuizCount();

@EntityGraph(attributePaths = "recommendedPosts", type = FETCH)
List<Keyword> 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<KeywordIdAndDoneQuizCount> 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 "
Expand All @@ -42,5 +37,9 @@ public interface KeywordRepository extends JpaRepository<Keyword, Long> {
+ "WHERE k.parent IS NULL")
List<Keyword> newFindByParentIsNull();

List<Keyword> findBySessionIdIn(final Set<Long> 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<Keyword> findAllByCurriculumId(Long curriculumId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package wooteco.prolog.roadmap.domain.repository.dto;

public interface KeywordIdAndDoneQuizCount {
long getKeywordId();

int getDoneQuizCount();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package wooteco.prolog.roadmap.domain.repository.dto;

public interface KeywordIdAndTotalQuizCount {
long getKeywordId();

int getTotalQuizCount();
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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<Long> sessionIds = new HashSet<>(
Arrays.asList(session1.getId(), session4.getId(), session5.getId())
);

//when
final List<Keyword> 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() {
Expand Down

0 comments on commit dc4fae9

Please sign in to comment.