From 24e550a2d1d637543f762b9e885c494483a9039d Mon Sep 17 00:00:00 2001 From: ohu-star Date: Sun, 4 Aug 2024 00:13:36 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20=ED=95=9C=20=EC=A3=BC=20=EC=98=88?= =?UTF-8?q?=EC=82=B0=20=EA=B8=88=EC=95=A1=20=EC=A1=B0=ED=9A=8C=20API=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../converter/MonthBudgetConverter.java | 6 ++++ .../haruchi/service/MonthBudgetService.java | 33 +++++++++++++++++++ .../web/controller/MonthBudgetController.java | 9 +++++ .../web/dto/MonthBudgetResponseDTO.java | 8 +++++ 4 files changed, 56 insertions(+) diff --git a/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java b/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java index e07f42f..f94f8d9 100644 --- a/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java +++ b/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java @@ -39,4 +39,10 @@ public static MonthBudgetResponseDTO.GetMonthUsedPercentResultDTO toGetMonthUsed .monthUsedPercent(percent) .build(); } + + public static MonthBudgetResponseDTO.GetWeekBudgetResultDTO toGetWeekBudgetResultDTO(Integer budget) { + return MonthBudgetResponseDTO.GetWeekBudgetResultDTO.builder() + .weekBudget(budget) + .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 eb560d8..199779a 100644 --- a/src/main/java/umc/haruchi/service/MonthBudgetService.java +++ b/src/main/java/umc/haruchi/service/MonthBudgetService.java @@ -19,12 +19,15 @@ import umc.haruchi.web.dto.MonthBudgetRequestDTO; import umc.haruchi.web.dto.MonthBudgetResponseDTO; +import java.time.DayOfWeek; import java.time.LocalDate; import java.time.YearMonth; import java.util.ArrayList; import java.util.List; import java.util.Optional; +import static umc.haruchi.apiPayload.code.status.ErrorStatus.NOT_SOME_DAY_BUDGET; + @Service @RequiredArgsConstructor @Transactional(readOnly = true) @@ -145,6 +148,36 @@ public double getMonthUsedPercent(Long memberId) { //소수점 6번째자리에서 반올림해서 리턴 return Math.round(monthUsedAmountPercent*1000000)/1000000.0; } + + public Integer getWeekBudget(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)); + + //이번 주 일요일까지 남은 일수 계산 + int remainDays = DayOfWeek.SUNDAY.getValue() - today.getDayOfWeek().getValue(); + + //남은 일수의 dayBudget 구하기 + List dayBudgets = new ArrayList<>(); + + for(int i=0; i<= remainDays; i++){ + DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, today.getDayOfMonth()+i) + .orElseThrow(() -> new DayBudgetHandler(NOT_SOME_DAY_BUDGET)); + dayBudgets.add(dayBudget.getDayBudget()); + } + + //남은 일수의 dayBudget 합 리턴 + return dayBudgets.stream() + .mapToInt(Integer::intValue) + .sum(); + } + 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 4e38c69..5f9b97e 100644 --- a/src/main/java/umc/haruchi/web/controller/MonthBudgetController.java +++ b/src/main/java/umc/haruchi/web/controller/MonthBudgetController.java @@ -8,6 +8,7 @@ import umc.haruchi.apiPayload.ApiResponse; import umc.haruchi.config.login.auth.MemberDetail; import umc.haruchi.converter.MonthBudgetConverter; +import umc.haruchi.domain.Member; import umc.haruchi.domain.MonthBudget; import umc.haruchi.service.MonthBudgetService; import umc.haruchi.web.dto.MonthBudgetRequestDTO; @@ -44,4 +45,12 @@ public ApiResponse getMonth double monthBudgetPercent = monthBudgetService.getMonthUsedPercent(memberDetail.getMember().getId()); return ApiResponse.onSuccess(MonthBudgetConverter.toGetMonthUsedPercentResultDTO(monthBudgetPercent)); } + + //한 주 예산 금액 조회 + @Operation(summary = "한 주 예산 금액 조회 API", description = "본인의 한 주 예산 금액을 조회하는 API 입니다.") + @GetMapping("/week") + public ApiResponse getWeekBudget(@AuthenticationPrincipal MemberDetail memberDetail) { + Integer weekBudget = monthBudgetService.getWeekBudget(memberDetail.getMember().getId()); + return ApiResponse.onSuccess(MonthBudgetConverter.toGetWeekBudgetResultDTO(weekBudget)); + } } diff --git a/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java b/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java index aff3847..c7e769d 100644 --- a/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java +++ b/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java @@ -44,4 +44,12 @@ public static class GetMonthResultDTO { public static class GetMonthUsedPercentResultDTO { double monthUsedPercent; } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class GetWeekBudgetResultDTO { + Integer weekBudget; + } }