diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java index 4f5857ce..46ae852e 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java @@ -96,21 +96,21 @@ List findAvgGoalAmountByCategory( List findAllGoalAmountByUserId(@Param("userId") Long userId); @Query("SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryConsumptionCountDto(" + - "e.category.id, COUNT(e)) " + - "FROM Expense e " + - "WHERE e.category.isDefault = true " + - "AND e.deleted = false " + - "AND e.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + - "AND e.user.gender = :peerGender " + - "AND e.expenseDate >= :currentMonth " + - "AND e.amount > 0 " + - "GROUP BY e.category.id " + - "ORDER BY COUNT(e) DESC") + "cg.category.id, COUNT(cg)) " + + "FROM ConsumptionGoal cg " + + "WHERE cg.category.isDefault = true " + + "AND cg.deleted = false " + + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + + "AND cg.user.gender = :peerGender " + + "AND cg.goalMonth >= :currentMonth " + + "AND cg.consumeAmount > 0 " + + "GROUP BY cg.category.id " + + "ORDER BY COUNT(cg) DESC") List findTopCategoriesByConsumptionCount( @Param("peerAgeStart") int peerAgeStart, @Param("peerAgeEnd") int peerAgeEnd, @Param("peerGender") Gender peerGender, - @Param("currentMonth") LocalDateTime currentMonth); + @Param("currentMonth") LocalDate currentMonth); @Modifying @Query("UPDATE ConsumptionGoal cg SET cg.deleted = TRUE WHERE cg.category.id = :categoryId AND cg.user.id = :userId") diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java index 45e2ef94..a371880d 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java @@ -172,7 +172,7 @@ public List getTopConsumptionCategories(Long userId, checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); List categoryConsumptionCountDto = consumptionGoalRepository - .findTopCategoriesByConsumptionCount(peerAgeStart, peerAgeEnd, peerGender, currentMonth.atStartOfDay()); + .findTopCategoriesByConsumptionCount(peerAgeStart, peerAgeEnd, peerGender, currentMonth); return categoryConsumptionCountDto.stream() .limit(3) diff --git a/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepositoryTest.java b/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepositoryTest.java index 5682da22..054b35de 100644 --- a/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepositoryTest.java +++ b/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepositoryTest.java @@ -3,7 +3,6 @@ import static org.assertj.core.api.Assertions.*; import java.time.LocalDate; -import java.time.LocalDateTime; import java.util.List; import org.junit.jupiter.api.BeforeEach; @@ -19,8 +18,6 @@ import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryConsumptionCountDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; -import com.bbteam.budgetbuddies.domain.expense.entity.Expense; -import com.bbteam.budgetbuddies.domain.expense.repository.ExpenseRepository; import com.bbteam.budgetbuddies.domain.user.entity.User; import com.bbteam.budgetbuddies.domain.user.repository.UserRepository; import com.bbteam.budgetbuddies.enums.Gender; @@ -35,15 +32,12 @@ class ConsumptionGoalRepositoryTest { UserRepository userRepository; @Autowired CategoryRepository categoryRepository; - @Autowired - ExpenseRepository expenseRepository; private User peerUser1; private User peerUser2; private Category defaultCategory1; private Category defaultCategory2; private LocalDate currentMonth; - private LocalDateTime now = LocalDateTime.now(); @BeforeEach void setUp() { @@ -99,30 +93,6 @@ void setUp() { .category(defaultCategory2) .goalMonth(currentMonth) .build()); - - expenseRepository.save( - Expense.builder() - .amount(1L) - .category(defaultCategory1) - .user(peerUser1) - .expenseDate(now) - .build()); - - expenseRepository.save( - Expense.builder() - .amount(1L) - .category(defaultCategory1) - .user(peerUser1) - .expenseDate(now) - .build()); - - expenseRepository.save( - Expense.builder() - .amount(1L) - .category(defaultCategory2) - .user(peerUser1) - .expenseDate(now) - .build()); } @Test @@ -302,7 +272,7 @@ void findTopCategoriesByConsumptionCount_Success() { LocalDate currentMonth = LocalDate.now(); List result = consumptionGoalRepository.findTopCategoriesByConsumptionCount( - peerAgeStart, peerAgeEnd, peerGender, currentMonth.atStartOfDay()); + peerAgeStart, peerAgeEnd, peerGender, currentMonth); // then assertThat(result).isNotEmpty(); @@ -317,7 +287,7 @@ void findTopCategoriesByConsumptionCount_Success() { .findFirst() .orElseThrow(() -> new AssertionError("Category ID " + defaultCategory2.getId() + " not found")); - assertThat(firstResult.getConsumptionCount()).isEqualTo(2); - assertThat(secondResult.getConsumptionCount()).isEqualTo(1); + assertThat(firstResult.getConsumptionCount()).isEqualTo(1); + assertThat(secondResult.getConsumptionCount()).isEqualTo(2); } } \ No newline at end of file diff --git a/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceTest.java b/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceTest.java index 8ec47b51..07b42d1c 100644 --- a/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceTest.java +++ b/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceTest.java @@ -384,7 +384,7 @@ void getTopConsumptionCategories_Success() { given(userRepository.findById(user.getId())).willReturn(Optional.of(user)); given(consumptionGoalRepository.findTopCategoriesByConsumptionCount(peerAgeStart, peerAgeEnd, - Gender.valueOf(peerGender), currentMonth.atStartOfDay())) + Gender.valueOf(peerGender), currentMonth)) .willReturn(List.of(topConsumption1, topConsumption2, topConsumption3)); given(categoryRepository.findById(defaultCategory1.getId())).willReturn(Optional.of(defaultCategory1));