Open psy-choi opened 1 year ago
HastMap 인 형태를 어떻게 pagination을 할 것인지 고민이 많았습니다.
Reserve 도큐먼트에서 직접 가져오는 것이 아니라, 내부의 table을 기준으로 정리한 것이라 DB 쿼리 내부에서 할 수도 없었습니다. 그래서 HashMap으로 table을 구분하여 reserve를 하나씩 넣지 않고, LinkedHashMap을 활용하여 table에 따른 reserve를 정리했습니다. LinkedHashMap은 java에서 지원하는 순서가 있는 일종의 Map이라고 보시면 될 것 같습니다.
Map<String, List<Reserved>> reservedListByTabldIdWithPagination = new LinkedHashMap<>();
reservedRepository.findReservedByConditions(cafeId, reserveStartTime, reserveEndTime, peopleNumber, tableOptions).forEach(reserved ->
{
if (!reservedListByTabldIdWithPagination.containsKey(reserved.getTable().getId())){
reservedListByTabldIdWithPagination.put(reserved.getTable().getId(), new ArrayList<>());
}
reservedListByTabldIdWithPagination.get(reserved.getTable().getId()).add(reserved);
}
);
List<Map.Entry<String, List
또한, 이전에 가져왔던 reserved 들 또한 tableId 순서대로 정렬되어 있기 때문에 일관된 순서대로 table의 정보가 가져오기 때문에 순서가 바뀔 여지도 없습니다.
-ReservedRepositoryImpl.class
@Override
public List
MatchOperation matchToday = Aggregation.match(Criteria.where("startTime").gte(today.with(LocalTime.MIN)).lt(today.with(LocalTime.MAX)));
MatchOperation matchCafdId = Aggregation.match(Criteria.where("cafeId").is(cafeId));
SortOperation sortByTable = Aggregation.sort(Sort.Direction.ASC, "table._id").and(Sort.Direction.ASC, "startTime");
Aggregation aggregation = Aggregation.newAggregation(matchToday, matchCafdId, sortByTable);
return mongoTemplate.aggregate(aggregation, "reserved", Reserved.class).getMappedResults();
}
혹시 테스트용 더미 데이터를 좀 많이 요청 가능할까요? 각 컨트롤러별로 10 * 5 = 50개 정도 있다면 원활하게 테스트 가능할 것 같습니다
✏️ Description
Pagination이 적용되지 않았던 것들을 controller 단위에서 추가
🛠 Features
Warning