Skip to content

Commit

Permalink
Merge pull request #168 from BudgetBuddiesTeam/revert-167-refactor/#163
Browse files Browse the repository at this point in the history
Revert "[refactor] 소비 레포트 소비 top3 조회 수정"
  • Loading branch information
m3k0813 authored Aug 22, 2024
2 parents d44d565 + 5e5aa89 commit cd1fad6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,21 @@ List<AvgConsumptionGoalDto> findAvgGoalAmountByCategory(
List<MyConsumptionGoalDto> 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<CategoryConsumptionCountDto> 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public List<TopCategoryConsumptionDto> getTopConsumptionCategories(Long userId,
checkPeerInfo(userId, peerAgeS, peerAgeE, peerG);

List<CategoryConsumptionCountDto> categoryConsumptionCountDto = consumptionGoalRepository
.findTopCategoriesByConsumptionCount(peerAgeStart, peerAgeEnd, peerGender, currentMonth.atStartOfDay());
.findTopCategoriesByConsumptionCount(peerAgeStart, peerAgeEnd, peerGender, currentMonth);

return categoryConsumptionCountDto.stream()
.limit(3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -302,7 +272,7 @@ void findTopCategoriesByConsumptionCount_Success() {
LocalDate currentMonth = LocalDate.now();

List<CategoryConsumptionCountDto> result = consumptionGoalRepository.findTopCategoriesByConsumptionCount(
peerAgeStart, peerAgeEnd, peerGender, currentMonth.atStartOfDay());
peerAgeStart, peerAgeEnd, peerGender, currentMonth);

// then
assertThat(result).isNotEmpty();
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit cd1fad6

Please sign in to comment.