Skip to content

Commit

Permalink
[FEAT] 한 주 예산 금액 조회 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ohu-star committed Aug 3, 2024
1 parent 6a163d5 commit 24e550a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/umc/haruchi/converter/MonthBudgetConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
33 changes: 33 additions & 0 deletions src/main/java/umc/haruchi/service/MonthBudgetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<Integer> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,4 +45,12 @@ public ApiResponse<MonthBudgetResponseDTO.GetMonthUsedPercentResultDTO> getMonth
double monthBudgetPercent = monthBudgetService.getMonthUsedPercent(memberDetail.getMember().getId());
return ApiResponse.onSuccess(MonthBudgetConverter.toGetMonthUsedPercentResultDTO(monthBudgetPercent));
}

//한 주 예산 금액 조회
@Operation(summary = "한 주 예산 금액 조회 API", description = "본인의 한 주 예산 금액을 조회하는 API 입니다.")
@GetMapping("/week")
public ApiResponse<MonthBudgetResponseDTO.GetWeekBudgetResultDTO> getWeekBudget(@AuthenticationPrincipal MemberDetail memberDetail) {
Integer weekBudget = monthBudgetService.getWeekBudget(memberDetail.getMember().getId());
return ApiResponse.onSuccess(MonthBudgetConverter.toGetWeekBudgetResultDTO(weekBudget));
}
}
8 changes: 8 additions & 0 deletions src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ public static class GetMonthResultDTO {
public static class GetMonthUsedPercentResultDTO {
double monthUsedPercent;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class GetWeekBudgetResultDTO {
Integer weekBudget;
}
}

0 comments on commit 24e550a

Please sign in to comment.