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 구현 #41

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
8 changes: 8 additions & 0 deletions src/main/java/umc/haruchi/converter/MonthBudgetConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
14 changes: 14 additions & 0 deletions src/main/java/umc/haruchi/service/MonthBudgetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ public List<DayBudget> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ public ApiResponse<MonthBudgetResponseDTO.UpdateMonthResultDTO> updateMonthBudge
MonthBudget monthBudget = monthBudgetService.updateMonthBudget(memberDetail.getMember().getId(), request);
return ApiResponse.onSuccess(MonthBudgetConverter.toUpdateMonthResultDTO(monthBudget));
}

//한달 예산 금액 조회
@Operation(summary = "한달 예산 금액 조회 API", description = "본인의 한달 예산 금액을 조회하는 API 입니다.")
@GetMapping("/")
public ApiResponse<MonthBudgetResponseDTO.GetMonthResultDTO> getMonthBudget(@AuthenticationPrincipal MemberDetail memberDetail) {
MonthBudget monthBudget = monthBudgetService.getMonthBudget(memberDetail.getMember().getId());
return ApiResponse.onSuccess(MonthBudgetConverter.toGetMonthResultDTO(monthBudget));
}
}
10 changes: 10 additions & 0 deletions src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Loading