Skip to content

Commit

Permalink
Merge pull request #172 from BudgetBuddiesTeam/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ggamD00 authored Aug 23, 2024
2 parents 0c1504c + 2190653 commit abeee27
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 27 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(" +
"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<CategoryConsumptionCountDto> 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")
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);
.findTopCategoriesByConsumptionCount(peerAgeStart, peerAgeEnd, peerGender, currentMonth.atStartOfDay());

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

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

// then
assertThat(result).isNotEmpty();
Expand All @@ -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);
}
}
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))
Gender.valueOf(peerGender), currentMonth.atStartOfDay()))
.willReturn(List.of(topConsumption1, topConsumption2, topConsumption3));

given(categoryRepository.findById(defaultCategory1.getId())).willReturn(Optional.of(defaultCategory1));
Expand Down

0 comments on commit abeee27

Please sign in to comment.