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 46ae852e..4f5857ce 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(" + - "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") + "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") List findTopCategoriesByConsumptionCount( @Param("peerAgeStart") int peerAgeStart, @Param("peerAgeEnd") int peerAgeEnd, @Param("peerGender") Gender peerGender, - @Param("currentMonth") LocalDate currentMonth); + @Param("currentMonth") LocalDateTime 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 a371880d..37dc7d7b 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); + .findTopCategoriesByConsumptionCount(peerAgeStart, peerAgeEnd, peerGender, currentMonth.atStartOfDay()); return categoryConsumptionCountDto.stream() .limit(3) @@ -545,25 +545,26 @@ public void decreaseConsumeAmount(Long userId, Long categoryId, Long amount, Loc @Transactional public void updateOrCreateDeletedConsumptionGoal(Long userId, Long categoryId, LocalDate goalMonth, Long amount) { User user = userRepository.findById(userId) - .orElseThrow(() -> new IllegalArgumentException("Invalid user ID")); + .orElseThrow(() -> new IllegalArgumentException("Invalid user ID")); Category category = categoryRepository.findById(categoryId) - .orElseThrow(() -> new IllegalArgumentException("Invalid category ID")); + .orElseThrow(() -> new IllegalArgumentException("Invalid category ID")); // 해당 월의 ConsumptionGoal이 존재하는지 확인 - Optional existingGoal = consumptionGoalRepository.findConsumptionGoalByUserAndCategoryAndGoalMonth(user, category, goalMonth); + Optional existingGoal = consumptionGoalRepository.findConsumptionGoalByUserAndCategoryAndGoalMonth( + user, category, goalMonth); - if (existingGoal.isPresent()) { // 존재하는 경우, consumeAmount 업데이트 + if (existingGoal.isPresent()) { // 존재하는 경우, consumeAmount 업데이트 ConsumptionGoal consumptionGoal = existingGoal.get(); consumptionGoal.updateConsumeAmount(amount); consumptionGoalRepository.save(consumptionGoal); - } else { // 존재하지 않는 경우, 새로운 ConsumptionGoal을 생성 (이 때 목표 금액은 0) + } else { // 존재하지 않는 경우, 새로운 ConsumptionGoal을 생성 (이 때 목표 금액은 0) ConsumptionGoal newGoal = ConsumptionGoal.builder() - .user(user) - .category(category) - .goalMonth(goalMonth) - .consumeAmount(amount) - .goalAmount(0L) - .build(); + .user(user) + .category(category) + .goalMonth(goalMonth) + .consumeAmount(amount) + .goalAmount(0L) + .build(); newGoal.updateConsumeAmount(amount); // 신규 생성된 목표에 소비 금액 추가 consumptionGoalRepository.save(newGoal); 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 054b35de..5682da22 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,6 +3,7 @@ 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; @@ -18,6 +19,8 @@ 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; @@ -32,12 +35,15 @@ 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() { @@ -93,6 +99,30 @@ 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 @@ -272,7 +302,7 @@ void findTopCategoriesByConsumptionCount_Success() { LocalDate currentMonth = LocalDate.now(); List result = consumptionGoalRepository.findTopCategoriesByConsumptionCount( - peerAgeStart, peerAgeEnd, peerGender, currentMonth); + peerAgeStart, peerAgeEnd, peerGender, currentMonth.atStartOfDay()); // then assertThat(result).isNotEmpty(); @@ -287,7 +317,7 @@ void findTopCategoriesByConsumptionCount_Success() { .findFirst() .orElseThrow(() -> new AssertionError("Category ID " + defaultCategory2.getId() + " not found")); - assertThat(firstResult.getConsumptionCount()).isEqualTo(1); - assertThat(secondResult.getConsumptionCount()).isEqualTo(2); + assertThat(firstResult.getConsumptionCount()).isEqualTo(2); + assertThat(secondResult.getConsumptionCount()).isEqualTo(1); } } \ 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 07b42d1c..8ec47b51 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)) + Gender.valueOf(peerGender), currentMonth.atStartOfDay())) .willReturn(List.of(topConsumption1, topConsumption2, topConsumption3)); given(categoryRepository.findById(defaultCategory1.getId())).willReturn(Optional.of(defaultCategory1));