From fc88ca97c9f5b75bda9cf9cfac181fdbb91e2beb Mon Sep 17 00:00:00 2001 From: Jeonghyun Lee Date: Sat, 14 Sep 2024 16:18:56 +0900 Subject: [PATCH] feature: getProductOrderList api --- .../product/api/ProductOrderController.java | 7 ++++++ .../productOrder/GetProductOrderForUser.java | 23 +++++++++++++++++++ .../repository/ProductOrderRepository.java | 1 + .../product/service/ProductOrderService.java | 15 ++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 src/main/java/com/example/repick/domain/product/dto/productOrder/GetProductOrderForUser.java diff --git a/src/main/java/com/example/repick/domain/product/api/ProductOrderController.java b/src/main/java/com/example/repick/domain/product/api/ProductOrderController.java index fe2e57ee..b6890eb7 100644 --- a/src/main/java/com/example/repick/domain/product/api/ProductOrderController.java +++ b/src/main/java/com/example/repick/domain/product/api/ProductOrderController.java @@ -40,6 +40,13 @@ public SuccessResponse validateOrder(@RequestBody PostPayment postPayme return SuccessResponse.createSuccess(productOrderService.validatePayment(postPayment)); } + @Operation(summary = "주문 내역 리스트") + @GetMapping + public SuccessResponse>> getProductOrdersForUser(@ParameterObject PageCondition pageCondition) { + return SuccessResponse.success(productOrderService.getProductOrdersForUser(pageCondition)); + } + + @Operation(summary = "상품 구매 확정", description = """ 구매를 확정합니다. diff --git a/src/main/java/com/example/repick/domain/product/dto/productOrder/GetProductOrderForUser.java b/src/main/java/com/example/repick/domain/product/dto/productOrder/GetProductOrderForUser.java new file mode 100644 index 00000000..48149575 --- /dev/null +++ b/src/main/java/com/example/repick/domain/product/dto/productOrder/GetProductOrderForUser.java @@ -0,0 +1,23 @@ +package com.example.repick.domain.product.dto.productOrder; + +import com.example.repick.domain.product.entity.Product; +import com.example.repick.domain.product.entity.ProductOrder; +import io.swagger.v3.oas.annotations.media.Schema; + +public record GetProductOrderForUser( + @Schema(description = "주문 아이디") long productOrderId, + @Schema(description = "브랜드 이름") String brandName, + @Schema(description = "상품 이름") String productName, + @Schema(description = "상품 이미지 URL") String productImageUrl, + @Schema(description = "구매확정 여부") Boolean isConfirmed +) { + public static GetProductOrderForUser from(ProductOrder productOrder, Product product) { + return new GetProductOrderForUser( + productOrder.getId(), + product.getBrandName(), + product.getProductName(), + product.getThumbnailImageUrl(), + productOrder.isConfirmed() + ); + } +} diff --git a/src/main/java/com/example/repick/domain/product/repository/ProductOrderRepository.java b/src/main/java/com/example/repick/domain/product/repository/ProductOrderRepository.java index 1f352c9f..7b4ad204 100644 --- a/src/main/java/com/example/repick/domain/product/repository/ProductOrderRepository.java +++ b/src/main/java/com/example/repick/domain/product/repository/ProductOrderRepository.java @@ -17,6 +17,7 @@ public interface ProductOrderRepository extends JpaRepository findByIsConfirmed(boolean isConfirmed); List findByUserId(Long userId); + Page findByUserId(Long userId, Pageable pageable); Page findByProductOrderStateIn(List productOrderStates, Pageable pageable); diff --git a/src/main/java/com/example/repick/domain/product/service/ProductOrderService.java b/src/main/java/com/example/repick/domain/product/service/ProductOrderService.java index f3971fcf..5efc91b2 100644 --- a/src/main/java/com/example/repick/domain/product/service/ProductOrderService.java +++ b/src/main/java/com/example/repick/domain/product/service/ProductOrderService.java @@ -166,6 +166,21 @@ private void cancelPayment(Payment payment) { paymentRepository.save(payment); } + public PageResponse> getProductOrdersForUser(PageCondition pageCondition) { + User user = userRepository.findByProviderId(SecurityContextHolder.getContext().getAuthentication().getName()) + .orElseThrow(() -> new CustomException(USER_NOT_FOUND)); + Pageable pageable = pageCondition.toPageable(); + Page productOrderPage = productOrderRepository.findByUserId(user.getId(), pageable); + List getProductOrders = productOrderPage.stream() + .map(productOrder -> { + Product product = productRepository.findById(productOrder.getProductId()) + .orElseThrow(() -> new CustomException(INVALID_PRODUCT_ID)); + return GetProductOrderForUser.from(productOrder, product); + }).toList(); + Page page = new PageImpl<>(getProductOrders, pageable, productOrderPage.getTotalElements()); + return new PageResponse<>(page.getContent(), page.getTotalPages(), page.getTotalElements()); + } + @Transactional public Boolean confirmProductOrder(Long productOrderId){ ProductOrder productOrder = productOrderRepository.findById(productOrderId)