diff --git a/src/main/java/igoMoney/BE/repository/RecordRepository.java b/src/main/java/igoMoney/BE/repository/RecordRepository.java index 0013033..a48b5e2 100644 --- a/src/main/java/igoMoney/BE/repository/RecordRepository.java +++ b/src/main/java/igoMoney/BE/repository/RecordRepository.java @@ -11,8 +11,11 @@ public interface RecordRepository extends JpaRepository { List findAllByUserIdAndDate(Long userId, LocalDate date); + @Query(value="SELECT r.user_id, SUM(r.cost) FROM record r WHERE r.challenge_id= :challengeId AND r.user_id= :userId", nativeQuery = true) + List calculateTotalCostByUserId(@Param("challengeId") Long challengeId, @Param("userId")Long userId); + @Query(value="SELECT r.user_id, SUM(r.cost) FROM record r WHERE r.challenge_id= :challengeId GROUP BY r.user_id", nativeQuery = true) - List calculateTotalCostByUserId(@Param("challengeId") Long challengeId); + List calculateTotalCostByChallengeId(@Param("challengeId") Long challengeId); List findAllByUserId(Long userId); @Query(value="SELECT COUNT(*) FROM record r WHERE r.user_id= :userId AND r.date >= DATE_SUB(:date,INTERVAL 3 DAY)", nativeQuery = true) diff --git a/src/main/java/igoMoney/BE/service/ChallengeService.java b/src/main/java/igoMoney/BE/service/ChallengeService.java index de3ea8a..6b7737b 100644 --- a/src/main/java/igoMoney/BE/service/ChallengeService.java +++ b/src/main/java/igoMoney/BE/service/ChallengeService.java @@ -230,9 +230,9 @@ public ChallengeTotalCostResponse getTotalCostPerChallengeUser(Long challengeId, User findUser = getUserOrThrow(userId); checkIfUserInTheChallenge(userId, challengeId); - List obs = recordRepository.calculateTotalCostByUserId(challengeId); + List obs = recordRepository.calculateTotalCostByUserId(challengeId, userId); Integer cost = 0; - if(obs.size() != 0){ + if(obs.size() != 0 && obs.get(0)[1]!=null){ cost = ((BigDecimal) obs.get(0)[1]).intValue(); // BigInteger } ChallengeTotalCostResponse response = ChallengeTotalCostResponse.builder() @@ -257,7 +257,7 @@ public void finishChallenge() { c.finishChallenge(); // 챌린지 승자 결정 - List totalCosts = recordRepository.calculateTotalCostByUserId(c.getId()); + List totalCosts = recordRepository.calculateTotalCostByChallengeId(c.getId()); for (Object[] obj: totalCosts){ if(((BigDecimal) obj[1]).intValue() == minCost){ check = true;