Skip to content

Commit

Permalink
feat: 질문 개수 체크 로직에서 DB 접근 줄이기
Browse files Browse the repository at this point in the history
  • Loading branch information
shin-jisong committed Nov 25, 2024
1 parent e30f7cd commit 6b52256
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public interface ChecklistQuestionRepository extends JpaRepository<ChecklistQues
List<Category> findAllQuestionCategoriesByUserIdAndChecklistId(@Param("userId") Long userId,
@Param("checklistId") Long checklistId);

@Query("SELECT COUNT(cq) FROM ChecklistQuestion cq "
@Query("SELECT cq FROM ChecklistQuestion cq "
+ "WHERE cq.checklist.id = :checklistId "
+ "AND cq.deleted = false "
+ "AND cq.answer <> 'NONE' "
+ "AND cq.question.category.id = :categoryId")
Integer countAnsweredQuestionsByChecklistIdAndCategoryId(@Param("checklistId") Long checklistId,
List<ChecklistQuestion> findAnsweredQuestionsByChecklistIdAndCategoryId(@Param("checklistId") Long checklistId,
@Param("categoryId") Integer categoryId);

@Query(" SELECT COUNT(cq) FROM ChecklistQuestion cq "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ public List<Category> findCategories(User user, Long checklistId) {

@Transactional(readOnly = true)
public Integer calculateCategoryScore(Long checklistId, Integer categoryId) {
int allAnsweredQuestionCount = checklistQuestionRepository.countAnsweredQuestionsByChecklistIdAndCategoryId(
List<ChecklistQuestion> allAnsweredQuestion = checklistQuestionRepository.findAnsweredQuestionsByChecklistIdAndCategoryId(
checklistId, categoryId);
int goodAnsweredQuestionCount = checklistQuestionRepository.countAnsweredQuestionsByChecklistIdAndCategoryIdAndAnswer(
checklistId, categoryId, Answer.GOOD);

int allAnsweredQuestionCount = allAnsweredQuestion.size();
int goodAnsweredQuestionCount = countGoodAnsweredQuestion(allAnsweredQuestion);

if (allAnsweredQuestionCount == 0) {
return null;
Expand All @@ -113,6 +114,12 @@ public Integer calculateCategoryScore(Long checklistId, Integer categoryId) {
return (int) Math.round(score);
}

private int countGoodAnsweredQuestion(List<ChecklistQuestion> questions) {
return (int) questions.stream()
.filter(question -> question.matchAnswer(Answer.GOOD))
.count();
}

@Transactional
public void deleteAllByChecklistId(Long id) {
checklistQuestionRepository.deleteAllByChecklistId(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void findAllQuestionCategoriesByUserIdAndChecklistId() {
assertThat(categories).hasSize(2);
}

@DisplayName("주어진 체크리스트 ID와 카테고리 ID로 답변된 질문 수 카운트 성공")
@DisplayName("주어진 체크리스트 ID와 카테고리 ID로 답변된 질문 조회 성공")
@Test
void countAnsweredQuestionsByChecklistIdAndCategoryId() {
// given
Expand All @@ -98,13 +98,14 @@ void countAnsweredQuestionsByChecklistIdAndCategoryId() {

// when
Integer categoryId = cq1.getQuestion().getCategory().getId();
Integer count = checklistQuestionRepository.countAnsweredQuestionsByChecklistIdAndCategoryId(checklist.getId(),
categoryId);
List<ChecklistQuestion> answeredQuestions = checklistQuestionRepository.findAnsweredQuestionsByChecklistIdAndCategoryId(
checklist.getId(), categoryId);

// then
assertThat(count).isEqualTo(2);
assertThat(answeredQuestions.size()).isEqualTo(2);
}


@DisplayName("주어진 체크리스트 ID, 카테고리 ID, 특정 답변으로 질문 수 카운트 성공")
@Test
void countAnsweredQuestionsByChecklistIdAndCategoryIdAndAnswer() {
Expand Down

0 comments on commit 6b52256

Please sign in to comment.