신상품과 인기상품을 조회하는데 총 5가지 정렬을 추가해야 했다! 더군다나.. 페이지네이션까지...
정렬 기준은 총 5가지였다!
1. 최신순
2. 가격 높은 순
3. 가격 낮은 순
4. 할인순
5. 인기순
QueryDSL을 도입하기 전에는 5가지의 정렬과 신상품이라는 조건을 모두 만족시키기 위해서 5개의 JPA 메서드를 이용했다.

인기 상품 조회는 더 끔찍하다..

서비스단은 다음과 같다!
// ItemService.java
@Transactional(readOnly = true)
public FindItemsResponse findNewItems(FindNewItemsCommand findNewItemsCommand) {
List<Item> items = findNewItemsFrom(findNewItemsCommand);
return FindItemsResponse.from(items);
}
private List<Item> findNewItemsFrom(FindNewItemsCommand findNewItemsCommand) {
LocalDateTime createdAt = LocalDateTime.now()
.minus(NEW_PRODUCT_REFERENCE_WEEK, ChronoUnit.WEEKS);
return switch (findNewItemsCommand.sortType()) {
case NEW ->
itemRepository.findByCreatedAtAfterAndItemIdLessThanOrderByCreatedAtDesc(createdAt,
findNewItemsCommand.lastIdx(), findNewItemsCommand.pageRequest());
case HIGHEST_AMOUNT ->
itemRepository.findByCreatedAtAfterAndPriceLessThanOrderByPriceDescItemIdDesc(
createdAt, findNewItemsCommand.lastIdx().intValue(),
findNewItemsCommand.pageRequest());
case LOWEST_AMOUNT ->
itemRepository.findByCreatedAtAfterAndPriceGreaterThanOrderByPriceAscItemIdDesc(
createdAt, findNewItemsCommand.lastIdx().intValue(),
findNewItemsCommand.pageRequest());
case DISCOUNT ->
itemRepository.findByCreatedAtAfterAndDiscountLessThanOrderByDiscountDescItemIdDesc(
createdAt, findNewItemsCommand.lastIdx().intValue(),
findNewItemsCommand.pageRequest());
default -> {
int lastIdx = findNewItemsCommand.lastIdx().intValue();
if (findNewItemsCommand.lastIdx() != Long.parseLong(
String.valueOf(Integer.MAX_VALUE))) {
lastIdx = orderItemRepository.countByOrderItemId(findNewItemsCommand.lastIdx()).intValue();
}
yield itemRepository.findNewItemOrderByOrders(createdAt, lastIdx,
findNewItemsCommand.pageRequest());
}
};
}
이제 이러한 것들을 QueryDSL을 이용하여 (나름) 깔끔하게 바꿔보자!
그 전에 QueryDSL을 사용하는 이유에 대해 먼저 알아보자!
여튼 이러한 이유들로 QueryDSL을 사용한다고 한다!