diff --git a/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java b/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java index 3c1bbd0..a7d5828 100644 --- a/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java +++ b/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java @@ -25,4 +25,12 @@ public static MonthBudget toMonthBudget(Long monthBudget) { .monthBudget(monthBudget) .build(); } + + public static MonthBudgetResponseDTO.GetMonthResultDTO toGetMonthResultDTO(MonthBudget monthBudget) { + return MonthBudgetResponseDTO.GetMonthResultDTO.builder() + .monthBudget(monthBudget.getMonthBudget()) + .usedAmount(monthBudget.getUsedAmount()) + .createdAt(monthBudget.getCreatedAt()) + .build(); + } } diff --git a/src/main/java/umc/haruchi/service/MonthBudgetService.java b/src/main/java/umc/haruchi/service/MonthBudgetService.java index bbed1e0..e218610 100644 --- a/src/main/java/umc/haruchi/service/MonthBudgetService.java +++ b/src/main/java/umc/haruchi/service/MonthBudgetService.java @@ -114,6 +114,20 @@ public List distributeDayBudgets(Long memberId) { return dayBudgets; } + public MonthBudget getMonthBudget(Long memberId) { + LocalDate today = LocalDate.now(); + + //member가 존재하는 지 확인 + Member member = memberRepository.findById(memberId) + .orElseThrow(() -> new MonthBudgetHandler(ErrorStatus.NO_MEMBER_EXIST)); + + //member와 year, month 기반으로 해당하는 monthBudget 찾기 + MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, today.getYear(), today.getMonthValue()) + .orElseThrow(() -> new MonthBudgetHandler(ErrorStatus.MONTH_BUDGET_NOT_FOUND)); + + return monthBudget; + } + private long roundDownToNearestHundred(long amount) { return (amount / 100) * 100; } diff --git a/src/main/java/umc/haruchi/web/controller/MonthBudgetController.java b/src/main/java/umc/haruchi/web/controller/MonthBudgetController.java index bd340d4..12818a0 100644 --- a/src/main/java/umc/haruchi/web/controller/MonthBudgetController.java +++ b/src/main/java/umc/haruchi/web/controller/MonthBudgetController.java @@ -28,4 +28,12 @@ public ApiResponse updateMonthBudge MonthBudget monthBudget = monthBudgetService.updateMonthBudget(memberDetail.getMember().getId(), request); return ApiResponse.onSuccess(MonthBudgetConverter.toUpdateMonthResultDTO(monthBudget)); } + + //한달 예산 금액 조회 + @Operation(summary = "한달 예산 금액 조회 API", description = "본인의 한달 예산 금액을 조회하는 API 입니다.") + @GetMapping("/") + public ApiResponse getMonthBudget(@AuthenticationPrincipal MemberDetail memberDetail) { + MonthBudget monthBudget = monthBudgetService.getMonthBudget(memberDetail.getMember().getId()); + return ApiResponse.onSuccess(MonthBudgetConverter.toGetMonthResultDTO(monthBudget)); + } } diff --git a/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java b/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java index 570f890..25fd8c8 100644 --- a/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java +++ b/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java @@ -26,4 +26,14 @@ public static class UpdateMonthResultDTO { Long id; LocalDateTime updatedAt; } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class GetMonthResultDTO { + Long monthBudget; + Long usedAmount; + LocalDateTime createdAt; + } }