프로젝트 날짜 필터 구현

  • Reminder 클래스의 LocalDate 멤버 reminderDate를 년/월 별로 모아 List<Reminder>를 반환하는 API 구현
public interface ReminderRepository extends JpaRepository<Reminder, Long> {

    List<Reminder> findAllByReminderDateBetween(LocalDate start, LocalDate end);

}
  • JPA를 활용해 start 날짜와 end 날짜 범위에 해당하는 reminderDate 값을 갖는 Reminder 리스트를 반환하도록 Repository 내에 정의해놓았다.
  • 서비스단의 findReminderByListByMonthAndYear 메서드
  • 유저의 식별자와 찾고자 하는 리마인더의 년도와 월을 파라미터로 받는다.
public List<ReminderResponseDto> readReminderListByFMonthAndYear(Long userNo, int year, int month){
    List<ReminderResponseDto> reminderDtoList = new ArrayList<>();
    List<Reminder> reminderList = findReminderListByMonthAndYear(year, month);

    for(Reminder r : reminderList){
        ReminderResponseDto dto = new ReminderResponseDto(r);
        if(dto.getUserNo()==userNo) reminderDtoList.add(dto);
    }

    return reminderDtoList;
  • 해당하는 리마인더 List 를 얻어내기 위해 findReminderListByMonthAndYear 메서드를 다음과 같이 작성했다.
public List<Reminder> findReminderListByMonthAndYear(int year, int month){
    LocalDate start = LocalDate.of(year, month, 1);
    LocalDate end = start.plusMonths(1).minusDays(1);

    return reminderRepository.findAllByReminderDateBetween(start, end);
}
  • year 과 month를 입력받아 LocalDate 클래스의 of 메서드를 사용한다.
    • of(int year, int month, int day)
  • 입력받은 month의 첫 번째 날짜인 LocalDate start를 생성한다.
  • start 를 이용해 같은 달의 마지막 날 LocalDate end를 생성한다.

이렇게 입력받은 년/월에 해당하는 List<Reminder>를 return 하면 된다.

 


 

다음은 Swagger UI를 활용해 정상적으로 작동하는지 활용하면 된다.

 

날짜 입력값에 대한 Validation은 다음과 같이 추가했다.

https://eggmomo.tistory.com/77

 

년/월 필터 Validation, 회원가입시 자동 이름과 랜덤 ID 생성

년/월 필터 Validation https://eggmomo.tistory.com/76 프로젝트 날짜 필터 구현 Reminder 클래스의 LocalDate 멤버 reminderDate를 년/월 별로 모아 List를 반환하는 API 구현 public interface ReminderRepository extends JpaRepositor

eggmomo.tistory.com