From c58e1212ce0bad158024b9f821109d25bab55625 Mon Sep 17 00:00:00 2001 From: ohu-star Date: Sat, 3 Aug 2024 21:58:24 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[FEAT]=20=ED=95=9C=EB=8B=AC=20=EC=A7=80?= =?UTF-8?q?=EC=B6=9C=EB=A5=A0=20=EC=A1=B0=ED=9A=8C=20API=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../haruchi/converter/MonthBudgetConverter.java | 9 ++++++++- .../umc/haruchi/service/MonthBudgetService.java | 17 +++++++++++++++++ .../web/controller/MonthBudgetController.java | 8 ++++++++ .../haruchi/web/dto/MonthBudgetResponseDTO.java | 8 ++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java b/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java index a7d5828..99a447f 100644 --- a/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java +++ b/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java @@ -1,6 +1,7 @@ package umc.haruchi.converter; import umc.haruchi.domain.MonthBudget; +import umc.haruchi.web.dto.BudgetRedistributionResponseDTO; import umc.haruchi.web.dto.MonthBudgetRequestDTO; import umc.haruchi.web.dto.MonthBudgetResponseDTO; @@ -33,4 +34,10 @@ public static MonthBudgetResponseDTO.GetMonthResultDTO toGetMonthResultDTO(Month .createdAt(monthBudget.getCreatedAt()) .build(); } -} + + public static MonthBudgetResponseDTO.GetMonthUsedPercentResultDTO toGetMonthUsedPercentResultDTO(double percent) { + return MonthBudgetResponseDTO.GetMonthUsedPercentResultDTO.builder() + .monthUsedPercent(percent) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/umc/haruchi/service/MonthBudgetService.java b/src/main/java/umc/haruchi/service/MonthBudgetService.java index e218610..eb560d8 100644 --- a/src/main/java/umc/haruchi/service/MonthBudgetService.java +++ b/src/main/java/umc/haruchi/service/MonthBudgetService.java @@ -128,6 +128,23 @@ public MonthBudget getMonthBudget(Long memberId) { return monthBudget; } + public double getMonthUsedPercent(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)); + + //한달 지출률 계산 + double monthUsedAmountPercent = ((double)monthBudget.getUsedAmount() / (double)monthBudget.getMonthBudget())*100; + + //소수점 6번째자리에서 반올림해서 리턴 + return Math.round(monthUsedAmountPercent*1000000)/1000000.0; + } 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 12818a0..4e38c69 100644 --- a/src/main/java/umc/haruchi/web/controller/MonthBudgetController.java +++ b/src/main/java/umc/haruchi/web/controller/MonthBudgetController.java @@ -36,4 +36,12 @@ public ApiResponse getMonthBudget(@Aut MonthBudget monthBudget = monthBudgetService.getMonthBudget(memberDetail.getMember().getId()); return ApiResponse.onSuccess(MonthBudgetConverter.toGetMonthResultDTO(monthBudget)); } + + //한달 지출률 조회 + @Operation(summary = "한달 지출률 조회 API", description = "본인의 한달 지출률을 조회하는 API") + @GetMapping("/percent") + public ApiResponse getMonthBudgetPercent(@AuthenticationPrincipal MemberDetail memberDetail) { + double monthBudgetPercent = monthBudgetService.getMonthUsedPercent(memberDetail.getMember().getId()); + return ApiResponse.onSuccess(MonthBudgetConverter.toGetMonthUsedPercentResultDTO(monthBudgetPercent)); + } } diff --git a/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java b/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java index 25fd8c8..aff3847 100644 --- a/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java +++ b/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java @@ -36,4 +36,12 @@ public static class GetMonthResultDTO { Long usedAmount; LocalDateTime createdAt; } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class GetMonthUsedPercentResultDTO { + double monthUsedPercent; + } } From ae939fcdc1e5c73a89cb6649e2e8927c242fbe22 Mon Sep 17 00:00:00 2001 From: ohu-star Date: Sat, 3 Aug 2024 22:00:40 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[FIX]=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/umc/haruchi/converter/MonthBudgetConverter.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java b/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java index 99a447f..e07f42f 100644 --- a/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java +++ b/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java @@ -1,7 +1,6 @@ package umc.haruchi.converter; import umc.haruchi.domain.MonthBudget; -import umc.haruchi.web.dto.BudgetRedistributionResponseDTO; import umc.haruchi.web.dto.MonthBudgetRequestDTO; import umc.haruchi.web.dto.MonthBudgetResponseDTO;