-
Notifications
You must be signed in to change notification settings - Fork 0
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
[47] 일반 사용자 주문 리스트 #50
Changes from 1 commit
700d0d8
d62f2a3
0e7668f
6ac59cb
7881ce6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.example.commerce_site.application.order.dto; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
|
||
import org.example.commerce_site.attribute.OrderStatus; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
public class OrderResponseDto { | ||
@Builder | ||
@Getter | ||
@AllArgsConstructor | ||
public static class Get { | ||
private Long id; | ||
private BigDecimal totalAmount; | ||
private OrderStatus status; | ||
private List<OrderDetailResponseDto.Get> orderDetails; | ||
|
||
public Get(Long id, BigDecimal totalAmount, OrderStatus status) { | ||
this.id = id; | ||
this.totalAmount = totalAmount; | ||
this.status = status; | ||
} | ||
|
||
public void setOrderDetails(List<OrderDetailResponseDto.Get> orderDetails) { | ||
this.orderDetails = orderDetails; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package org.example.commerce_site.common.util; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.springframework.data.domain.Page; | ||
|
@@ -8,7 +9,16 @@ | |
|
||
public class PageConverter { | ||
public static <T> Page<T> getPage(List<T> content, Pageable pageable) { | ||
if (content == null || pageable == null) { | ||
return new PageImpl<>(Collections.emptyList(), pageable != null ? pageable : Pageable.unpaged(), 0); | ||
} | ||
|
||
int start = (int)pageable.getOffset(); | ||
|
||
if (start >= content.size()) { | ||
return new PageImpl<>(Collections.emptyList(), pageable, content.size()); | ||
} | ||
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이건 어떤 경우를 처리하는거에요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 입력받은 페이지가 전체 페이지보다 클 경우(예: 총 3페이지인데 6페이지 보여달라고 요청 받았을때) 예외가 발생해, empty리스트를 반환하도록 했습니다 |
||
|
||
int end = Math.min((start + pageable.getPageSize()), content.size()); | ||
|
||
return new PageImpl<>(content.subList(start, end), pageable, content.size()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.example.commerce_site.infrastructure.order; | ||
|
||
import org.example.commerce_site.application.order.dto.OrderResponseDto; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface CustomOrderRepository { | ||
Page<OrderResponseDto.Get> getOrders(Pageable pageable, String keyword, Long userId); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.example.commerce_site.infrastructure.order; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.example.commerce_site.application.order.dto.OrderDetailResponseDto; | ||
import org.example.commerce_site.application.order.dto.OrderResponseDto; | ||
import org.example.commerce_site.common.util.PageConverter; | ||
import org.example.commerce_site.domain.QOrder; | ||
import org.example.commerce_site.domain.QOrderDetail; | ||
import org.example.commerce_site.domain.QProduct; | ||
import org.example.commerce_site.domain.QShipment; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.util.StringUtils; | ||
|
||
import com.querydsl.core.BooleanBuilder; | ||
import com.querydsl.core.types.Projections; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class CustomOrderRepositoryImpl implements CustomOrderRepository { | ||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public Page<OrderResponseDto.Get> getOrders(Pageable pageable, String keyword, Long userId) { | ||
BooleanBuilder builder = new BooleanBuilder(); | ||
|
||
if (StringUtils.hasText(keyword)) { | ||
builder.and(QProduct.product.name.contains(keyword)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 조건을 위한 index를 설정한다고 어떻게 하면 될까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fullText 인덱스로 설정하는 방법과 일반 like 인덱스로 설정하는 방법 두가지를 찾았습니다.
이런 이유로 FullText 인덱스로 설정하려고합니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FullText 만으로는 iphone16 이라는 상품을 검색할때 'iph' 만으로 찾아지지 않아 like 와 FullText 를 병행하여 사용하도록 수정했습니다. 쿼리 성능은 인덱스를 사용하지 않을때는 0.246 에서 0.121 로 빨라졌습니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fulltext를 사용하면 어떻게 이런것들이 가능한걸까요? |
||
} | ||
|
||
List<OrderResponseDto.Get> orderList = queryFactory.select(Projections.constructor(OrderResponseDto.Get.class, | ||
QOrder.order.id, | ||
QOrder.order.totalAmount, | ||
QOrder.order.status, | ||
Projections.list(Projections.constructor(OrderDetailResponseDto.Get.class, | ||
QOrderDetail.orderDetail.createdAt, | ||
QOrderDetail.orderDetail.id, | ||
QOrderDetail.orderDetail.productId, | ||
QOrderDetail.orderDetail.quantity, | ||
QOrderDetail.orderDetail.order.id, | ||
QOrderDetail.orderDetail.unitPrice, | ||
QProduct.product.name, | ||
QShipment.shipment.status, | ||
QShipment.shipment.createdAt, | ||
QShipment.shipment.updatedAt | ||
)) | ||
)) | ||
.from(QOrder.order) | ||
.leftJoin(QOrderDetail.orderDetail).on(QOrder.order.id.eq(QOrderDetail.orderDetail.order.id)) | ||
.leftJoin(QProduct.product).on(QOrderDetail.orderDetail.productId.eq(QProduct.product.id)) | ||
.leftJoin(QShipment.shipment).on(QOrderDetail.orderDetail.id.eq(QShipment.shipment.orderDetail.id)) | ||
.where(QOrder.order.userId.eq(userId).and(builder)) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 쿼리의 성능은 어느정도가 나올까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. EXPLAIN ANALYZE 를 돌려본 결과 검색어가 없는경우 0.246 검색어가 있는경우는 0.266 이 나왔습니다. |
||
|
||
List<OrderResponseDto.Get> groupedOrderList = orderList.stream() | ||
.collect(Collectors.groupingBy(OrderResponseDto.Get::getId)) | ||
.values() | ||
.stream() | ||
.map(orderGroup -> { | ||
OrderResponseDto.Get firstOrder = orderGroup.get(0); | ||
List<OrderDetailResponseDto.Get> allDetails = orderGroup.stream() | ||
.flatMap(order -> order.getOrderDetails().stream()) | ||
.collect(Collectors.toList()); | ||
firstOrder.setOrderDetails(allDetails); | ||
return firstOrder; | ||
}) | ||
.collect(Collectors.toList()); | ||
|
||
return PageConverter.getPage(groupedOrderList, pageable); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.example.commerce_site.representation.order.dto; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import org.example.commerce_site.application.order.dto.OrderDetailResponseDto; | ||
import org.example.commerce_site.application.order.dto.OrderResponseDto; | ||
import org.example.commerce_site.attribute.OrderStatus; | ||
import org.example.commerce_site.attribute.ShipmentStatus; | ||
import org.springframework.data.domain.Page; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
public class OrderResponse { | ||
@Getter | ||
@Builder | ||
public static class Get { | ||
private Long id; | ||
private BigDecimal totalAmount; | ||
private OrderStatus status; | ||
private List<DetailGet> orderDetails; | ||
|
||
public static Get of(OrderResponseDto.Get dto) { | ||
return Get.builder() | ||
.id(dto.getId()) | ||
.totalAmount(dto.getTotalAmount()) | ||
.status(dto.getStatus()) | ||
.orderDetails(DetailGet.of(dto.getOrderDetails())) | ||
.build(); | ||
} | ||
|
||
public static Page<Get> of(Page<OrderResponseDto.Get> dtos) { | ||
return dtos.map(Get::of); | ||
} | ||
} | ||
|
||
@Getter | ||
@Builder | ||
public static class DetailGet { | ||
private LocalDateTime createdAt; | ||
private Long id; | ||
private Long productId; | ||
private Long quantity; | ||
private Long orderId; | ||
private BigDecimal unitPrice; | ||
private String productName; | ||
private ShipmentStatus shipmentStatus; | ||
private LocalDateTime shipmentCreatedAt; | ||
private LocalDateTime shipmentUpdatedAt; | ||
|
||
public static DetailGet of(OrderDetailResponseDto.Get dto) { | ||
return DetailGet.builder() | ||
.createdAt(dto.getCreatedAt()) | ||
.id(dto.getId()) | ||
.productId(dto.getProductId()) | ||
.quantity(dto.getQuantity()) | ||
.orderId(dto.getOrderId()) | ||
.unitPrice(dto.getUnitPrice()) | ||
.productName(dto.getProductName()) | ||
.shipmentStatus(dto.getShipmentStatus()) | ||
.shipmentCreatedAt(dto.getShipmentCreatedAt()) | ||
.shipmentUpdatedAt(dto.getShipmentUpdatedAt()) | ||
.build(); | ||
} | ||
|
||
public static List<DetailGet> of(List<OrderDetailResponseDto.Get> dtos) { | ||
return dtos.stream().map(DetailGet::of).toList(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
content 전체를 받고 일부를 잘라서 사용하는 것 같은데, 전체 row의 갯수가 엄청 많아지면 무슨일이 생길까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
굳이 필요없는 데이터까지 가져오므로 많이 느려질것같네요.. 쿼리에서 limit 을 주고 Total 값을 얻는 쿼리를 한번 더 돌리는 방법으로 수정 하겠습니다..!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이부분 [48] 파트너회원 order list 브랜치에서 반영했습니다..!