From 80598d49f665bc158194754fabe78701844c64b4 Mon Sep 17 00:00:00 2001 From: BK Date: Mon, 20 Nov 2023 01:10:58 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20=EC=B1=8C=EB=A6=B0=EC=A7=80=20totalcost?= =?UTF-8?q?=20=EC=82=B0=EC=B6=9C=EB=B0=A9=EC=8B=9D=20=EC=98=A4=EB=A5=98=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/igoMoney/BE/repository/RecordRepository.java | 5 ++++- src/main/java/igoMoney/BE/service/ChallengeService.java | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) 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;