Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 한달 지출률 조회 API 구현 #43

Merged
merged 2 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,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();
}
}
17 changes: 17 additions & 0 deletions src/main/java/umc/haruchi/service/MonthBudgetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ public ApiResponse<MonthBudgetResponseDTO.GetMonthResultDTO> getMonthBudget(@Aut
MonthBudget monthBudget = monthBudgetService.getMonthBudget(memberDetail.getMember().getId());
return ApiResponse.onSuccess(MonthBudgetConverter.toGetMonthResultDTO(monthBudget));
}

//한달 지출률 조회
@Operation(summary = "한달 지출률 조회 API", description = "본인의 한달 지출률을 조회하는 API")
@GetMapping("/percent")
public ApiResponse<MonthBudgetResponseDTO.GetMonthUsedPercentResultDTO> getMonthBudgetPercent(@AuthenticationPrincipal MemberDetail memberDetail) {
double monthBudgetPercent = monthBudgetService.getMonthUsedPercent(memberDetail.getMember().getId());
return ApiResponse.onSuccess(MonthBudgetConverter.toGetMonthUsedPercentResultDTO(monthBudgetPercent));
}
}
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 @@ -36,4 +36,12 @@ public static class GetMonthResultDTO {
Long usedAmount;
LocalDateTime createdAt;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class GetMonthUsedPercentResultDTO {
double monthUsedPercent;
}
}
Loading