From ec8d1723b5fbbfd8e9f8bb6e6866ba17ce4dd167 Mon Sep 17 00:00:00 2001 From: subin Date: Sun, 14 Jul 2024 21:06:10 +0900 Subject: [PATCH 1/6] =?UTF-8?q?refactor=20:=20=EC=A3=BC=EB=AC=B8-=EC=A3=BC?= =?UTF-8?q?=EB=AC=B8=EC=83=81=EC=84=B8=20=EC=83=9D=EC=84=B1=20cascade=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refactor : 주문-주문상세 생성 cascade 적용 --- .../order/controller/OrderUserController.java | 4 +- .../store/clothstar/order/domain/Order.java | 9 +- .../clothstar/order/domain/OrderDetail.java | 4 + .../order/dto/reponse/OrderResponse.java | 1 - .../service/OrderApplicationService.java | 23 ---- .../order/service/OrderDetailService.java | 33 ----- .../clothstar/order/service/OrderService.java | 32 ++++- .../product/service/ProductService.java | 5 + .../service/ProductLineService.java | 4 + .../service/OrderDetailServiceTest.java | 52 +------ .../order/service/OrderServiceTest.java | 130 +++++++++++------- 11 files changed, 133 insertions(+), 164 deletions(-) delete mode 100644 src/main/java/org/store/clothstar/order/service/OrderApplicationService.java rename src/test/java/org/store/clothstar/{orderDetail => order}/service/OrderDetailServiceTest.java (82%) diff --git a/src/main/java/org/store/clothstar/order/controller/OrderUserController.java b/src/main/java/org/store/clothstar/order/controller/OrderUserController.java index 9c09e3c..aaec98c 100644 --- a/src/main/java/org/store/clothstar/order/controller/OrderUserController.java +++ b/src/main/java/org/store/clothstar/order/controller/OrderUserController.java @@ -15,7 +15,6 @@ import org.store.clothstar.common.dto.SaveResponseDTO; import org.store.clothstar.order.dto.reponse.OrderResponse; import org.store.clothstar.order.dto.request.OrderRequestWrapper; -import org.store.clothstar.order.service.OrderApplicationService; import org.store.clothstar.order.service.OrderService; @Tag(name = "Order", description = "주문(Order) 정보 관리에 대한 API 입니다.") @@ -25,7 +24,6 @@ public class OrderUserController { private final OrderService orderService; - private final OrderApplicationService orderApplicationService; @Operation(summary = "단일 주문 조회", description = "단일 주문의 정보를 조회한다.") @GetMapping("/{orderId}") @@ -53,7 +51,7 @@ public ResponseEntity> getAllOrderSlicePaging( @Operation(summary = "주문 생성", description = "단일 주문을 생성한다.") @PostMapping public ResponseEntity saveOrder(@RequestBody @Validated OrderRequestWrapper orderRequestWrapper) { - Long orderId = orderApplicationService.saveOrderWithTransaction(orderRequestWrapper); + Long orderId = orderService.saveOrder(orderRequestWrapper); return ResponseEntity.ok(new SaveResponseDTO( orderId, HttpStatus.OK.value(), "주문이 정상적으로 생성되었습니다.")); } diff --git a/src/main/java/org/store/clothstar/order/domain/Order.java b/src/main/java/org/store/clothstar/order/domain/Order.java index 285677f..8e18759 100644 --- a/src/main/java/org/store/clothstar/order/domain/Order.java +++ b/src/main/java/org/store/clothstar/order/domain/Order.java @@ -11,6 +11,7 @@ import org.store.clothstar.order.domain.vo.TotalPrice; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.List; @AllArgsConstructor @@ -42,7 +43,13 @@ public class Order extends BaseEntity { TotalPrice totalPrice; - + public void addOrderDetail(OrderDetail orderDetail) { + if (orderDetails == null) { + orderDetails = new ArrayList<>(); + } + orderDetails.add(orderDetail); + orderDetail.setterOrder(this); + } public void updateDeletedAt() { diff --git a/src/main/java/org/store/clothstar/order/domain/OrderDetail.java b/src/main/java/org/store/clothstar/order/domain/OrderDetail.java index 9102082..40b27d4 100644 --- a/src/main/java/org/store/clothstar/order/domain/OrderDetail.java +++ b/src/main/java/org/store/clothstar/order/domain/OrderDetail.java @@ -38,4 +38,8 @@ public class OrderDetail extends BaseEntity { public void updateDeletedAt() { this.deletedAt = LocalDateTime.now(); } + + public void setterOrder(Order order) { + this.order = order; + } } \ No newline at end of file diff --git a/src/main/java/org/store/clothstar/order/dto/reponse/OrderResponse.java b/src/main/java/org/store/clothstar/order/dto/reponse/OrderResponse.java index 5af26a8..7ebf1e6 100644 --- a/src/main/java/org/store/clothstar/order/dto/reponse/OrderResponse.java +++ b/src/main/java/org/store/clothstar/order/dto/reponse/OrderResponse.java @@ -12,7 +12,6 @@ import org.store.clothstar.order.domain.type.Status; import org.store.clothstar.order.domain.vo.AddressDTO; import org.store.clothstar.order.domain.vo.OrderDetailDTO; -import org.store.clothstar.order.domain.vo.Price; import org.store.clothstar.order.domain.vo.TotalPrice; import java.time.LocalDate; diff --git a/src/main/java/org/store/clothstar/order/service/OrderApplicationService.java b/src/main/java/org/store/clothstar/order/service/OrderApplicationService.java deleted file mode 100644 index 6b5e0e6..0000000 --- a/src/main/java/org/store/clothstar/order/service/OrderApplicationService.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.store.clothstar.order.service; - -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.store.clothstar.order.dto.request.OrderRequestWrapper; - -@Service -@RequiredArgsConstructor -public class OrderApplicationService { - private final OrderService orderService; - private final OrderDetailService orderDetailService; - - @Transactional - public Long saveOrderWithTransaction(OrderRequestWrapper orderRequestWrapper) { - - Long orderId = orderService.saveOrder(orderRequestWrapper.getCreateOrderRequest()); - - orderDetailService.saveOrderDetailWithOrder(orderRequestWrapper.getCreateOrderDetailRequest(), orderId); - - return orderId; - } -} diff --git a/src/main/java/org/store/clothstar/order/service/OrderDetailService.java b/src/main/java/org/store/clothstar/order/service/OrderDetailService.java index b7ef150..73ab763 100644 --- a/src/main/java/org/store/clothstar/order/service/OrderDetailService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderDetailService.java @@ -10,7 +10,6 @@ import org.store.clothstar.order.domain.type.Status; import org.store.clothstar.order.domain.OrderDetail; import org.store.clothstar.order.dto.request.AddOrderDetailRequest; -import org.store.clothstar.order.dto.request.CreateOrderDetailRequest; import org.store.clothstar.order.repository.order.OrderDetailRepository; import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.product.repository.ProductJPARepository; @@ -42,38 +41,6 @@ public OrderDetailService( this.productLineJPARepository = productLineJPARepository; } - // 주문 생성시 같이 호출되는 주문 상세 생성 메서드 - 하나의 트랜잭션으로 묶임 - @Transactional - public void saveOrderDetailWithOrder(CreateOrderDetailRequest createOrderDetailRequest, long orderId) { - - Order order = orderUserRepository.findById(orderId) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "주문 정보를 찾을 수 없습니다.")); - - ProductLineEntity productLineEntity = productLineJPARepository.findById(createOrderDetailRequest.getProductLineId()) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 옵션 정보를 찾을 수 없습니다.")); - - ProductEntity productEntity = productJPARepository.findById(createOrderDetailRequest.getProductId()) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 정보를 찾을 수 없습니다.")); - - // 주문상세 생성 유효성 검사: 주문 수량이 상품 재고보다 클 경우, 주문이 생성되지 않는다. - if (createOrderDetailRequest.getQuantity() > productEntity.getStock()) { - throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문 개수가 재고보다 더 많습니다."); - } - - OrderDetail orderDetail = createOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity); - orderDetailRepository.save(orderDetail); - - // 주문 정보 업데이트: 주문 상세 생성에 따른, 총 상품 금액과 총 결제 금액 업데이트 - int newTotalProductsPrice = order.getTotalPrice().getProducts() + orderDetail.getPrice().getOneKindTotalPrice(); - int newTotalPaymentPrice = - order.getTotalPrice().getProducts() + order.getTotalPrice().getShipping() + orderDetail.getPrice().getOneKindTotalPrice(); - - order.getTotalPrice().updatePrices(newTotalProductsPrice, newTotalPaymentPrice); - - // 주문 수량만큼 상품 재고 차감 - updateProductStock(productEntity, orderDetail.getQuantity()); - } - // 주문 상세 추가 생성 @Transactional public Long addOrderDetail(AddOrderDetailRequest addOrderDetailRequest) { diff --git a/src/main/java/org/store/clothstar/order/service/OrderService.java b/src/main/java/org/store/clothstar/order/service/OrderService.java index 8f4ab4c..c19dd67 100644 --- a/src/main/java/org/store/clothstar/order/service/OrderService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderService.java @@ -14,7 +14,9 @@ import org.store.clothstar.member.service.MemberService; import org.store.clothstar.order.domain.Order; import org.store.clothstar.order.dto.reponse.OrderResponse; +import org.store.clothstar.order.dto.request.CreateOrderDetailRequest; import org.store.clothstar.order.dto.request.CreateOrderRequest; +import org.store.clothstar.order.dto.request.OrderRequestWrapper; import org.store.clothstar.order.repository.order.OrderUserRepository; import org.store.clothstar.order.domain.type.Status; import org.store.clothstar.order.domain.OrderDetail; @@ -157,14 +159,42 @@ public Slice getAllOrderSlicePaging(Pageable pageable) { } @Transactional - public Long saveOrder(CreateOrderRequest createOrderRequest) { + public Long saveOrder(OrderRequestWrapper orderRequestWrapper) { + CreateOrderRequest createOrderRequest = orderRequestWrapper.getCreateOrderRequest(); + CreateOrderDetailRequest createOrderDetailRequest = orderRequestWrapper.getCreateOrderDetailRequest(); + // 주문 생성 Member member = memberService.getMemberByMemberId(createOrderRequest.getMemberId()); Address address = addressService.getAddressById(createOrderRequest.getAddressId()); Order order = createOrderRequest.toOrder(member, address); + + ProductLineEntity productLineEntity = productLineService.findById(createOrderDetailRequest.getProductLineId()) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품을 찾을 수 없습니다")); + ProductEntity productEntity = productService.findById(createOrderDetailRequest.getProductId()) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 옵션을 찾을 수 없습니다")); + + // 주문상세 생성 유효성 검사 + if (createOrderDetailRequest.getQuantity() > productEntity.getStock()) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문 개수가 재고보다 더 많습니다."); + } + + OrderDetail orderDetail = createOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity); + order.addOrderDetail(orderDetail); // 주문에 주문상세 추가 + + // 주문 저장 (orderDetail은 cascade 설정에 의해 자동 저장됨) orderUserRepository.save(order); + // 주문 정보 업데이트 + int newTotalProductsPrice = order.getTotalPrice().getProducts() + orderDetail.getPrice().getOneKindTotalPrice(); + int newTotalPaymentPrice = + order.getTotalPrice().getProducts() + order.getTotalPrice().getShipping() + orderDetail.getPrice().getOneKindTotalPrice(); + + order.getTotalPrice().updatePrices(newTotalProductsPrice, newTotalPaymentPrice); + + // 주문 수량만큼 상품 재고 차감 + orderDetailService.updateProductStock(productEntity, orderDetail.getQuantity()); + return order.getOrderId(); } diff --git a/src/main/java/org/store/clothstar/product/service/ProductService.java b/src/main/java/org/store/clothstar/product/service/ProductService.java index 3d2c1f5..579a1de 100644 --- a/src/main/java/org/store/clothstar/product/service/ProductService.java +++ b/src/main/java/org/store/clothstar/product/service/ProductService.java @@ -17,6 +17,7 @@ import org.store.clothstar.productLine.repository.ProductLineJPARepository; import java.util.List; +import java.util.Optional; @Service @RequiredArgsConstructor @@ -92,4 +93,8 @@ public void restoreProductStockByOrderDetail(OrderDetail orderDetail) { public List findByIdIn(List productIds) { return productJPARepository.findByIdIn(productIds); } + + public Optional findById(Long productId) { + return productRepository.findById(productId); + } } diff --git a/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java b/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java index 9be6aba..8a1b8b8 100644 --- a/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java +++ b/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java @@ -138,4 +138,8 @@ private ProductLineWithProductsJPAResponse convertToDtoWithProducts(ProductLineE public List findByIdIn(List productLineIds) { return productLineRepository.findByIdIn(productLineIds); } + + public Optional findById(Long productLineId) { + return productLineRepository.findById(productLineId); + } } \ No newline at end of file diff --git a/src/test/java/org/store/clothstar/orderDetail/service/OrderDetailServiceTest.java b/src/test/java/org/store/clothstar/order/service/OrderDetailServiceTest.java similarity index 82% rename from src/test/java/org/store/clothstar/orderDetail/service/OrderDetailServiceTest.java rename to src/test/java/org/store/clothstar/order/service/OrderDetailServiceTest.java index f9c2b40..59430b7 100644 --- a/src/test/java/org/store/clothstar/orderDetail/service/OrderDetailServiceTest.java +++ b/src/test/java/org/store/clothstar/order/service/OrderDetailServiceTest.java @@ -1,4 +1,4 @@ -package org.store.clothstar.orderDetail.service; +package org.store.clothstar.order.service; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,10 +12,8 @@ import org.store.clothstar.order.domain.vo.TotalPrice; import org.store.clothstar.order.repository.order.OrderUserRepository; import org.store.clothstar.order.domain.type.Status; -import org.store.clothstar.order.service.OrderDetailService; import org.store.clothstar.order.domain.OrderDetail; import org.store.clothstar.order.dto.request.AddOrderDetailRequest; -import org.store.clothstar.order.dto.request.CreateOrderDetailRequest; import org.store.clothstar.order.repository.order.OrderDetailRepository; import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.product.repository.ProductJPARepository; @@ -70,53 +68,6 @@ class OrderDetailServiceTest { @Mock private Price price; - @DisplayName("saveOrderDetailWithOrder: 주문상세 생성 - 메서드 호출 테스트") - @Test - void saveOrderDetailWithOrder_verify_test() { - //given - long orderId = 1L; - CreateOrderDetailRequest mockRequest = mock(CreateOrderDetailRequest.class); - - given(orderUserRepository.findById(orderId)).willReturn(Optional.of(order)); - given(productLineJPARepository.findById(mockRequest.getProductLineId())).willReturn(Optional.of(productLine)); - given(productJPARepository.findById(mockRequest.getProductId())).willReturn(Optional.of(product)); - given(order.getTotalPrice()).willReturn(totalPrice); - given(orderDetail.getPrice()).willReturn(price); - given(mockRequest.toOrderDetail(order, productLine, product)).willReturn(orderDetail); - - //when - orderDetailService.saveOrderDetailWithOrder(mockRequest, orderId); - - //then - then(orderUserRepository).should(times(1)).findById(orderId); - then(productLineJPARepository).should(times(1)).findById(mockRequest.getProductLineId()); - then(productJPARepository).should(times(1)).findById(mockRequest.getProductId()); - then(orderDetailRepository).should(times(1)).save(orderDetail); - } - - @DisplayName("saveOrderDetailWithOrder: 주문상세 생성 - 주문 수량이 상품 재고보다 클 때 예외처리 테스트") - @Test - void saveOrderDetailWithOrder_exception_test() { - //given - long orderId = 1L; - CreateOrderDetailRequest mockRequest = mock(CreateOrderDetailRequest.class); - ProductLineEntity mockProductLine = mock(ProductLineEntity.class); - ProductEntity mockProduct = mock(ProductEntity.class); - - given(orderUserRepository.findById(orderId)).willReturn(Optional.of(order)); - given(productLineJPARepository.findById(mockRequest.getProductLineId())).willReturn(Optional.of(mockProductLine)); - given(productJPARepository.findById(mockRequest.getProductId())).willReturn(Optional.of(mockProduct)); - given(mockRequest.getQuantity()).willReturn(10); - given(mockProduct.getStock()).willReturn(1L); - - //when - ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> - orderDetailService.saveOrderDetailWithOrder(mockRequest, orderId)); - - //then - assertEquals("400 BAD_REQUEST \"주문 개수가 재고보다 더 많습니다.\"", thrown.getMessage()); - } - @DisplayName("addOrderDetail: 주문상세 추가 - 주문 유효성 검사 예외처리 테스트") @Test void getOrderDetail_quantityZero_exception_test() { @@ -139,7 +90,6 @@ void getOrderDetail_quantityZero_exception_test() { assertEquals("400 BAD_REQUEST \"주문 개수가 재고보다 더 많습니다.\"", thrown.getMessage()); } - @DisplayName("addOrderDetail: 주문상세 추가 - 반환값 테스트") @Test void addOrderDetail_test() { diff --git a/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java b/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java index 454e308..6783a5b 100644 --- a/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java +++ b/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java @@ -21,8 +21,6 @@ import org.store.clothstar.order.domain.vo.Price; import org.store.clothstar.order.domain.vo.TotalPrice; import org.store.clothstar.order.dto.reponse.OrderResponse; -import org.store.clothstar.order.dto.request.CreateOrderRequest; -import org.store.clothstar.order.dto.request.OrderRequestWrapper; import org.store.clothstar.order.repository.order.OrderUserRepository; import org.store.clothstar.order.domain.type.Status; import org.store.clothstar.order.domain.OrderDetail; @@ -252,55 +250,85 @@ void getAllOrderSlicePaging_verify_test() { verify(productLineService, times(1)).findByIdIn(List.of(productLineId)); } - @Test - @DisplayName("saveOrder: 주문 생성 - 메서드 호출 테스트") - void saveOrder_verify_test() { - //given - OrderRequestWrapper orderRequestWrapper = mock(OrderRequestWrapper.class); - CreateOrderRequest createOrderRequest = mock(CreateOrderRequest.class); - - given(orderRequestWrapper.getCreateOrderRequest()).willReturn(createOrderRequest); - given(createOrderRequest.getMemberId()).willReturn(1L); - given(createOrderRequest.getAddressId()).willReturn(2L); - - given(memberService.getMemberByMemberId(createOrderRequest.getMemberId())).willReturn(member); - given(addressService.getAddressById(createOrderRequest.getAddressId())).willReturn(address); - given(createOrderRequest.toOrder(member, address)).willReturn(order); - - //when - orderService.saveOrder(orderRequestWrapper.getCreateOrderRequest()); - - //then - then(memberService).should(times(1)).getMemberByMemberId(createOrderRequest.getMemberId()); - then(addressService).should(times(1)).getAddressById(createOrderRequest.getAddressId()); - then(orderUserRepository).should(times(1)).save(order); - verify(order).getOrderId(); - } - - @Test - @DisplayName("saveOrder: 주문 생성 - 반환값 테스트") - void saveOrder_test() { - //given - OrderRequestWrapper orderRequestWrapper = mock(OrderRequestWrapper.class); - CreateOrderRequest createOrderRequest = mock(CreateOrderRequest.class); - - given(order.getOrderId()).willReturn(1L); - - given(orderRequestWrapper.getCreateOrderRequest()).willReturn(createOrderRequest); - - given(createOrderRequest.getMemberId()).willReturn(1L); - given(createOrderRequest.getAddressId()).willReturn(2L); - - given(memberService.getMemberByMemberId(1L)).willReturn(member); - given(addressService.getAddressById(2L)).willReturn(address); - given(createOrderRequest.toOrder(member, address)).willReturn(order); - - //when - Long orderId = orderService.saveOrder(orderRequestWrapper.getCreateOrderRequest()); - - //then - assertThat(orderId).isEqualTo(1L); - } +// @Test +// @DisplayName("saveOrder: 주문 생성 - 메서드 호출 테스트") +// void saveOrder_verify_test() { +// //given +// OrderRequestWrapper orderRequestWrapper = mock(OrderRequestWrapper.class); +// CreateOrderRequest createOrderRequest = mock(CreateOrderRequest.class); +// CreateOrderDetailRequest createOrderDetailRequest = mock(CreateOrderDetailRequest.class); +// +// given(orderRequestWrapper.getCreateOrderRequest()).willReturn(createOrderRequest); +// given(orderRequestWrapper.getCreateOrderDetailRequest()).willReturn(createOrderDetailRequest); +// given(createOrderRequest.getMemberId()).willReturn(1L); +// given(createOrderRequest.getAddressId()).willReturn(2L); +// given(address.getAddressInfo()).willReturn(addressInfo); +// given(order.getTotalPrice()).willReturn(totalPrice); +// given(order.getCreatedAt()).willReturn(LocalDateTime.now()); +// given(orderDetail.getPrice()).willReturn(price); +// given(productLineEntity.getSeller()).willReturn(seller); +// given(memberService.getMemberByMemberId(1L)).willReturn(member); +// given(addressService.getAddressById(2L)).willReturn(address); +// +// +// given(createOrderRequest.toOrder(member, address)).willReturn(order); +// +// given(createOrderDetailRequest.getProductLineId()).willReturn(3L); +// given(createOrderDetailRequest.getProductId()).willReturn(4L); +// given(productLineService.findById(3L)).willReturn(Optional.of(productLineEntity)); +// given(productService.findById(4L)).willReturn(Optional.of(productEntity)); +// +// given(createOrderDetailRequest.getQuantity()).willReturn(5); +// given(productEntity.getStock()).willReturn(10L); +// +// given(createOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity)).willReturn(orderDetail); +// +// // when +// orderService.saveOrder(orderRequestWrapper); +// +// // then +// then(memberService).should(times(1)).getMemberByMemberId(createOrderRequest.getMemberId()); +// then(addressService).should(times(1)).getAddressById(createOrderRequest.getAddressId()); +// then(productLineService).should(times(1)).findById(createOrderDetailRequest.getProductLineId()); +// then(productService).should(times(1)).findById(createOrderDetailRequest.getProductId()); +// then(orderUserRepository).should(times(1)).save(order); +// then(orderDetailRepository).should(times(1)).save(orderDetail); +// verify(order).getOrderId(); +// +// } +// +// @Test +// @DisplayName("saveOrder: 주문 생성 - 반환값 테스트") +// void saveOrder_test() { +// // given +// OrderRequestWrapper orderRequestWrapper = mock(OrderRequestWrapper.class); +// CreateOrderRequest createOrderRequest = mock(CreateOrderRequest.class); +// CreateOrderDetailRequest createOrderDetailRequest = mock(CreateOrderDetailRequest.class); +// +// given(order.getOrderId()).willReturn(1L); +// +// given(orderRequestWrapper.getCreateOrderRequest()).willReturn(createOrderRequest); +// given(orderRequestWrapper.getCreateOrderDetailRequest()).willReturn(createOrderDetailRequest); +// +// given(createOrderRequest.getMemberId()).willReturn(1L); +// given(createOrderRequest.getAddressId()).willReturn(2L); +// given(createOrderDetailRequest.getProductLineId()).willReturn(3L); +// given(createOrderDetailRequest.getProductId()).willReturn(4L); +// given(createOrderDetailRequest.getQuantity()).willReturn(0); +// +// given(memberService.getMemberByMemberId(1L)).willReturn(member); +// given(addressService.getAddressById(2L)).willReturn(address); +// given(productLineService.findById(3L)).willReturn(Optional.of(productLineEntity)); +// given(productService.findById(4L)).willReturn(Optional.of(productEntity)); +// given(createOrderRequest.toOrder(member, address)).willReturn(order); +// given(createOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity)).willReturn(orderDetail); +// +// // when +// Long orderId = orderService.saveOrder(orderRequestWrapper); +// +// // then +// assertThat(orderId).isEqualTo(1L); +// } @Test @DisplayName("confirmOrder: 구매 확정 - 성공 메서드 호출 테스트") From d5ea82ee456f7cebc6cc811b28194f5881af6450 Mon Sep 17 00:00:00 2001 From: subin Date: Sun, 14 Jul 2024 21:28:57 +0900 Subject: [PATCH 2/6] =?UTF-8?q?refactor=20:=20=EC=A3=BC=EB=AC=B8=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EB=A1=9C=EC=A7=81=EC=97=90=EC=84=9C=20findById=20-?= =?UTF-8?q?>=20findByOrderIdAndDeletedAtIsNull=EB=A1=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order/repository/order/JpaOrderUserRepository.java | 4 ++++ .../order/repository/order/OrderUserRepository.java | 2 ++ .../org/store/clothstar/order/service/OrderService.java | 6 +----- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/store/clothstar/order/repository/order/JpaOrderUserRepository.java b/src/main/java/org/store/clothstar/order/repository/order/JpaOrderUserRepository.java index 38f39cd..04eb571 100644 --- a/src/main/java/org/store/clothstar/order/repository/order/JpaOrderUserRepository.java +++ b/src/main/java/org/store/clothstar/order/repository/order/JpaOrderUserRepository.java @@ -7,8 +7,12 @@ import org.springframework.transaction.annotation.Transactional; import org.store.clothstar.order.domain.Order; +import java.util.Optional; + public interface JpaOrderUserRepository extends JpaRepository, OrderUserRepository { + Optional findByOrderIdAndDeletedAtIsNull(Long orderId); + @Transactional @Modifying @Query("UPDATE orders o SET o.status ='CONFIRM' WHERE o.orderId = :orderId") diff --git a/src/main/java/org/store/clothstar/order/repository/order/OrderUserRepository.java b/src/main/java/org/store/clothstar/order/repository/order/OrderUserRepository.java index 938e322..98409aa 100644 --- a/src/main/java/org/store/clothstar/order/repository/order/OrderUserRepository.java +++ b/src/main/java/org/store/clothstar/order/repository/order/OrderUserRepository.java @@ -10,6 +10,8 @@ public interface OrderUserRepository { Optional findById(Long orderId); + Optional findByOrderIdAndDeletedAtIsNull(Long orderId); + Page findAll(Pageable pageable); Order save(Order order); diff --git a/src/main/java/org/store/clothstar/order/service/OrderService.java b/src/main/java/org/store/clothstar/order/service/OrderService.java index c19dd67..312f4b3 100644 --- a/src/main/java/org/store/clothstar/order/service/OrderService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderService.java @@ -59,13 +59,9 @@ public OrderService( @Transactional(readOnly = true) public OrderResponse getOrder(Long orderId) { - Order order = orderUserRepository.findById(orderId) + Order order = orderUserRepository.findByOrderIdAndDeletedAtIsNull(orderId) .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "주문을 찾을 수 없습니다")); - if (order.getDeletedAt() != null) { - throw new ResponseStatusException(HttpStatus.NOT_FOUND, "삭제된 주문입니다."); - } - Member member = memberService.getMemberByMemberId(order.getMemberId()); Address address = addressService.getAddressById(order.getAddressId()); From e5a9f45d4c4d22e13eddca09b8d8c545b6f41183 Mon Sep 17 00:00:00 2001 From: subin Date: Mon, 15 Jul 2024 12:44:33 +0900 Subject: [PATCH 3/6] =?UTF-8?q?refactor=20:=20saveOrder=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order/service/OrderServiceTest.java | 132 +++++++----------- 1 file changed, 51 insertions(+), 81 deletions(-) diff --git a/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java b/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java index 6783a5b..ab82786 100644 --- a/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java +++ b/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java @@ -21,6 +21,9 @@ import org.store.clothstar.order.domain.vo.Price; import org.store.clothstar.order.domain.vo.TotalPrice; import org.store.clothstar.order.dto.reponse.OrderResponse; +import org.store.clothstar.order.dto.request.CreateOrderDetailRequest; +import org.store.clothstar.order.dto.request.CreateOrderRequest; +import org.store.clothstar.order.dto.request.OrderRequestWrapper; import org.store.clothstar.order.repository.order.OrderUserRepository; import org.store.clothstar.order.domain.type.Status; import org.store.clothstar.order.domain.OrderDetail; @@ -47,6 +50,9 @@ class OrderServiceTest { @InjectMocks private OrderService orderService; + @Mock + private OrderDetailService orderDetailService; + @Mock private MemberService memberService; @@ -105,7 +111,7 @@ void getOrder_test() { Long productId = 4L; Long productLineId = 5L; - given(orderUserRepository.findById(orderId)).willReturn(Optional.of(order)); + given(orderUserRepository.findByOrderIdAndDeletedAtIsNull(orderId)).willReturn(Optional.of(order)); given(order.getMemberId()).willReturn(memberId); given(order.getAddressId()).willReturn(addressId); given(order.getCreatedAt()).willReturn(LocalDateTime.now()); @@ -134,7 +140,7 @@ void getOrder_test() { // then assertThat(orderResponse).usingRecursiveComparison().isEqualTo(expectedOrderResponse); - then(orderUserRepository).should(times(1)).findById(orderId); + then(orderUserRepository).should(times(1)).findByOrderIdAndDeletedAtIsNull(orderId); then(memberService).should(times(1)).getMemberByMemberId(memberId); then(addressService).should(times(1)).getAddressById(addressId); then(productService).should(times(1)).findByIdIn(List.of(productId)); @@ -250,85 +256,49 @@ void getAllOrderSlicePaging_verify_test() { verify(productLineService, times(1)).findByIdIn(List.of(productLineId)); } -// @Test -// @DisplayName("saveOrder: 주문 생성 - 메서드 호출 테스트") -// void saveOrder_verify_test() { -// //given -// OrderRequestWrapper orderRequestWrapper = mock(OrderRequestWrapper.class); -// CreateOrderRequest createOrderRequest = mock(CreateOrderRequest.class); -// CreateOrderDetailRequest createOrderDetailRequest = mock(CreateOrderDetailRequest.class); -// -// given(orderRequestWrapper.getCreateOrderRequest()).willReturn(createOrderRequest); -// given(orderRequestWrapper.getCreateOrderDetailRequest()).willReturn(createOrderDetailRequest); -// given(createOrderRequest.getMemberId()).willReturn(1L); -// given(createOrderRequest.getAddressId()).willReturn(2L); -// given(address.getAddressInfo()).willReturn(addressInfo); -// given(order.getTotalPrice()).willReturn(totalPrice); -// given(order.getCreatedAt()).willReturn(LocalDateTime.now()); -// given(orderDetail.getPrice()).willReturn(price); -// given(productLineEntity.getSeller()).willReturn(seller); -// given(memberService.getMemberByMemberId(1L)).willReturn(member); -// given(addressService.getAddressById(2L)).willReturn(address); -// -// -// given(createOrderRequest.toOrder(member, address)).willReturn(order); -// -// given(createOrderDetailRequest.getProductLineId()).willReturn(3L); -// given(createOrderDetailRequest.getProductId()).willReturn(4L); -// given(productLineService.findById(3L)).willReturn(Optional.of(productLineEntity)); -// given(productService.findById(4L)).willReturn(Optional.of(productEntity)); -// -// given(createOrderDetailRequest.getQuantity()).willReturn(5); -// given(productEntity.getStock()).willReturn(10L); -// -// given(createOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity)).willReturn(orderDetail); -// -// // when -// orderService.saveOrder(orderRequestWrapper); -// -// // then -// then(memberService).should(times(1)).getMemberByMemberId(createOrderRequest.getMemberId()); -// then(addressService).should(times(1)).getAddressById(createOrderRequest.getAddressId()); -// then(productLineService).should(times(1)).findById(createOrderDetailRequest.getProductLineId()); -// then(productService).should(times(1)).findById(createOrderDetailRequest.getProductId()); -// then(orderUserRepository).should(times(1)).save(order); -// then(orderDetailRepository).should(times(1)).save(orderDetail); -// verify(order).getOrderId(); -// -// } -// -// @Test -// @DisplayName("saveOrder: 주문 생성 - 반환값 테스트") -// void saveOrder_test() { -// // given -// OrderRequestWrapper orderRequestWrapper = mock(OrderRequestWrapper.class); -// CreateOrderRequest createOrderRequest = mock(CreateOrderRequest.class); -// CreateOrderDetailRequest createOrderDetailRequest = mock(CreateOrderDetailRequest.class); -// -// given(order.getOrderId()).willReturn(1L); -// -// given(orderRequestWrapper.getCreateOrderRequest()).willReturn(createOrderRequest); -// given(orderRequestWrapper.getCreateOrderDetailRequest()).willReturn(createOrderDetailRequest); -// -// given(createOrderRequest.getMemberId()).willReturn(1L); -// given(createOrderRequest.getAddressId()).willReturn(2L); -// given(createOrderDetailRequest.getProductLineId()).willReturn(3L); -// given(createOrderDetailRequest.getProductId()).willReturn(4L); -// given(createOrderDetailRequest.getQuantity()).willReturn(0); -// -// given(memberService.getMemberByMemberId(1L)).willReturn(member); -// given(addressService.getAddressById(2L)).willReturn(address); -// given(productLineService.findById(3L)).willReturn(Optional.of(productLineEntity)); -// given(productService.findById(4L)).willReturn(Optional.of(productEntity)); -// given(createOrderRequest.toOrder(member, address)).willReturn(order); -// given(createOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity)).willReturn(orderDetail); -// -// // when -// Long orderId = orderService.saveOrder(orderRequestWrapper); -// -// // then -// assertThat(orderId).isEqualTo(1L); -// } + @Test + @DisplayName("saveOrder: 주문 생성 - 메서드 호출 & 반환값 테스트") + void saveOrder_verify_test() { + //given + OrderRequestWrapper orderRequestWrapper = mock(OrderRequestWrapper.class); + CreateOrderRequest createOrderRequest = mock(CreateOrderRequest.class); + CreateOrderDetailRequest createOrderDetailRequest = mock(CreateOrderDetailRequest.class); + + given(order.getOrderId()).willReturn(1L); + + given(orderRequestWrapper.getCreateOrderRequest()).willReturn(createOrderRequest); + given(orderRequestWrapper.getCreateOrderDetailRequest()).willReturn(createOrderDetailRequest); + given(createOrderRequest.getMemberId()).willReturn(1L); + given(createOrderRequest.getAddressId()).willReturn(2L); + given(memberService.getMemberByMemberId(1L)).willReturn(member); + given(addressService.getAddressById(2L)).willReturn(address); + given(createOrderRequest.toOrder(member,address)).willReturn(order); + + given(order.getTotalPrice()).willReturn(totalPrice); + given(orderDetail.getPrice()).willReturn(price); + + given(createOrderDetailRequest.getProductLineId()).willReturn(3L); + given(createOrderDetailRequest.getProductId()).willReturn(4L); + given(productLineService.findById(3L)).willReturn(Optional.of(productLineEntity)); + given(productService.findById(4L)).willReturn(Optional.of(productEntity)); + + given(createOrderDetailRequest.getQuantity()).willReturn(5); + given(productEntity.getStock()).willReturn(10L); + + given(createOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity)).willReturn(orderDetail); + + // when + Long orderId = orderService.saveOrder(orderRequestWrapper); + + // then + then(memberService).should(times(1)).getMemberByMemberId(createOrderRequest.getMemberId()); + then(addressService).should(times(1)).getAddressById(createOrderRequest.getAddressId()); + then(productLineService).should(times(1)).findById(createOrderDetailRequest.getProductLineId()); + then(productService).should(times(1)).findById(createOrderDetailRequest.getProductId()); + then(orderUserRepository).should(times(1)).save(order); + verify(order).getOrderId(); + assertThat(orderId).isEqualTo(1L); + } @Test @DisplayName("confirmOrder: 구매 확정 - 성공 메서드 호출 테스트") From 32c7782ba5dca11d7208c546fa32f896fe6273cc Mon Sep 17 00:00:00 2001 From: subin Date: Mon, 15 Jul 2024 12:45:16 +0900 Subject: [PATCH 4/6] refactor : Optimize Imports --- .../store/clothstar/order/service/OrderDetailService.java | 4 ++-- .../store/clothstar/order/service/OrderSellerService.java | 6 +++--- .../org/store/clothstar/order/service/OrderService.java | 8 ++++---- .../clothstar/order/service/OrderDetailServiceTest.java | 6 +++--- .../clothstar/order/service/OrderSellerServiceTest.java | 6 +++--- .../store/clothstar/order/service/OrderServiceTest.java | 8 ++++---- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/store/clothstar/order/service/OrderDetailService.java b/src/main/java/org/store/clothstar/order/service/OrderDetailService.java index 73ab763..1e623c4 100644 --- a/src/main/java/org/store/clothstar/order/service/OrderDetailService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderDetailService.java @@ -6,11 +6,11 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.server.ResponseStatusException; import org.store.clothstar.order.domain.Order; -import org.store.clothstar.order.repository.order.OrderUserRepository; -import org.store.clothstar.order.domain.type.Status; import org.store.clothstar.order.domain.OrderDetail; +import org.store.clothstar.order.domain.type.Status; import org.store.clothstar.order.dto.request.AddOrderDetailRequest; import org.store.clothstar.order.repository.order.OrderDetailRepository; +import org.store.clothstar.order.repository.order.OrderUserRepository; import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.product.repository.ProductJPARepository; import org.store.clothstar.product.service.ProductService; diff --git a/src/main/java/org/store/clothstar/order/service/OrderSellerService.java b/src/main/java/org/store/clothstar/order/service/OrderSellerService.java index 6e66fba..27a0827 100644 --- a/src/main/java/org/store/clothstar/order/service/OrderSellerService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderSellerService.java @@ -10,12 +10,12 @@ import org.store.clothstar.member.service.AddressService; import org.store.clothstar.member.service.MemberService; import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.OrderDetail; +import org.store.clothstar.order.domain.type.Status; +import org.store.clothstar.order.domain.vo.OrderDetailDTO; import org.store.clothstar.order.dto.reponse.OrderResponse; import org.store.clothstar.order.repository.order.OrderUserRepository; import org.store.clothstar.order.repository.orderSeller.OrderSellerRepository; -import org.store.clothstar.order.domain.type.Status; -import org.store.clothstar.order.domain.OrderDetail; -import org.store.clothstar.order.domain.vo.OrderDetailDTO; import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.product.service.ProductService; import org.store.clothstar.productLine.entity.ProductLineEntity; diff --git a/src/main/java/org/store/clothstar/order/service/OrderService.java b/src/main/java/org/store/clothstar/order/service/OrderService.java index 312f4b3..95d5098 100644 --- a/src/main/java/org/store/clothstar/order/service/OrderService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderService.java @@ -13,15 +13,15 @@ import org.store.clothstar.member.service.AddressService; import org.store.clothstar.member.service.MemberService; import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.OrderDetail; +import org.store.clothstar.order.domain.type.Status; +import org.store.clothstar.order.domain.vo.OrderDetailDTO; import org.store.clothstar.order.dto.reponse.OrderResponse; import org.store.clothstar.order.dto.request.CreateOrderDetailRequest; import org.store.clothstar.order.dto.request.CreateOrderRequest; import org.store.clothstar.order.dto.request.OrderRequestWrapper; -import org.store.clothstar.order.repository.order.OrderUserRepository; -import org.store.clothstar.order.domain.type.Status; -import org.store.clothstar.order.domain.OrderDetail; -import org.store.clothstar.order.domain.vo.OrderDetailDTO; import org.store.clothstar.order.repository.order.OrderDetailRepository; +import org.store.clothstar.order.repository.order.OrderUserRepository; import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.product.service.ProductService; import org.store.clothstar.productLine.entity.ProductLineEntity; diff --git a/src/test/java/org/store/clothstar/order/service/OrderDetailServiceTest.java b/src/test/java/org/store/clothstar/order/service/OrderDetailServiceTest.java index 59430b7..7c14c76 100644 --- a/src/test/java/org/store/clothstar/order/service/OrderDetailServiceTest.java +++ b/src/test/java/org/store/clothstar/order/service/OrderDetailServiceTest.java @@ -8,13 +8,13 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.web.server.ResponseStatusException; import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.OrderDetail; +import org.store.clothstar.order.domain.type.Status; import org.store.clothstar.order.domain.vo.Price; import org.store.clothstar.order.domain.vo.TotalPrice; -import org.store.clothstar.order.repository.order.OrderUserRepository; -import org.store.clothstar.order.domain.type.Status; -import org.store.clothstar.order.domain.OrderDetail; import org.store.clothstar.order.dto.request.AddOrderDetailRequest; import org.store.clothstar.order.repository.order.OrderDetailRepository; +import org.store.clothstar.order.repository.order.OrderUserRepository; import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.product.repository.ProductJPARepository; import org.store.clothstar.product.service.ProductService; diff --git a/src/test/java/org/store/clothstar/order/service/OrderSellerServiceTest.java b/src/test/java/org/store/clothstar/order/service/OrderSellerServiceTest.java index 5852384..88afe22 100644 --- a/src/test/java/org/store/clothstar/order/service/OrderSellerServiceTest.java +++ b/src/test/java/org/store/clothstar/order/service/OrderSellerServiceTest.java @@ -17,14 +17,14 @@ import org.store.clothstar.member.service.AddressService; import org.store.clothstar.member.service.MemberService; import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.OrderDetail; +import org.store.clothstar.order.domain.type.Status; +import org.store.clothstar.order.domain.vo.OrderDetailDTO; import org.store.clothstar.order.domain.vo.Price; import org.store.clothstar.order.domain.vo.TotalPrice; import org.store.clothstar.order.dto.reponse.OrderResponse; import org.store.clothstar.order.repository.order.OrderUserRepository; import org.store.clothstar.order.repository.orderSeller.OrderSellerRepository; -import org.store.clothstar.order.domain.type.Status; -import org.store.clothstar.order.domain.OrderDetail; -import org.store.clothstar.order.domain.vo.OrderDetailDTO; import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.product.service.ProductService; import org.store.clothstar.productLine.entity.ProductLineEntity; diff --git a/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java b/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java index ab82786..b2ec632 100644 --- a/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java +++ b/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java @@ -18,17 +18,17 @@ import org.store.clothstar.member.service.AddressService; import org.store.clothstar.member.service.MemberService; import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.OrderDetail; +import org.store.clothstar.order.domain.type.Status; +import org.store.clothstar.order.domain.vo.OrderDetailDTO; import org.store.clothstar.order.domain.vo.Price; import org.store.clothstar.order.domain.vo.TotalPrice; import org.store.clothstar.order.dto.reponse.OrderResponse; import org.store.clothstar.order.dto.request.CreateOrderDetailRequest; import org.store.clothstar.order.dto.request.CreateOrderRequest; import org.store.clothstar.order.dto.request.OrderRequestWrapper; -import org.store.clothstar.order.repository.order.OrderUserRepository; -import org.store.clothstar.order.domain.type.Status; -import org.store.clothstar.order.domain.OrderDetail; -import org.store.clothstar.order.domain.vo.OrderDetailDTO; import org.store.clothstar.order.repository.order.OrderDetailRepository; +import org.store.clothstar.order.repository.order.OrderUserRepository; import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.product.service.ProductService; import org.store.clothstar.productLine.entity.ProductLineEntity; From c0c452fb8fb37c882478f33f581800f0435b190e Mon Sep 17 00:00:00 2001 From: subin Date: Mon, 15 Jul 2024 13:51:01 +0900 Subject: [PATCH 5/6] =?UTF-8?q?refactor=20:=20=ED=8C=90=EB=A7=A4=EC=9E=90?= =?UTF-8?q?=20=EC=A3=BC=EB=AC=B8=EC=A1=B0=ED=9A=8C=20JPQL=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20&=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../orderSeller/JpaOrderSellerRepository.java | 2 +- .../order/service/OrderSellerService.java | 1 - .../clothstar/order/utils/URIBuilder.java | 16 ---------- .../order/service/OrderSellerServiceTest.java | 8 ++--- .../order/service/OrderServiceTest.java | 31 +++++++++++++++++++ 5 files changed, 36 insertions(+), 22 deletions(-) delete mode 100644 src/main/java/org/store/clothstar/order/utils/URIBuilder.java diff --git a/src/main/java/org/store/clothstar/order/repository/orderSeller/JpaOrderSellerRepository.java b/src/main/java/org/store/clothstar/order/repository/orderSeller/JpaOrderSellerRepository.java index 4718dbf..726f4e0 100644 --- a/src/main/java/org/store/clothstar/order/repository/orderSeller/JpaOrderSellerRepository.java +++ b/src/main/java/org/store/clothstar/order/repository/orderSeller/JpaOrderSellerRepository.java @@ -11,7 +11,7 @@ public interface JpaOrderSellerRepository extends JpaRepository, OrderSellerRepository { - @Query("SELECT o FROM orders o WHERE o.status = 'WAITING'") + @Query("SELECT o FROM orders o WHERE o.status = 'WAITING' AND o.deletedAt IS NULL") List findWaitingOrders(); @Transactional diff --git a/src/main/java/org/store/clothstar/order/service/OrderSellerService.java b/src/main/java/org/store/clothstar/order/service/OrderSellerService.java index 27a0827..1099c36 100644 --- a/src/main/java/org/store/clothstar/order/service/OrderSellerService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderSellerService.java @@ -57,7 +57,6 @@ public List getWaitingOrder() { List waitingOrders = orderSellerRepository.findWaitingOrders(); List filteredOrders = waitingOrders.stream() - .filter(order -> order.getDeletedAt() == null) .toList(); return filteredOrders.stream() diff --git a/src/main/java/org/store/clothstar/order/utils/URIBuilder.java b/src/main/java/org/store/clothstar/order/utils/URIBuilder.java deleted file mode 100644 index cb93cde..0000000 --- a/src/main/java/org/store/clothstar/order/utils/URIBuilder.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.store.clothstar.order.utils; - -import org.springframework.web.servlet.support.ServletUriComponentsBuilder; - -import java.net.URI; - -public class URIBuilder { - - public static URI buildURI(Long id) { - return ServletUriComponentsBuilder - .fromCurrentRequest() // 현재 요청의 URI를 사용 - .path("/{id}") // 경로 변수 추가 - .buildAndExpand(id) // {/id} 자리에 실제 id 값을 삽입 - .toUri(); - } -} diff --git a/src/test/java/org/store/clothstar/order/service/OrderSellerServiceTest.java b/src/test/java/org/store/clothstar/order/service/OrderSellerServiceTest.java index 88afe22..c5dff1d 100644 --- a/src/test/java/org/store/clothstar/order/service/OrderSellerServiceTest.java +++ b/src/test/java/org/store/clothstar/order/service/OrderSellerServiceTest.java @@ -111,13 +111,13 @@ void getWaitingOrder_test() { given(orderSellerRepository.findWaitingOrders()).willReturn(waitingOrders); given(order.getMemberId()).willReturn(memberId); given(order.getAddressId()).willReturn(addressId); + given(memberService.getMemberByMemberId(memberId)).willReturn(member); + given(address.getAddressInfo()).willReturn(addressInfo); + given(addressService.getAddressById(addressId)).willReturn(address); given(order.getCreatedAt()).willReturn(LocalDateTime.now()); - given(orderDetail.getDeletedAt()).willReturn(null); given(order.getOrderDetails()).willReturn(List.of(orderDetail)); + given(orderDetail.getDeletedAt()).willReturn(null); - given(memberService.getMemberByMemberId(memberId)).willReturn(member); - given(addressService.getAddressById(addressId)).willReturn(address); - given(address.getAddressInfo()).willReturn(addressInfo); given(order.getTotalPrice()).willReturn(totalPrice); given(orderDetail.getPrice()).willReturn(price); given(orderDetail.getProductId()).willReturn(productId); diff --git a/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java b/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java index b2ec632..38af22a 100644 --- a/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java +++ b/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java @@ -300,6 +300,37 @@ void saveOrder_verify_test() { assertThat(orderId).isEqualTo(1L); } + @Test + @DisplayName("saveOrder: 주문 생성 - 주문 개수가 재고보다 많을 때 예외처리 테스트") + void saveOrder_quantity_exception_test() { + //given + OrderRequestWrapper orderRequestWrapper = mock(OrderRequestWrapper.class); + CreateOrderRequest createOrderRequest = mock(CreateOrderRequest.class); + CreateOrderDetailRequest createOrderDetailRequest = mock(CreateOrderDetailRequest.class); + + given(orderRequestWrapper.getCreateOrderRequest()).willReturn(createOrderRequest); + given(orderRequestWrapper.getCreateOrderDetailRequest()).willReturn(createOrderDetailRequest); + given(createOrderRequest.getMemberId()).willReturn(1L); + given(createOrderRequest.getAddressId()).willReturn(2L); + given(memberService.getMemberByMemberId(1L)).willReturn(member); + given(addressService.getAddressById(2L)).willReturn(address); + given(createOrderRequest.toOrder(member,address)).willReturn(order); + given(createOrderDetailRequest.getProductLineId()).willReturn(3L); + given(createOrderDetailRequest.getProductId()).willReturn(4L); + given(productLineService.findById(3L)).willReturn(Optional.of(productLineEntity)); + given(productService.findById(4L)).willReturn(Optional.of(productEntity)); + + given(createOrderDetailRequest.getQuantity()).willReturn(10); + given(productEntity.getStock()).willReturn(1L); + + //when + ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> + orderService.saveOrder(orderRequestWrapper)); + + //then + assertEquals("400 BAD_REQUEST \"주문 개수가 재고보다 더 많습니다.\"", thrown.getMessage()); + } + @Test @DisplayName("confirmOrder: 구매 확정 - 성공 메서드 호출 테스트") void confirmOrder_verify_test() { From fc0d945bfb88c52a7bf71e10537bf11540a93125 Mon Sep 17 00:00:00 2001 From: subin Date: Wed, 17 Jul 2024 15:20:46 +0900 Subject: [PATCH 6/6] =?UTF-8?q?refactor:=20saveOrder=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 과정별 인터페이스 생성 - 파사드 패턴 적용 --- .../order/service/OrderSave/OrderCreator.java | 10 ++ .../service/OrderSave/OrderCreatorImpl.java | 17 ++++ .../service/OrderSave/OrderDetailAdder.java | 8 ++ .../OrderSave/OrderDetailAdderImpl.java | 13 +++ .../service/OrderSave/OrderDetailCreator.java | 11 +++ .../OrderSave/OrderDetailCreatorImpl.java | 16 ++++ .../OrderSave/OrderDetailValidator.java | 8 ++ .../OrderSave/OrderDetailValidatorImpl.java | 17 ++++ .../service/OrderSave/OrderPriceUpdater.java | 8 ++ .../OrderSave/OrderPriceUpdaterImpl.java | 17 ++++ .../service/OrderSave/OrderSaveFacade.java | 95 +++++++++++++++++++ .../order/service/OrderSave/OrderSaver.java | 7 ++ .../service/OrderSave/OrderSaverImpl.java | 18 ++++ .../order/service/OrderSave/StockUpdater.java | 7 ++ .../service/OrderSave/StockUpdaterImpl.java | 20 ++++ .../clothstar/order/service/OrderService.java | 49 ++-------- .../product/service/ProductService.java | 6 ++ .../order/service/OrderSellerServiceTest.java | 1 + 18 files changed, 288 insertions(+), 40 deletions(-) create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/OrderCreator.java create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/OrderCreatorImpl.java create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailAdder.java create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailAdderImpl.java create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailCreator.java create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailCreatorImpl.java create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailValidator.java create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailValidatorImpl.java create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/OrderPriceUpdater.java create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/OrderPriceUpdaterImpl.java create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/OrderSaveFacade.java create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/OrderSaver.java create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/OrderSaverImpl.java create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/StockUpdater.java create mode 100644 src/main/java/org/store/clothstar/order/service/OrderSave/StockUpdaterImpl.java diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/OrderCreator.java b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderCreator.java new file mode 100644 index 0000000..028c6fc --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderCreator.java @@ -0,0 +1,10 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.store.clothstar.member.domain.Address; +import org.store.clothstar.member.domain.Member; +import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.dto.request.CreateOrderRequest; + +public interface OrderCreator { + Order createOrder(CreateOrderRequest request,Member member,Address address); +} diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/OrderCreatorImpl.java b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderCreatorImpl.java new file mode 100644 index 0000000..f1dcd25 --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderCreatorImpl.java @@ -0,0 +1,17 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.springframework.stereotype.Service; +import org.store.clothstar.member.domain.Address; +import org.store.clothstar.member.domain.Member; +import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.dto.request.CreateOrderRequest; + + +@Service +public class OrderCreatorImpl implements OrderCreator { + + @Override + public Order createOrder(CreateOrderRequest request,Member member,Address address) { + return request.toOrder(member, address); + } +} diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailAdder.java b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailAdder.java new file mode 100644 index 0000000..e8d5820 --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailAdder.java @@ -0,0 +1,8 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.OrderDetail; + +public interface OrderDetailAdder { + void addOrderDetail(Order order, OrderDetail orderDetail); +} diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailAdderImpl.java b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailAdderImpl.java new file mode 100644 index 0000000..e5248ee --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailAdderImpl.java @@ -0,0 +1,13 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.springframework.stereotype.Service; +import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.OrderDetail; + +@Service +public class OrderDetailAdderImpl implements OrderDetailAdder { + @Override + public void addOrderDetail(Order order, OrderDetail orderDetail) { + order.addOrderDetail(orderDetail); + } +} diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailCreator.java b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailCreator.java new file mode 100644 index 0000000..4d82e42 --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailCreator.java @@ -0,0 +1,11 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.OrderDetail; +import org.store.clothstar.order.dto.request.CreateOrderDetailRequest; +import org.store.clothstar.product.entity.ProductEntity; +import org.store.clothstar.productLine.entity.ProductLineEntity; + +public interface OrderDetailCreator { + OrderDetail createOrderDetail(CreateOrderDetailRequest createOrderDetailRequest, Order order, ProductLineEntity productLineEntity, ProductEntity productEntity); +} diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailCreatorImpl.java b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailCreatorImpl.java new file mode 100644 index 0000000..fd59d78 --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailCreatorImpl.java @@ -0,0 +1,16 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.springframework.stereotype.Service; +import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.OrderDetail; +import org.store.clothstar.order.dto.request.CreateOrderDetailRequest; +import org.store.clothstar.product.entity.ProductEntity; +import org.store.clothstar.productLine.entity.ProductLineEntity; + +@Service +public class OrderDetailCreatorImpl implements OrderDetailCreator { + @Override + public OrderDetail createOrderDetail(CreateOrderDetailRequest createOrderDetailRequest, Order order, ProductLineEntity productLineEntity, ProductEntity productEntity) { + return createOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity); + } +} diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailValidator.java b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailValidator.java new file mode 100644 index 0000000..bdda45b --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailValidator.java @@ -0,0 +1,8 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.store.clothstar.order.dto.request.CreateOrderDetailRequest; +import org.store.clothstar.product.entity.ProductEntity; + +public interface OrderDetailValidator { + void validateOrderDetail(CreateOrderDetailRequest request, ProductEntity product); +} diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailValidatorImpl.java b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailValidatorImpl.java new file mode 100644 index 0000000..b733445 --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderDetailValidatorImpl.java @@ -0,0 +1,17 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import org.springframework.web.server.ResponseStatusException; +import org.store.clothstar.order.dto.request.CreateOrderDetailRequest; +import org.store.clothstar.product.entity.ProductEntity; + +@Service +public class OrderDetailValidatorImpl implements OrderDetailValidator{ + @Override + public void validateOrderDetail(CreateOrderDetailRequest request, ProductEntity product) { + if (request.getQuantity() > product.getStock()) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문 개수가 재고보다 더 많습니다."); + } + } +} diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/OrderPriceUpdater.java b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderPriceUpdater.java new file mode 100644 index 0000000..73a8ebe --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderPriceUpdater.java @@ -0,0 +1,8 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.OrderDetail; + +public interface OrderPriceUpdater { + void updateOrderPrice(Order order, OrderDetail orderDetail); +} diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/OrderPriceUpdaterImpl.java b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderPriceUpdaterImpl.java new file mode 100644 index 0000000..42e050b --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderPriceUpdaterImpl.java @@ -0,0 +1,17 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.springframework.stereotype.Service; +import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.OrderDetail; + +@Service +public class OrderPriceUpdaterImpl implements OrderPriceUpdater { + @Override + public void updateOrderPrice(Order order, OrderDetail orderDetail) { + int newTotalProductsPrice = order.getTotalPrice().getProducts() + orderDetail.getPrice().getOneKindTotalPrice(); + int newTotalPaymentPrice = + order.getTotalPrice().getProducts() + order.getTotalPrice().getShipping() + orderDetail.getPrice().getOneKindTotalPrice(); + + order.getTotalPrice().updatePrices(newTotalProductsPrice, newTotalPaymentPrice); + } +} diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/OrderSaveFacade.java b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderSaveFacade.java new file mode 100644 index 0000000..5268073 --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderSaveFacade.java @@ -0,0 +1,95 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.server.ResponseStatusException; +import org.store.clothstar.member.domain.Address; +import org.store.clothstar.member.domain.Member; +import org.store.clothstar.member.service.AddressService; +import org.store.clothstar.member.service.MemberService; +import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.OrderDetail; +import org.store.clothstar.order.dto.request.CreateOrderDetailRequest; +import org.store.clothstar.order.dto.request.CreateOrderRequest; +import org.store.clothstar.order.dto.request.OrderRequestWrapper; +import org.store.clothstar.product.entity.ProductEntity; +import org.store.clothstar.product.service.ProductService; +import org.store.clothstar.productLine.entity.ProductLineEntity; +import org.store.clothstar.productLine.service.ProductLineService; + +@Service +public class OrderSaveFacade { + + private final MemberService memberService; + private final AddressService addressService; + private final ProductService productService; + private final ProductLineService productLineService; + private final OrderCreator orderCreator; + private final OrderDetailValidator orderDetailValidator; + private final OrderDetailCreator orderDetailCreator; + private final OrderDetailAdder orderDetailAdder; + private final OrderSaver orderSaver; + private final OrderPriceUpdater orderPriceUpdater; + private final StockUpdater stockUpdater; + + + public OrderSaveFacade( + MemberService memberService, AddressService addressService + , ProductService productService, ProductLineService productLineService + , OrderCreator orderCreator + , OrderDetailValidator orderDetailValidator + , OrderDetailCreator orderDetailCreator + , OrderDetailAdder orderDetailAdder + , OrderSaver orderSaver + , OrderPriceUpdater orderPriceUpdater + , StockUpdater stockUpdater + ) { + this.memberService = memberService; + this.addressService = addressService; + this.productService = productService; + this.productLineService = productLineService; + this.orderCreator=orderCreator; + this.orderDetailValidator = orderDetailValidator; + this.orderDetailCreator = orderDetailCreator; + this.orderDetailAdder=orderDetailAdder; + this.orderSaver=orderSaver; + this.orderPriceUpdater=orderPriceUpdater; + this.stockUpdater=stockUpdater; + } + + @Transactional + public Long saveOrder(OrderRequestWrapper orderRequestWrapper) { + CreateOrderRequest createOrderRequest = orderRequestWrapper.getCreateOrderRequest(); + CreateOrderDetailRequest createOrderDetailRequest = orderRequestWrapper.getCreateOrderDetailRequest(); + Member member = memberService.getMemberByMemberId(createOrderRequest.getMemberId()); + Address address = addressService.getAddressById(createOrderRequest.getAddressId()); + ProductLineEntity productLineEntity = productLineService.findById(createOrderDetailRequest.getProductLineId()) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품을 찾을 수 없습니다")); + ProductEntity productEntity = productService.findById(createOrderDetailRequest.getProductId()) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 옵션을 찾을 수 없습니다")); + + // 요청 DTO로부터 주문 생성 + Order order = orderCreator.createOrder(createOrderRequest,member,address); + + // 주문상세 생성 유효성 검사 + orderDetailValidator.validateOrderDetail(createOrderDetailRequest,productEntity); + + // 주문상세 생성 + OrderDetail orderDetail = orderDetailCreator.createOrderDetail(createOrderDetailRequest, order, productLineEntity, productEntity); + + // 주문에 주문상세 추가 + orderDetailAdder.addOrderDetail(order, orderDetail); + + // 주문 저장 (orderDetail은 cascade 설정에 의해 자동 저장됨) + orderSaver.saveOrder(order); + + // 주문 정보 업데이트 + orderPriceUpdater.updateOrderPrice(order,orderDetail); + + // 주문 수량만큼 상품 재고 차감 + stockUpdater.updateStock(productEntity,orderDetail.getQuantity()); + + return order.getOrderId(); + } +} diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/OrderSaver.java b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderSaver.java new file mode 100644 index 0000000..bd83f9e --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderSaver.java @@ -0,0 +1,7 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.store.clothstar.order.domain.Order; + +public interface OrderSaver { + void saveOrder(Order order); +} diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/OrderSaverImpl.java b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderSaverImpl.java new file mode 100644 index 0000000..0bdc43b --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/OrderSaverImpl.java @@ -0,0 +1,18 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.springframework.stereotype.Service; +import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.repository.order.OrderUserRepository; + +@Service +public class OrderSaverImpl implements OrderSaver{ + private final OrderUserRepository orderUserRepository; + + public OrderSaverImpl(OrderUserRepository orderUserRepository) { + this.orderUserRepository = orderUserRepository; + } + @Override + public void saveOrder(Order order) { + orderUserRepository.save(order); + } +} diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/StockUpdater.java b/src/main/java/org/store/clothstar/order/service/OrderSave/StockUpdater.java new file mode 100644 index 0000000..667aeb3 --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/StockUpdater.java @@ -0,0 +1,7 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.store.clothstar.product.entity.ProductEntity; + +public interface StockUpdater { + void updateStock(ProductEntity product, int quantity); +} diff --git a/src/main/java/org/store/clothstar/order/service/OrderSave/StockUpdaterImpl.java b/src/main/java/org/store/clothstar/order/service/OrderSave/StockUpdaterImpl.java new file mode 100644 index 0000000..bbf490f --- /dev/null +++ b/src/main/java/org/store/clothstar/order/service/OrderSave/StockUpdaterImpl.java @@ -0,0 +1,20 @@ +package org.store.clothstar.order.service.OrderSave; + +import org.springframework.stereotype.Service; +import org.store.clothstar.product.entity.ProductEntity; +import org.store.clothstar.product.service.ProductService; + +@Service +public class StockUpdaterImpl implements StockUpdater { + + private static ProductService productService; + + public StockUpdaterImpl(ProductService productService){ + this.productService=productService; + } + + @Override + public void updateStock(ProductEntity product, int quantity) { + productService.updateProductStock(product, quantity); + } +} \ No newline at end of file diff --git a/src/main/java/org/store/clothstar/order/service/OrderService.java b/src/main/java/org/store/clothstar/order/service/OrderService.java index 95d5098..48c1e8a 100644 --- a/src/main/java/org/store/clothstar/order/service/OrderService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderService.java @@ -17,11 +17,10 @@ import org.store.clothstar.order.domain.type.Status; import org.store.clothstar.order.domain.vo.OrderDetailDTO; import org.store.clothstar.order.dto.reponse.OrderResponse; -import org.store.clothstar.order.dto.request.CreateOrderDetailRequest; -import org.store.clothstar.order.dto.request.CreateOrderRequest; import org.store.clothstar.order.dto.request.OrderRequestWrapper; import org.store.clothstar.order.repository.order.OrderDetailRepository; import org.store.clothstar.order.repository.order.OrderUserRepository; +import org.store.clothstar.order.service.OrderSave.*; import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.product.service.ProductService; import org.store.clothstar.productLine.entity.ProductLineEntity; @@ -42,12 +41,16 @@ public class OrderService { private final OrderDetailService orderDetailService; private final ProductService productService; private final ProductLineService productLineService; + private final OrderSaveFacade orderSaveFacade; + public OrderService( OrderUserRepository orderUserRepository , MemberService memberService, AddressService addressService - , OrderDetailService orderDetailService, OrderDetailRepository orderDetailRepository, - ProductService productService, ProductLineService productLineService) { + , OrderDetailService orderDetailService, OrderDetailRepository orderDetailRepository + , ProductService productService, ProductLineService productLineService + , OrderSaveFacade orderSaveFacade + ) { this.orderUserRepository = orderUserRepository; this.memberService = memberService; this.addressService = addressService; @@ -55,6 +58,7 @@ public OrderService( this.orderDetailService = orderDetailService; this.productService = productService; this.productLineService = productLineService; + this.orderSaveFacade=orderSaveFacade; } @Transactional(readOnly = true) @@ -156,42 +160,7 @@ public Slice getAllOrderSlicePaging(Pageable pageable) { @Transactional public Long saveOrder(OrderRequestWrapper orderRequestWrapper) { - CreateOrderRequest createOrderRequest = orderRequestWrapper.getCreateOrderRequest(); - CreateOrderDetailRequest createOrderDetailRequest = orderRequestWrapper.getCreateOrderDetailRequest(); - - // 주문 생성 - Member member = memberService.getMemberByMemberId(createOrderRequest.getMemberId()); - Address address = addressService.getAddressById(createOrderRequest.getAddressId()); - - Order order = createOrderRequest.toOrder(member, address); - - ProductLineEntity productLineEntity = productLineService.findById(createOrderDetailRequest.getProductLineId()) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품을 찾을 수 없습니다")); - ProductEntity productEntity = productService.findById(createOrderDetailRequest.getProductId()) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 옵션을 찾을 수 없습니다")); - - // 주문상세 생성 유효성 검사 - if (createOrderDetailRequest.getQuantity() > productEntity.getStock()) { - throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문 개수가 재고보다 더 많습니다."); - } - - OrderDetail orderDetail = createOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity); - order.addOrderDetail(orderDetail); // 주문에 주문상세 추가 - - // 주문 저장 (orderDetail은 cascade 설정에 의해 자동 저장됨) - orderUserRepository.save(order); - - // 주문 정보 업데이트 - int newTotalProductsPrice = order.getTotalPrice().getProducts() + orderDetail.getPrice().getOneKindTotalPrice(); - int newTotalPaymentPrice = - order.getTotalPrice().getProducts() + order.getTotalPrice().getShipping() + orderDetail.getPrice().getOneKindTotalPrice(); - - order.getTotalPrice().updatePrices(newTotalProductsPrice, newTotalPaymentPrice); - - // 주문 수량만큼 상품 재고 차감 - orderDetailService.updateProductStock(productEntity, orderDetail.getQuantity()); - - return order.getOrderId(); + return orderSaveFacade.saveOrder(orderRequestWrapper); } @Transactional diff --git a/src/main/java/org/store/clothstar/product/service/ProductService.java b/src/main/java/org/store/clothstar/product/service/ProductService.java index 579a1de..874c51a 100644 --- a/src/main/java/org/store/clothstar/product/service/ProductService.java +++ b/src/main/java/org/store/clothstar/product/service/ProductService.java @@ -97,4 +97,10 @@ public List findByIdIn(List productIds) { public Optional findById(Long productId) { return productRepository.findById(productId); } + + @Transactional + public void updateProductStock(ProductEntity productEntity, int quantity) { + long updatedStock = productEntity.getStock() - quantity; + productEntity.updateStock(updatedStock); + } } diff --git a/src/test/java/org/store/clothstar/order/service/OrderSellerServiceTest.java b/src/test/java/org/store/clothstar/order/service/OrderSellerServiceTest.java index c5dff1d..33eac96 100644 --- a/src/test/java/org/store/clothstar/order/service/OrderSellerServiceTest.java +++ b/src/test/java/org/store/clothstar/order/service/OrderSellerServiceTest.java @@ -107,6 +107,7 @@ void getWaitingOrder_test() { Long productId = 3L; Long productLineId = 4L; + //TODO 추후 개선 예정 List waitingOrders = List.of(order); given(orderSellerRepository.findWaitingOrders()).willReturn(waitingOrders); given(order.getMemberId()).willReturn(memberId);