Skip to content

Commit

Permalink
Type: 챌린지 total cost 조회 방식 변경 (#66)
Browse files Browse the repository at this point in the history
[Chore] get total cost 조회 방식 변경
  • Loading branch information
pingowl authored Nov 5, 2023
2 parents 5bdcbb8 + d4b34da commit 2978047
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/main/java/igoMoney/BE/common/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum ErrorCode {
EXIST_USER_CHALLENGE(HttpStatus.CONFLICT, "이미 등록한 챌린지가 있거나 진행중인 챌린지가 있습니다."),
USER_NOT_IN_CHALLENGE(HttpStatus.CONFLICT, "참여중인 챌린지가 없습니다."),
PERMISSION_DENIED(HttpStatus.UNAUTHORIZED, "해당 챌린지에 인증글을 올릴 권한이 없습니다"),
USER_NOT_FOUND_IN_THE_CHALLENGE(HttpStatus.NOT_FOUND, "해당 사용자는 해당 챌린지에 참여하지 않았습니다."),

// Token 예외
TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, "토큰이 만료되었습니다."),
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/igoMoney/BE/controller/ChallengeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -71,10 +72,10 @@ public ResponseEntity<Void> giveupChallenge(@PathVariable Long userId) {
}

// 챌린지의 각 사용자별 누적금액 조회
@GetMapping("total-cost/{challengeId}")
public ResponseEntity<List<ChallengeTotalCostResponse>> getTotalCostPerChallengeUser(@PathVariable Long challengeId) {
@GetMapping("total-cost")
public ResponseEntity<ChallengeTotalCostResponse> getTotalCostPerChallengeUser(@RequestBody Map<String, Long> request) {

List<ChallengeTotalCostResponse> response = challengeService.getTotalCostPerChallengeUser(challengeId);
ChallengeTotalCostResponse response = challengeService.getTotalCostPerChallengeUser(request.get("challengeId"), request.get("userId"));

return ResponseEntity.status(HttpStatus.OK).body(response);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package igoMoney.BE.repository;

import igoMoney.BE.domain.Challenge;
import igoMoney.BE.domain.ChallengeUser;
import igoMoney.BE.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
Expand All @@ -11,4 +9,5 @@ public interface ChallengeUserRepository extends JpaRepository<ChallengeUser, Lo

List<ChallengeUser> findAllByChallengeId(Long challengeId);
List<ChallengeUser> findAllByUserId(Long userId);
ChallengeUser findByChallengeIdAndUserId(Long challengeId, Long userId);
}
35 changes: 22 additions & 13 deletions src/main/java/igoMoney/BE/service/ChallengeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,20 @@ else if (sel==2){
}

// 챌린지의 각 사용자별 누적금액 조회
public List<ChallengeTotalCostResponse> getTotalCostPerChallengeUser(Long challengeId) {
public ChallengeTotalCostResponse getTotalCostPerChallengeUser(Long challengeId, Long userId) {

List<ChallengeTotalCostResponse> responseList = new ArrayList<>();
List<Object[]> totalCosts = recordRepository.calculateTotalCostByUserId(challengeId);
for (Object[] obj: totalCosts){

ChallengeTotalCostResponse challengeTotalCostResponse = ChallengeTotalCostResponse.builder()
.userId((Long) obj[0])
.totalCost(((BigDecimal) obj[1]).intValue()) // BigInteger
.build();
responseList.add(challengeTotalCostResponse);
User findUser = getUserOrThrow(userId);
checkIfUserInTheChallenge(userId, challengeId);
List<Object[]> obs = recordRepository.calculateTotalCostByUserId(challengeId);
Integer cost = 0;
if(obs.size() != 0){
cost = ((BigDecimal) obs.get(0)[1]).intValue(); // BigInteger
}

return responseList;
ChallengeTotalCostResponse response = ChallengeTotalCostResponse.builder()
.userId(userId)
.totalCost(cost)
.build();
return response;
}

// 챌린지 완료 (마지막날까지 성공)
Expand Down Expand Up @@ -310,7 +310,7 @@ else if (((BigDecimal) obj[1]).intValue() < minCost){
}

// 챌린지 출석 확인
@Scheduled(cron="10 * * * * *", zone = "Asia/Seoul") // 초 분 시 일 월 요일
@Scheduled(cron="0 0 0 * * *", zone = "Asia/Seoul") // 초 분 시 일 월 요일
public void checkAttendance() {
Integer check = 0;
List<Challenge> challenges = challengeRepository.findAllByStatus("inProgress");
Expand Down Expand Up @@ -378,4 +378,13 @@ private List<User> getAllChallengeUser(Long challengeId) {
}
return userList;
}

// 특정 챌린지에 유저가 참여했는지 조회
private void checkIfUserInTheChallenge(Long userId, Long challengeId){

ChallengeUser cu = challengeUserRepository.findByChallengeIdAndUserId(challengeId, userId);
if(cu==null){
throw new CustomException(ErrorCode.USER_NOT_FOUND_IN_THE_CHALLENGE);
}
}
}

0 comments on commit 2978047

Please sign in to comment.