Java Pageable - Each page different date starting from today

Viewed 710

So I want to return a pageable that contains appointments. Each page in the pageable should contain only the appointments happening on one day. Page 0 should be Today's date and the next page will be all appointments taking place tomorrow, and it continues based on the amount of pages requested.

If you request 3 pages you will get all the appointments from today upto the day after tomorrow, each day in its own page.

I'm using the following:

  • Spring Boot application
  • JPARepository
  • MySQL database

This is how my service implement looks:

public Page<Appointment> PageAppointments(int page, int size) throws RestException{

    Pageable limit = new PageRequest(page, size);
    return appointmentRepository.findAllByOrderByAppointmentDateDesc(limit);

}

This is how my repository looks:

public interface AppointmentRepository extends JpaRepository<Appointment, Long> {

    Page<Appointment> findAllByOrderByAppointmentDateDesc(Pageable pageable);

}

The result im looking for is something like this: (Conceptual)

//All Appointments taking place today
Page 1: {
    Appointment Entity with date 2017/07/22 11:19
    Appointment Entity with date 2017/07/22 14:08
    Appointment Entity with date 2017/07/22 15:21
    Appointment Entity with date 2017/07/22 16:44
}

//All Appointments taking place tomorrow
Page 2: {
    Appointment Entity with date 2017/07/23 11:19
    Appointment Entity with date 2017/07/23 14:08
    Appointment Entity with date 2017/07/23 15:21
    Appointment Entity with date 2017/07/23 15:34
    Appointment Entity with date 2017/07/23 16:11
    Appointment Entity with date 2017/07/23 16:44
}

//All Appointments taking place the day after tomorrow
Page 3: {
    Appointment Entity with date 2017/07/24 11:19
    Appointment Entity with date 2017/07/24 14:08
}

Then in this scenario if I request page 4 I'm supposed to get back all the appointments for 3 days from today which would be 2017/07/25 .

Any help will be greatly appreciated.

Thanks in advance.

1 Answers
Related