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 4, 2024
1 parent 7da8286 commit e4cc147
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/umc/haruchi/converter/MonthBudgetConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,11 @@ public static MonthBudgetResponseDTO.GetWeekBudgetResultListDTO toGetWeekBudgetR
.weekBudget(weekBudgetDTOList)
.build();
}

public static MonthBudgetResponseDTO.GetMonthLeftNowResultDTO toGetMonthLeftNowResultDTO(Integer leftDay, Long leftBudget) {
return MonthBudgetResponseDTO.GetMonthLeftNowResultDTO.builder()
.leftDay(leftDay)
.leftBudget(leftBudget)
.build();
}
}
38 changes: 38 additions & 0 deletions src/main/java/umc/haruchi/service/MonthBudgetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,44 @@ public List<Integer> getMonthAndWeek(Long memberId) {
return currentWeek;
}


public Integer getMonthLeftDay(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 nowDay = today.getDayOfMonth();

int year = monthBudget.getYear();
int month = monthBudget.getMonth();
int dayInMonth = YearMonth.of(year, month).lengthOfMonth();

//남은 일자
return dayInMonth - nowDay + 1;
}

public Long getMonthLeftBudget(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(member.getId(), today.getYear(), today.getMonthValue())
.orElseThrow(() -> new MonthBudgetHandler(ErrorStatus.MONTH_BUDGET_NOT_FOUND));

//남은 예산
return monthBudget.getMonthBudget() -monthBudget.getUsedAmount();
}

private long roundDownToNearestHundred(long amount) {
return (amount / 100) * 100;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ public ApiResponse<MonthBudgetResponseDTO.GetWeekBudgetResultListDTO> getWeekBud
List<Integer> currentWeek = monthBudgetService.getMonthAndWeek(memberDetail.getMember().getId());
return ApiResponse.onSuccess(MonthBudgetConverter.toGetWeekBudgetResultListDTO(weekBudget, currentWeek.get(0), currentWeek.get(1)));
}

//남은 일정 및 금액 조회
@Operation(summary = "남은 일정 및 금액 조회 API", description = "본인의 남은 한달 일정 및 금액을 조회하는 API")
@GetMapping("/left-now")
public ApiResponse<MonthBudgetResponseDTO.GetMonthLeftNowResultDTO> getMonthLeftNow(@AuthenticationPrincipal MemberDetail memberDetail) {
Integer leftDay = monthBudgetService.getMonthLeftDay(memberDetail.getMember().getId());
Long leftBudget = monthBudgetService.getMonthLeftBudget(memberDetail.getMember().getId());
return ApiResponse.onSuccess(MonthBudgetConverter.toGetMonthLeftNowResultDTO(leftDay, leftBudget));
}
}
9 changes: 9 additions & 0 deletions src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,13 @@ public static class GetWeekBudgetResultListDTO {
Integer month;
Integer week;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class GetMonthLeftNowResultDTO {
Integer leftDay;
Long leftBudget;
}
}

0 comments on commit e4cc147

Please sign in to comment.