Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✏️ full-scan 후 직접 slicing하는 방식으로 정렬 변경 #12

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
public class PickupReader {
private final PickupRepository pickupRepository;

public Page<Pickup> readByUserId(Long userId, Pageable pageable) {
return pickupRepository.findAllByUserIdOrderByPickupDateTimeDesc(userId, pageable);
public List<Pickup> readByUserId(Long userId) {
return pickupRepository.findAllByUserId(userId);
}
public Long userPickupCount() {
return pickupRepository.count();
}

public List<Pickup> readByStoreIdAndPickupDate(Long storeId, String pickupDate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
@EnableScan
public interface PickupRepository extends CrudRepository<Pickup, String> {
List<Pickup> findAllByStoreId(Long storeId);
Page<Pickup> findAllByUserIdOrderByPickupDateTimeDesc(Long userId, Pageable pageable);
List<Pickup> findAllByUserId(Long userId);
List<Pickup> findAllByStoreIdAndPickupDate(Long storeId, String pickupDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ public Pickup createPickup(StoreNameAndAddressDto storeAddress, PickupCreateDto



public Page<PickupsInMypageDto> getPickupsForUser(Long userId, Pageable pageable, LocalDate now) {
Page<Pickup> pickups = pickupReader.readByUserId(userId, pageable);
List<Pickup> contents = pickups.getContent();
List<Pickup> sortedPickups = sortAroundNow(contents, now);
public Page<PickupsInMypageDto> getPickupsForUser(Long userId, Pageable pageable, LocalDate today) {
List<Pickup> contents = pickupReader.readByUserId(userId);
List<Pickup> sortedPickups = sortAroundToday(contents, today);
List<Pickup> slicedPickups = sliceList(sortedPickups, pageable);
Long count = pickupReader.userPickupCount();

List<PickupsInMypageDto> pickupsInMyPageDtos = sortedPickups.stream()
List<PickupsInMypageDto> pickupsInMyPageDtos = slicedPickups.stream()
.map(PickupsInMypageDto::fromEntity)
.collect(Collectors.toList());
return new PageImpl<>(pickupsInMyPageDtos, pickups.getPageable(), pickups.getTotalElements());
return new PageImpl<>(pickupsInMyPageDtos, pageable, count);
}

public List<PickupsForDateDto> getPickupsForDate(Long storeId, String pickupDate) {
Expand Down Expand Up @@ -82,7 +83,7 @@ public void updateReservationStatus(String subscriptionId, String reservationSta
* 오늘이 13일이고 데이터가 [11,12,13,14,15,16]이라면
* [13,14,15,16,12,11]로 정렬됩니다
*/
private List<Pickup> sortAroundNow(List<Pickup> pickups, LocalDate now) {
private List<Pickup> sortAroundToday(List<Pickup> pickups, LocalDate now) {
Map<Boolean, List<Pickup>> collect = pickups.stream()
.collect(partitioningBy(pickup -> pickup.getPickupDateTime().toLocalDate().isBefore(now)));
List<Pickup> afterOrEqualFromNow = collect.get(false);
Expand All @@ -94,4 +95,11 @@ private List<Pickup> sortAroundNow(List<Pickup> pickups, LocalDate now) {
.collect(Collectors.toList());
}

private List<Pickup> sliceList(List<Pickup> contents, Pageable pageable) {
int start = (int) pageable.getOffset();
int end = Math.min((start + pageable.getPageSize()), contents.size());

return contents.subList(start,end);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -214,29 +214,6 @@ void getPickupReservationDetail() {
assertThat(pickupDetailDto.getPickupDate()).isEqualTo(pickup.getPickupDate());
}

@DisplayName("데이터를 페이징처리해 가져온다")
@Test
void getDataWithPaging() {
// given
Long userId = 1L;
Pickup p1 = createPickupWithUserId(userId);
Pickup p2 = createPickupWithUserId(userId);
Pickup p3 = createPickupWithUserId(userId);
Pickup p4 = createPickupWithUserId(userId);
Pickup p5 = createPickupWithUserId(userId);
Pickup p6 = createPickupWithUserId(userId);
Pickup p7 = createPickupWithUserId(userId);
pickupRepository.saveAll(List.of(p1,p2,p3,p4,p5,p6,p7));

// when
Pageable page = PageRequest.of(1,5);
Page<Pickup> result = pickupRepository.findAllByUserIdOrderByPickupDateTimeDesc(userId, page);

// then
assertThat(result.getContent().size()).isEqualTo(2);
assertThat(result.getTotalElements()).isEqualTo(7);
}

private PickupCreateDto createPickupCreateDto(String pickupReservationId) {
return PickupCreateDto.builder()
.pickupReservationId(pickupReservationId)
Expand Down
Loading