Chef.Yeon
Code Cook
Chef.Yeon
전체 방문자
오늘
어제
  • 분류 전체보기 (230)
    • 게임 개발 (1)
      • Unity (1)
    • Android (27)
      • Kotlin (19)
      • 우아한테크코스 5기 (4)
    • Language (11)
      • 파이썬 (3)
      • Java (7)
    • DB (2)
      • SQL (16)
    • Spring (25)
    • 코딩테스트 (56)
    • Git (1)
    • TIL (85)
    • DevOps (6)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • SQL
  • 백준
  • 레포지토리
  • ec2
  • 내림차순
  • 우아한테크코스
  • rsocket
  • grafana
  • Android
  • 코틀린 인 액션
  • webflux
  • MariaDB
  • Docker
  • 프리코스
  • elasticsearch
  • spring
  • 파이썬
  • kibana
  • 다이나믹 프로그래밍
  • Wil
  • 코딩테스트
  • 문자열
  • enum
  • 프로그래머스
  • java
  • 에라토스테네스의 체
  • 안드로이드
  • til
  • 코틀린
  • kotlin

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Chef.Yeon

Code Cook

[TIL - 20231222] searchWeeklyIntakeCalories 로직 개선
TIL

[TIL - 20231222] searchWeeklyIntakeCalories 로직 개선

2023. 12. 22. 19:39

 

 

💻문제점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
    'TIL' 카테고리의 다른 글
    • [TIL - 20231226] jacoco 패키지, 클래스 Report에서 제외
    • [TIL - 20231222] AuthenticationEntryPoint를 사용한 JWT 예외 핸들링
    • [TIL-20231222] Illegal pop() with non-matching JdbcValuesSourceProcessingState
    • [TIL-20231220] 리플렉션을 사용한 private 필드 접근 및 값 설정
    Chef.Yeon
    Chef.Yeon
    보기 좋고 깔끔한 코드를 요리하기 위해 노력하고 있습니다.

    티스토리툴바