Skip to content

Commit

Permalink
Type: 챌린지 totalcost 산출방식 오류 수정 (#94)
Browse files Browse the repository at this point in the history
[Fix] 챌린지 totalcost 산출방식 오류 수정
  • Loading branch information
pingowl authored Nov 19, 2023
2 parents beca2d2 + 80598d4 commit 6fc228f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/main/java/igoMoney/BE/repository/RecordRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
public interface RecordRepository extends JpaRepository<Record, Long> {
List<Record> 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<Object[]> 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<Object[]> calculateTotalCostByUserId(@Param("challengeId") Long challengeId);
List<Object[]> calculateTotalCostByChallengeId(@Param("challengeId") Long challengeId);

List<Record> 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)
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/igoMoney/BE/service/ChallengeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ public ChallengeTotalCostResponse getTotalCostPerChallengeUser(Long challengeId,

User findUser = getUserOrThrow(userId);
checkIfUserInTheChallenge(userId, challengeId);
List<Object[]> obs = recordRepository.calculateTotalCostByUserId(challengeId);
List<Object[]> 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()
Expand All @@ -257,7 +257,7 @@ public void finishChallenge() {
c.finishChallenge();

// 챌린지 승자 결정
List<Object[]> totalCosts = recordRepository.calculateTotalCostByUserId(c.getId());
List<Object[]> totalCosts = recordRepository.calculateTotalCostByChallengeId(c.getId());
for (Object[] obj: totalCosts){
if(((BigDecimal) obj[1]).intValue() == minCost){
check = true;
Expand Down

0 comments on commit 6fc228f

Please sign in to comment.