💻문제점1
NutrientsQueryService.java
public List<CaloriesOfMealType> searchWeeklyIntakeCalories(
Long memberId, LocalDate startDate, LocalDate endDate) {
memberQueryService.search(memberId);
List<CaloriesOfMealType> caloriesOfMealTypes = new ArrayList<>();
LocalDate currentDate = startDate;
while (!currentDate.isAfter(endDate)) {
LocalDateTime startDateTime = currentDate.atStartOfDay();
LocalDateTime endDateTime = currentDate.atTime(LocalTime.MAX);
List<MealLog> mealLogs = findMealLog(memberId, startDateTime, endDateTime);
caloriesOfMealTypes.add(sumCalorieByMealType(mealLogs));
currentDate = currentDate.plusDays(1);
}
return caloriesOfMealTypes;
}
현재 코드는 이렇다. while문을 쓰지 않고 startDate에서 endDate까지 하루씩 더해갈 수 있을까?
🔍문제점1-해결
LocalDate에는 datesUntil() 메서드가 있다. 이를 통해서 날짜 스트림을 생성할 수 있다.
나는 startDate를 시작으로 endDate.plusDays(1) 까지의 날짜 스트림을 얻어 처리하도록 변경했다. (endDate 포함)
public List<CaloriesOfMealType> searchWeeklyIntakeCalories(
Long memberId, LocalDate startDate, LocalDate endDate) {
memberQueryService.search(memberId);
return startDate.datesUntil(endDate.plusDays(1))
.map(date -> {
LocalDateTime startDateTime = date.atStartOfDay();
LocalDateTime endDateTime = date.atTime(LocalTime.MAX);
List<MealLog> mealLogs = findMealLog(memberId, startDateTime, endDateTime);
return sumCalorieByMealType(mealLogs);
})
.toList();
}
728x90
'TIL' 카테고리의 다른 글
[TIL - 20231226] jacoco 패키지, 클래스 Report에서 제외 (0) | 2023.12.26 |
---|---|
[TIL - 20231222] AuthenticationEntryPoint를 사용한 JWT 예외 핸들링 (1) | 2023.12.22 |
[TIL-20231222] Illegal pop() with non-matching JdbcValuesSourceProcessingState (0) | 2023.12.22 |
[TIL-20231220] 리플렉션을 사용한 private 필드 접근 및 값 설정 (0) | 2023.12.20 |
[TIL - 20230914~15] Github Actions+Docker CI/CD 트러블슈팅 (0) | 2023.09.15 |