diff --git a/src/main/java/org/store/clothstar/orderDetail/controller/OrderDetailController.java b/src/main/java/org/store/clothstar/order/controller/OrderDetailController.java similarity index 90% rename from src/main/java/org/store/clothstar/orderDetail/controller/OrderDetailController.java rename to src/main/java/org/store/clothstar/order/controller/OrderDetailController.java index 68de9814..da685f50 100644 --- a/src/main/java/org/store/clothstar/orderDetail/controller/OrderDetailController.java +++ b/src/main/java/org/store/clothstar/order/controller/OrderDetailController.java @@ -1,4 +1,4 @@ -package org.store.clothstar.orderDetail.controller; +package org.store.clothstar.order.controller; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -9,8 +9,8 @@ import org.springframework.web.bind.annotation.*; import org.store.clothstar.common.dto.MessageDTO; import org.store.clothstar.common.dto.SaveResponseDTO; -import org.store.clothstar.orderDetail.dto.request.AddOrderDetailRequest; -import org.store.clothstar.orderDetail.service.OrderDetailService; +import org.store.clothstar.order.dto.request.AddOrderDetailRequest; +import org.store.clothstar.order.service.OrderDetailService; @Tag(name = "OrderDetail", description = "주문 내 개별 상품에 대한 옵션, 수량 등을 나타내는, 주문상세(OrderDetail) 정보 관리에 대한 API 입니다.") @RestController diff --git a/src/main/java/org/store/clothstar/order/controller/OrderSellerController.java b/src/main/java/org/store/clothstar/order/controller/OrderSellerController.java index bef1e030..19617037 100644 --- a/src/main/java/org/store/clothstar/order/controller/OrderSellerController.java +++ b/src/main/java/org/store/clothstar/order/controller/OrderSellerController.java @@ -14,7 +14,7 @@ @Tag(name = "OrderSeller", description = "판매자(OrderSeller)의 주문 정보 관리에 대한 API 입니다.") @RestController @RequiredArgsConstructor -@RequestMapping("/v1/orders") +@RequestMapping("/v1/orders/seller") public class OrderSellerController { private final OrderSellerService orderSellerService; diff --git a/src/main/java/org/store/clothstar/order/controller/OrderController.java b/src/main/java/org/store/clothstar/order/controller/OrderUserController.java similarity index 79% rename from src/main/java/org/store/clothstar/order/controller/OrderController.java rename to src/main/java/org/store/clothstar/order/controller/OrderUserController.java index 46aeda3d..9c09e3c6 100644 --- a/src/main/java/org/store/clothstar/order/controller/OrderController.java +++ b/src/main/java/org/store/clothstar/order/controller/OrderUserController.java @@ -22,7 +22,7 @@ @RestController @RequiredArgsConstructor @RequestMapping("/v1/orders") -public class OrderController { +public class OrderUserController { private final OrderService orderService; private final OrderApplicationService orderApplicationService; @@ -58,13 +58,20 @@ public ResponseEntity saveOrder(@RequestBody @Validated OrderRe orderId, HttpStatus.OK.value(), "주문이 정상적으로 생성되었습니다.")); } - @Operation(summary = "구매 확정", description = "구매자가 구매 확정 시, 주문상태가 '구매확정'으로 변경된다.") - @PatchMapping("{orderId}") - public ResponseEntity deliveredToConfirmOrder(@PathVariable Long orderId) { - orderService.deliveredToConfirmOrder(orderId); + @Operation(summary = "(구매자)구매 확정", description = "구매자가 구매 확정 시, 주문상태가 '구매확정'으로 변경된다(단, 주문상태가 '배송완료'일 때만 가능).") + @PatchMapping("{orderId}/confirm") + public ResponseEntity confirmOrder(@PathVariable Long orderId) { + orderService.confirmOrder(orderId); return ResponseEntity.ok(new MessageDTO(HttpStatus.OK.value(), "주문이 정상적으로 구매 확정 되었습니다.")); } + @Operation(summary = "(구매자)주문 취소", description = "구매자가 주문 취소 시, 주문상태가 '주문취소'로 변경된다(단, 주문상태가 '승인대기' 또는 '주문승인'일 때만 가능).") + @PatchMapping("{orderId}/cancel") + public ResponseEntity cancelOrder(@PathVariable Long orderId) { + orderService.cancelOrder(orderId); + return ResponseEntity.ok(new MessageDTO(HttpStatus.OK.value(), "주문이 정상적으로 취소되었습니다.")); + } + @Operation(summary = "주문 삭제", description = "주문 삭제시간을 현재시간으로 업데이트 한다.") @DeleteMapping("{orderId}") public ResponseEntity deleteOrder(@PathVariable Long orderId) { diff --git a/src/main/java/org/store/clothstar/order/entity/OrderEntity.java b/src/main/java/org/store/clothstar/order/domain/Order.java similarity index 51% rename from src/main/java/org/store/clothstar/order/entity/OrderEntity.java rename to src/main/java/org/store/clothstar/order/domain/Order.java index d9d894b1..285677f4 100644 --- a/src/main/java/org/store/clothstar/order/entity/OrderEntity.java +++ b/src/main/java/org/store/clothstar/order/domain/Order.java @@ -1,4 +1,4 @@ -package org.store.clothstar.order.entity; +package org.store.clothstar.order.domain; import jakarta.persistence.*; import lombok.AllArgsConstructor; @@ -6,9 +6,9 @@ import lombok.Getter; import lombok.NoArgsConstructor; import org.store.clothstar.common.entity.BaseEntity; -import org.store.clothstar.order.type.PaymentMethod; -import org.store.clothstar.order.type.Status; -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; +import org.store.clothstar.order.domain.type.PaymentMethod; +import org.store.clothstar.order.domain.type.Status; +import org.store.clothstar.order.domain.vo.TotalPrice; import java.time.LocalDateTime; import java.util.List; @@ -18,39 +18,32 @@ @Getter @Builder @Entity(name = "orders") -public class OrderEntity extends BaseEntity { +public class Order extends BaseEntity { @Id private Long orderId; + @Column(name = "member_id") + private Long memberId; + + @Column(name = "address_id") + private Long addressId; + @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true) - private List orderDetails; + private List orderDetails; @Enumerated(EnumType.STRING) private Status status; - @Column(name = "total_shipping_price") - private int totalShippingPrice; - - @Column(name = "total_products_price") - private int totalProductsPrice; - @Enumerated(EnumType.STRING) @Column(name = "payment_method") private PaymentMethod paymentMethod; - @Column(name = "total_payment_price") - private int totalPaymentPrice; + @Embedded + TotalPrice totalPrice; + - @Column(name = "member_id") - private Long memberId; - @Column(name = "address_id") - private Long addressId; - public void updatePrices(int totalProductsPrice, int totalPaymentPrice) { - this.totalProductsPrice = totalProductsPrice; - this.totalPaymentPrice = totalPaymentPrice; - } public void updateDeletedAt() { this.deletedAt = LocalDateTime.now(); diff --git a/src/main/java/org/store/clothstar/orderDetail/entity/OrderDetailEntity.java b/src/main/java/org/store/clothstar/order/domain/OrderDetail.java similarity index 56% rename from src/main/java/org/store/clothstar/orderDetail/entity/OrderDetailEntity.java rename to src/main/java/org/store/clothstar/order/domain/OrderDetail.java index b1f6a189..91020821 100644 --- a/src/main/java/org/store/clothstar/orderDetail/entity/OrderDetailEntity.java +++ b/src/main/java/org/store/clothstar/order/domain/OrderDetail.java @@ -1,4 +1,4 @@ -package org.store.clothstar.orderDetail.entity; +package org.store.clothstar.order.domain; import jakarta.persistence.*; import lombok.AllArgsConstructor; @@ -6,7 +6,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import org.store.clothstar.common.entity.BaseEntity; -import org.store.clothstar.order.entity.OrderEntity; +import org.store.clothstar.order.domain.vo.Price; import java.time.LocalDateTime; @@ -15,28 +15,25 @@ @NoArgsConstructor @Builder @Entity(name = "order_detail") -public class OrderDetailEntity extends BaseEntity { +public class OrderDetail extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long orderDetailId; - private int quantity; + @Column(name = "product_line_id") + private Long productLineId; - @Column(name = "fixed_price") - private int fixedPrice; // 고정된 상품 가격 ( 주문 당시 가격 ) + @Column(name = "product_id") + private Long productId; + + private int quantity; - @Column(name = "onekind_total_price") - private int oneKindTotalPrice; // 상품 종류 하나당 총 가격 + @Embedded + Price price; @ManyToOne @JoinColumn(name = "order_id") - private OrderEntity order; - - @JoinColumn(name = "product_line_id") - private Long productLineId; - - @JoinColumn(name = "product_id") - private Long productId; + private Order order; public void updateDeletedAt() { this.deletedAt = LocalDateTime.now(); diff --git a/src/main/java/org/store/clothstar/order/type/ApprovalStatus.java b/src/main/java/org/store/clothstar/order/domain/type/ApprovalStatus.java similarity index 54% rename from src/main/java/org/store/clothstar/order/type/ApprovalStatus.java rename to src/main/java/org/store/clothstar/order/domain/type/ApprovalStatus.java index f6ff70b9..dd50c0f4 100644 --- a/src/main/java/org/store/clothstar/order/type/ApprovalStatus.java +++ b/src/main/java/org/store/clothstar/order/domain/type/ApprovalStatus.java @@ -1,4 +1,4 @@ -package org.store.clothstar.order.type; +package org.store.clothstar.order.domain.type; public enum ApprovalStatus { APPROVE, diff --git a/src/main/java/org/store/clothstar/order/type/PaymentMethod.java b/src/main/java/org/store/clothstar/order/domain/type/PaymentMethod.java similarity index 59% rename from src/main/java/org/store/clothstar/order/type/PaymentMethod.java rename to src/main/java/org/store/clothstar/order/domain/type/PaymentMethod.java index 983331e4..f20a5c9f 100644 --- a/src/main/java/org/store/clothstar/order/type/PaymentMethod.java +++ b/src/main/java/org/store/clothstar/order/domain/type/PaymentMethod.java @@ -1,4 +1,4 @@ -package org.store.clothstar.order.type; +package org.store.clothstar.order.domain.type; public enum PaymentMethod { CARD, diff --git a/src/main/java/org/store/clothstar/order/type/Status.java b/src/main/java/org/store/clothstar/order/domain/type/Status.java similarity index 58% rename from src/main/java/org/store/clothstar/order/type/Status.java rename to src/main/java/org/store/clothstar/order/domain/type/Status.java index 569b7ec7..54513577 100644 --- a/src/main/java/org/store/clothstar/order/type/Status.java +++ b/src/main/java/org/store/clothstar/order/domain/type/Status.java @@ -1,8 +1,9 @@ -package org.store.clothstar.order.type; +package org.store.clothstar.order.domain.type; public enum Status { WAITING, APPROVE, + DELIVERING, DELIVERED, CONFIRM, CANCEL diff --git a/src/main/java/org/store/clothstar/order/dto/reponse/AddressDTO.java b/src/main/java/org/store/clothstar/order/domain/vo/AddressDTO.java similarity index 94% rename from src/main/java/org/store/clothstar/order/dto/reponse/AddressDTO.java rename to src/main/java/org/store/clothstar/order/domain/vo/AddressDTO.java index e7aa741b..83ab7c94 100644 --- a/src/main/java/org/store/clothstar/order/dto/reponse/AddressDTO.java +++ b/src/main/java/org/store/clothstar/order/domain/vo/AddressDTO.java @@ -1,4 +1,4 @@ -package org.store.clothstar.order.dto.reponse; +package org.store.clothstar.order.domain.vo; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; diff --git a/src/main/java/org/store/clothstar/order/domain/vo/OrderDetailDTO.java b/src/main/java/org/store/clothstar/order/domain/vo/OrderDetailDTO.java new file mode 100644 index 00000000..5cb82e2f --- /dev/null +++ b/src/main/java/org/store/clothstar/order/domain/vo/OrderDetailDTO.java @@ -0,0 +1,40 @@ +package org.store.clothstar.order.domain.vo; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.store.clothstar.order.domain.OrderDetail; +import org.store.clothstar.product.entity.ProductEntity; +import org.store.clothstar.productLine.entity.ProductLineEntity; + +@Getter +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class OrderDetailDTO { + + private Long orderDetailId; + private String productName; // 상품명 + private String optionName; + private String brandName; + private int productPrice; // 고정된 상품 가격 ( 주문 당시 가격 ) + private int extraCharge; + private int quantity; + private int totalPrice; // 상품 종류 하나당 총 가격 + + public static OrderDetailDTO from(OrderDetail orderDetail, ProductEntity productEntity, ProductLineEntity productLineEntity){ + + return OrderDetailDTO.builder() + .orderDetailId(orderDetail.getOrderDetailId()) + .productName(productLineEntity.getName()) + .optionName(productEntity.getName()) + .brandName(productLineEntity.getSeller().getBrandName()) + .productPrice(productLineEntity.getPrice()) + .extraCharge(productEntity.getExtraCharge()) + .quantity(orderDetail.getQuantity()) + .totalPrice(orderDetail.getPrice().getOneKindTotalPrice()) + .build(); + } +} + diff --git a/src/main/java/org/store/clothstar/order/domain/vo/Price.java b/src/main/java/org/store/clothstar/order/domain/vo/Price.java new file mode 100644 index 00000000..d97ca29e --- /dev/null +++ b/src/main/java/org/store/clothstar/order/domain/vo/Price.java @@ -0,0 +1,20 @@ +package org.store.clothstar.order.domain.vo; + +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Embeddable +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class Price { + @Column(name = "fixed_price") + private int fixedPrice; // 고정된 상품 가격 ( 주문 당시 가격 ) + @Column(name = "onekind_total_price") + private int oneKindTotalPrice; // 상품 종류 하나당 총 가격 +} diff --git a/src/main/java/org/store/clothstar/order/domain/vo/TotalPrice.java b/src/main/java/org/store/clothstar/order/domain/vo/TotalPrice.java new file mode 100644 index 00000000..13fa581f --- /dev/null +++ b/src/main/java/org/store/clothstar/order/domain/vo/TotalPrice.java @@ -0,0 +1,27 @@ +package org.store.clothstar.order.domain.vo; + +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Embeddable +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class TotalPrice { + @Column(name = "total_shipping_price") + private int shipping; + @Column(name = "total_products_price") + private int products; + @Column(name = "total_payment_price") + private int payment; + + public void updatePrices(int totalProductsPrice, int totalPaymentPrice) { + this.products = totalProductsPrice; + this.payment = totalPaymentPrice; + } +} diff --git a/src/main/java/org/store/clothstar/orderDetail/dto/response/OrderDetailResponse.java b/src/main/java/org/store/clothstar/order/dto/reponse/OrderDetailResponse.java similarity index 55% rename from src/main/java/org/store/clothstar/orderDetail/dto/response/OrderDetailResponse.java rename to src/main/java/org/store/clothstar/order/dto/reponse/OrderDetailResponse.java index b1510579..d3576e87 100644 --- a/src/main/java/org/store/clothstar/orderDetail/dto/response/OrderDetailResponse.java +++ b/src/main/java/org/store/clothstar/order/dto/reponse/OrderDetailResponse.java @@ -1,4 +1,4 @@ -package org.store.clothstar.orderDetail.dto.response; +package org.store.clothstar.order.dto.reponse; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Builder; @@ -44,22 +44,4 @@ public class OrderDetailResponse { @Schema(description = "브랜드 이름", example = "나이키") private String brandName; - - -// public static OrderDetailResponse fromOrderDetailEntity(OrderDetailEntity orderDetailEntity, ProductLineEntity productLineEntity, ProductEntity productEntity) { -// return OrderDetailResponse.builder() -// .orderDetailId(orderDetailEntity.getOrderDetailId()) -// .orderId(orderDetailEntity.getOrder().getOrderId()) -// .productLineId(orderDetailEntity.getProductLineId()) -// .productId(orderDetailEntity.getProductId()) -// .quantity(orderDetailEntity.getQuantity()) -// .fixedPrice(orderDetailEntity.getFixedPrice()) -// .oneKindTotalPrice(orderDetailEntity.getOneKindTotalPrice()) -// .name(orderDetailEntity.getName()) -// .stock(orderDetailEntity.getStock()) -// .optionName(orderDetailEntity.getName()) -// .extraCharge(orderDetailEntity.getExtraCharge()) -// .brandName(orderDetailEntity.getSeller().getBrandName()) -// .build(); -// } } 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 b78b1ea8..5af26a81 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 @@ -1,6 +1,5 @@ package org.store.clothstar.order.dto.reponse; -import com.querydsl.core.annotations.QueryProjection; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Builder; @@ -8,12 +7,13 @@ import lombok.NoArgsConstructor; import org.store.clothstar.member.domain.Address; import org.store.clothstar.member.domain.Member; -import org.store.clothstar.order.entity.OrderEntity; -import org.store.clothstar.order.type.PaymentMethod; -import org.store.clothstar.order.type.Status; -import org.store.clothstar.orderDetail.dto.OrderDetailDTO; -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; -import org.store.clothstar.productLine.entity.ProductLineEntity; +import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.type.PaymentMethod; +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; import java.util.ArrayList; @@ -44,51 +44,25 @@ public class OrderResponse { @Schema(description = "결제 수단", example = "CARD") private PaymentMethod paymentMethod; - @Schema(description = "총 배송비", example = "3000") - private int totalShippingPrice; - - @Schema(description = "총 상품 금액", example = "15000") - private int totalProductsPrice; - - @Schema(description = "총 결제 금액", example = "18000") - private int totalPaymentPrice; + private TotalPrice totalPrice; @Builder.Default private List orderDetailList = new ArrayList<>(); - @QueryProjection - public OrderResponse(OrderEntity orderEntity, - OrderDetailEntity orderDetailEntity, - Member member, Address address, - ProductLineEntity productLineEntity) { - this.orderId = orderEntity.getOrderId(); - this.ordererName = member.getName(); - this.createdAt = orderEntity.getCreatedAt().toLocalDate(); - this.status = orderEntity.getStatus(); - this.totalShippingPrice = orderEntity.getTotalShippingPrice(); - this.totalProductsPrice = orderEntity.getTotalProductsPrice(); - this.paymentMethod = orderEntity.getPaymentMethod(); - this.totalPaymentPrice = orderEntity.getTotalPaymentPrice(); - this.address = AddressDTO.builder() - .receiverName(address.getReceiverName()) - .addressBasic(address.getAddressInfo().getAddressBasic()) - .addressDetail(address.getAddressInfo().getAddressDetail()) - .telNo(address.getTelNo()) - .deliveryRequest(address.getAddressInfo().getDeliveryRequest()) + public static OrderResponse from(Order order, Member member, Address address) { + TotalPrice totalPrice = TotalPrice.builder() + .shipping(order.getTotalPrice().getShipping()) + .products(order.getTotalPrice().getProducts()) + .payment(order.getTotalPrice().getPayment()) .build(); - this.orderDetailList = new ArrayList<>(); - } - public static OrderResponse from(OrderEntity orderEntity, Member member, Address address) { return OrderResponse.builder() - .orderId(orderEntity.getOrderId()) + .orderId(order.getOrderId()) .ordererName(member.getName()) - .createdAt(orderEntity.getCreatedAt().toLocalDate()) - .status(orderEntity.getStatus()) - .totalShippingPrice(orderEntity.getTotalShippingPrice()) - .totalProductsPrice(orderEntity.getTotalProductsPrice()) - .paymentMethod(orderEntity.getPaymentMethod()) - .totalPaymentPrice(orderEntity.getTotalPaymentPrice()) + .createdAt(order.getCreatedAt().toLocalDate()) + .status(order.getStatus()) + .paymentMethod(order.getPaymentMethod()) + .totalPrice(totalPrice) .address(AddressDTO.builder() .receiverName(address.getReceiverName()) .addressBasic(address.getAddressInfo().getAddressBasic()) diff --git a/src/main/java/org/store/clothstar/orderDetail/dto/request/AddOrderDetailRequest.java b/src/main/java/org/store/clothstar/order/dto/request/AddOrderDetailRequest.java similarity index 67% rename from src/main/java/org/store/clothstar/orderDetail/dto/request/AddOrderDetailRequest.java rename to src/main/java/org/store/clothstar/order/dto/request/AddOrderDetailRequest.java index a1122981..dc1e4900 100644 --- a/src/main/java/org/store/clothstar/orderDetail/dto/request/AddOrderDetailRequest.java +++ b/src/main/java/org/store/clothstar/order/dto/request/AddOrderDetailRequest.java @@ -1,4 +1,4 @@ -package org.store.clothstar.orderDetail.dto.request; +package org.store.clothstar.order.dto.request; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotNull; @@ -7,8 +7,9 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.store.clothstar.order.entity.OrderEntity; -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; +import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.OrderDetail; +import org.store.clothstar.order.domain.vo.Price; import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.productLine.entity.ProductLineEntity; @@ -19,32 +20,36 @@ @Schema(description = "주문 상세 추가용 Request") public class AddOrderDetailRequest { - @Schema(description = "주문 번호", nullable = false) + @Schema(description = "주문 번호") @NotNull(message = "주문 번호는 비어있을 수 없습니다.") private Long orderId; - @Schema(description = "상품 번호", nullable = false) + @Schema(description = "상품 번호") @NotNull(message = "상품 번호는 비어있을 수 없습니다.") private Long productLineId; - @Schema(description = "상품 옵션 번호", nullable = false) + @Schema(description = "상품 옵션 번호") @NotNull(message = "상품 옵션 번호는 비어있을 수 없습니다.") private Long productId; - @Schema(description = "상품 수량", nullable = false) + @Schema(description = "상품 수량") @NotNull(message = "상품 수량은 비어있을 수 없습니다.") @Positive(message = "상품 수량은 0보다 커야 합니다.") private int quantity; - public OrderDetailEntity toOrderDetailEntity(OrderEntity orderEntity, ProductLineEntity productLineEntity, ProductEntity productEntity) { - return OrderDetailEntity.builder() - .order(orderEntity) + public OrderDetail toOrderDetail(Order order, ProductLineEntity productLineEntity, ProductEntity productEntity) { + Price price = Price.builder() + .fixedPrice(productLineEntity.getPrice()) + .oneKindTotalPrice(quantity * productLineEntity.getPrice()) + .build(); + + return OrderDetail.builder() + .order(order) .productLineId(productLineEntity.getProductLineId()) .productId(productEntity.getProductId()) .quantity(quantity) - .fixedPrice(productLineEntity.getPrice()) - .oneKindTotalPrice(quantity * productLineEntity.getPrice()) + .price(price) .build(); } } diff --git a/src/main/java/org/store/clothstar/orderDetail/dto/request/CreateOrderDetailRequest.java b/src/main/java/org/store/clothstar/order/dto/request/CreateOrderDetailRequest.java similarity index 67% rename from src/main/java/org/store/clothstar/orderDetail/dto/request/CreateOrderDetailRequest.java rename to src/main/java/org/store/clothstar/order/dto/request/CreateOrderDetailRequest.java index 9b21d5e9..baf9ef95 100644 --- a/src/main/java/org/store/clothstar/orderDetail/dto/request/CreateOrderDetailRequest.java +++ b/src/main/java/org/store/clothstar/order/dto/request/CreateOrderDetailRequest.java @@ -1,4 +1,4 @@ -package org.store.clothstar.orderDetail.dto.request; +package org.store.clothstar.order.dto.request; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotNull; @@ -7,8 +7,9 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.store.clothstar.order.entity.OrderEntity; -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; +import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.OrderDetail; +import org.store.clothstar.order.domain.vo.Price; import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.productLine.entity.ProductLineEntity; @@ -19,28 +20,32 @@ @Schema(description = "주문 상세 저장용 Request") public class CreateOrderDetailRequest { - @Schema(description = "상품 번호", nullable = false) + @Schema(description = "상품 번호") @NotNull(message = "상품 번호는 비어있을 수 없습니다.") private Long productLineId; - @Schema(description = "상품 옵션 번호", nullable = false) + @Schema(description = "상품 옵션 번호") @NotNull(message = "상품 옵션 번호는 비어있을 수 없습니다.") private Long productId; - @Schema(description = "상품 수량", nullable = false) + @Schema(description = "상품 수량") @NotNull(message = "상품 수량은 비어있을 수 없습니다.") @Positive(message = "상품 수량은 0보다 커야 합니다.") private int quantity; - public OrderDetailEntity toOrderDetailEntity(OrderEntity orderEntity, ProductLineEntity productLineEntity, ProductEntity productEntity) { - return OrderDetailEntity.builder() - .order(orderEntity) + public OrderDetail toOrderDetail(Order order, ProductLineEntity productLineEntity, ProductEntity productEntity) { + Price price = Price.builder() + .fixedPrice(productLineEntity.getPrice()) + .oneKindTotalPrice(quantity * productLineEntity.getPrice()) + .build(); + + return OrderDetail.builder() + .order(order) .productLineId(productLineEntity.getProductLineId()) .productId(productEntity.getProductId()) .quantity(quantity) - .fixedPrice(productLineEntity.getPrice()) - .oneKindTotalPrice(quantity * productLineEntity.getPrice()) + .price(price) .build(); } } diff --git a/src/main/java/org/store/clothstar/order/dto/request/CreateOrderRequest.java b/src/main/java/org/store/clothstar/order/dto/request/CreateOrderRequest.java index 093e070b..c1cc9de3 100644 --- a/src/main/java/org/store/clothstar/order/dto/request/CreateOrderRequest.java +++ b/src/main/java/org/store/clothstar/order/dto/request/CreateOrderRequest.java @@ -8,9 +8,10 @@ import lombok.NoArgsConstructor; import org.store.clothstar.member.domain.Address; import org.store.clothstar.member.domain.Member; -import org.store.clothstar.order.entity.OrderEntity; -import org.store.clothstar.order.type.PaymentMethod; -import org.store.clothstar.order.type.Status; +import org.store.clothstar.order.domain.Order; +import org.store.clothstar.order.domain.type.PaymentMethod; +import org.store.clothstar.order.domain.type.Status; +import org.store.clothstar.order.domain.vo.TotalPrice; import org.store.clothstar.order.utils.GenerateOrderId; @Getter @@ -20,29 +21,33 @@ @Schema(description = "주문 저장용 Request") public class CreateOrderRequest { - @Schema(description = "결제 수단", nullable = false) + @Schema(description = "결제 수단") @NotNull(message = "결제 수단은 비어있을 수 없습니다.") private PaymentMethod paymentMethod; - @Schema(description = "회원 번호", nullable = false) + @Schema(description = "회원 번호") @NotNull(message = "회원 번호는 비어있을 수 없습니다.") private Long memberId; - @Schema(description = "배송지 번호", nullable = false) + @Schema(description = "배송지 번호") @NotNull(message = "배송지 번호는 비어있을 수 없습니다.") private Long addressId; - public OrderEntity toOrderEntity(Member member, Address address) { - return OrderEntity.builder() + public Order toOrder(Member member, Address address) { + TotalPrice totalPrice = TotalPrice.builder() + .shipping(3000) + .products(0) + .payment(0) + .build(); + + return Order.builder() .orderId(GenerateOrderId.generateOrderId()) .memberId(member.getMemberId()) .addressId(address.getAddressId()) .status(Status.WAITING) - .totalShippingPrice(3000) - .totalProductsPrice(0) .paymentMethod(paymentMethod) - .totalPaymentPrice(0) + .totalPrice(totalPrice) .build(); } } diff --git a/src/main/java/org/store/clothstar/order/dto/request/OrderRequestWrapper.java b/src/main/java/org/store/clothstar/order/dto/request/OrderRequestWrapper.java index c1914851..61e377b4 100644 --- a/src/main/java/org/store/clothstar/order/dto/request/OrderRequestWrapper.java +++ b/src/main/java/org/store/clothstar/order/dto/request/OrderRequestWrapper.java @@ -4,7 +4,6 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.store.clothstar.orderDetail.dto.request.CreateOrderDetailRequest; @Getter @AllArgsConstructor diff --git a/src/main/java/org/store/clothstar/order/dto/request/OrderSellerRequest.java b/src/main/java/org/store/clothstar/order/dto/request/OrderSellerRequest.java index a53061aa..8828562c 100644 --- a/src/main/java/org/store/clothstar/order/dto/request/OrderSellerRequest.java +++ b/src/main/java/org/store/clothstar/order/dto/request/OrderSellerRequest.java @@ -6,7 +6,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.store.clothstar.order.type.ApprovalStatus; +import org.store.clothstar.order.domain.type.ApprovalStatus; @Getter @AllArgsConstructor diff --git a/src/main/java/org/store/clothstar/orderDetail/repository/JpaOrderDetailRepository.java b/src/main/java/org/store/clothstar/order/repository/order/JpaOrderDetailRepository.java similarity index 65% rename from src/main/java/org/store/clothstar/orderDetail/repository/JpaOrderDetailRepository.java rename to src/main/java/org/store/clothstar/order/repository/order/JpaOrderDetailRepository.java index 58e0a24d..caca25e8 100644 --- a/src/main/java/org/store/clothstar/orderDetail/repository/JpaOrderDetailRepository.java +++ b/src/main/java/org/store/clothstar/order/repository/order/JpaOrderDetailRepository.java @@ -1,17 +1,17 @@ -package org.store.clothstar.orderDetail.repository; +package org.store.clothstar.order.repository.order; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; +import org.store.clothstar.order.domain.OrderDetail; import java.util.List; @Repository -public interface JpaOrderDetailRepository extends JpaRepository, OrderDetailRepository { +public interface JpaOrderDetailRepository extends JpaRepository, OrderDetailRepository { @Modifying @Query("SELECT od FROM order_detail od WHERE od.order.orderId = :orderId") - List findOrderDetailListByOrderId(@Param("orderId") Long orderId); + List findOrderDetailListByOrderId(@Param("orderId") Long orderId); } diff --git a/src/main/java/org/store/clothstar/order/repository/order/JpaOrderRepository.java b/src/main/java/org/store/clothstar/order/repository/order/JpaOrderUserRepository.java similarity index 55% rename from src/main/java/org/store/clothstar/order/repository/order/JpaOrderRepository.java rename to src/main/java/org/store/clothstar/order/repository/order/JpaOrderUserRepository.java index 59339b33..38f39cd8 100644 --- a/src/main/java/org/store/clothstar/order/repository/order/JpaOrderRepository.java +++ b/src/main/java/org/store/clothstar/order/repository/order/JpaOrderUserRepository.java @@ -5,12 +5,17 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.transaction.annotation.Transactional; -import org.store.clothstar.order.entity.OrderEntity; +import org.store.clothstar.order.domain.Order; -public interface JpaOrderRepository extends JpaRepository, OrderRepository{ +public interface JpaOrderUserRepository extends JpaRepository, OrderUserRepository { @Transactional @Modifying @Query("UPDATE orders o SET o.status ='CONFIRM' WHERE o.orderId = :orderId") - void deliveredToConfirmOrder(@Param("orderId") Long orderId); + void confirmOrder(@Param("orderId") Long orderId); + + @Transactional + @Modifying + @Query("UPDATE orders o SET o.status ='CANCEL' WHERE o.orderId = :orderId") + void cancelOrder(@Param("orderId") Long orderId); } diff --git a/src/main/java/org/store/clothstar/order/repository/order/OrderDetailRepository.java b/src/main/java/org/store/clothstar/order/repository/order/OrderDetailRepository.java new file mode 100644 index 00000000..926541b3 --- /dev/null +++ b/src/main/java/org/store/clothstar/order/repository/order/OrderDetailRepository.java @@ -0,0 +1,14 @@ +package org.store.clothstar.order.repository.order; + +import org.store.clothstar.order.domain.OrderDetail; + +import java.util.List; +import java.util.Optional; + +public interface OrderDetailRepository { + Optional findById(Long orderDetailId); + + OrderDetail save(OrderDetail orderdetailEntity); + + List findOrderDetailListByOrderId(Long orderId); +} diff --git a/src/main/java/org/store/clothstar/order/repository/order/OrderRepository.java b/src/main/java/org/store/clothstar/order/repository/order/OrderRepository.java deleted file mode 100644 index 586840fb..00000000 --- a/src/main/java/org/store/clothstar/order/repository/order/OrderRepository.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.store.clothstar.order.repository.order; - -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Slice; -import org.store.clothstar.order.entity.OrderEntity; - -import java.util.Optional; - -public interface OrderRepository { - - Optional findById(Long orderId); - - OrderEntity save(OrderEntity orderEntity); - - void deliveredToConfirmOrder(Long orderId); - - Page findAll(Pageable pageable); - - Slice findAllBy(Pageable pageable); -} 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 new file mode 100644 index 00000000..938e3220 --- /dev/null +++ b/src/main/java/org/store/clothstar/order/repository/order/OrderUserRepository.java @@ -0,0 +1,20 @@ +package org.store.clothstar.order.repository.order; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.store.clothstar.order.domain.Order; + +import java.util.Optional; + +public interface OrderUserRepository { + + Optional findById(Long orderId); + + Page findAll(Pageable pageable); + + Order save(Order order); + + void confirmOrder(Long orderId); + + void cancelOrder(Long orderId); +} 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 ac9df152..4718dbf4 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 @@ -5,14 +5,14 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.transaction.annotation.Transactional; -import org.store.clothstar.order.entity.OrderEntity; +import org.store.clothstar.order.domain.Order; import java.util.List; -public interface JpaOrderSellerRepository extends JpaRepository, OrderSellerRepository { +public interface JpaOrderSellerRepository extends JpaRepository, OrderSellerRepository { @Query("SELECT o FROM orders o WHERE o.status = 'WAITING'") - List findWaitingOrders(); + List findWaitingOrders(); @Transactional @Modifying diff --git a/src/main/java/org/store/clothstar/order/repository/orderSeller/OrderSellerRepository.java b/src/main/java/org/store/clothstar/order/repository/orderSeller/OrderSellerRepository.java index 971da6d1..b29e6116 100644 --- a/src/main/java/org/store/clothstar/order/repository/orderSeller/OrderSellerRepository.java +++ b/src/main/java/org/store/clothstar/order/repository/orderSeller/OrderSellerRepository.java @@ -1,12 +1,12 @@ package org.store.clothstar.order.repository.orderSeller; -import org.store.clothstar.order.entity.OrderEntity; +import org.store.clothstar.order.domain.Order; import java.util.List; public interface OrderSellerRepository { - List findWaitingOrders(); + List findWaitingOrders(); void approveOrder(Long orderId); diff --git a/src/main/java/org/store/clothstar/order/service/OrderApplicationService.java b/src/main/java/org/store/clothstar/order/service/OrderApplicationService.java index 2b226e4e..6b5e0e6a 100644 --- a/src/main/java/org/store/clothstar/order/service/OrderApplicationService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderApplicationService.java @@ -4,7 +4,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.store.clothstar.order.dto.request.OrderRequestWrapper; -import org.store.clothstar.orderDetail.service.OrderDetailService; @Service @RequiredArgsConstructor diff --git a/src/main/java/org/store/clothstar/orderDetail/service/OrderDetailService.java b/src/main/java/org/store/clothstar/order/service/OrderDetailService.java similarity index 64% rename from src/main/java/org/store/clothstar/orderDetail/service/OrderDetailService.java rename to src/main/java/org/store/clothstar/order/service/OrderDetailService.java index 00040613..b7ef150b 100644 --- a/src/main/java/org/store/clothstar/orderDetail/service/OrderDetailService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderDetailService.java @@ -1,18 +1,17 @@ -package org.store.clothstar.orderDetail.service; +package org.store.clothstar.order.service; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Qualifier; 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.order.entity.OrderEntity; -import org.store.clothstar.order.repository.order.OrderRepository; -import org.store.clothstar.order.type.Status; -import org.store.clothstar.orderDetail.dto.request.AddOrderDetailRequest; -import org.store.clothstar.orderDetail.dto.request.CreateOrderDetailRequest; -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; -import org.store.clothstar.orderDetail.repository.OrderDetailRepository; +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.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; import org.store.clothstar.product.service.ProductService; @@ -24,19 +23,19 @@ @Slf4j @Service public class OrderDetailService { - private final OrderRepository orderRepository; + private final OrderUserRepository orderUserRepository; private final ProductService productService; private final OrderDetailRepository orderDetailRepository; private final ProductJPARepository productJPARepository; private final ProductLineJPARepository productLineJPARepository; public OrderDetailService( - @Qualifier("jpaOrderDetailRepository") OrderDetailRepository orderDetailRepository, - @Qualifier("jpaOrderRepository") OrderRepository orderRepository, ProductService productService + OrderDetailRepository orderDetailRepository, + OrderUserRepository orderUserRepository, ProductService productService , ProductJPARepository productJPARepository , ProductLineJPARepository productLineJPARepository ) { - this.orderRepository = orderRepository; + this.orderUserRepository = orderUserRepository; this.orderDetailRepository = orderDetailRepository; this.productService = productService; this.productJPARepository = productJPARepository; @@ -47,7 +46,7 @@ public OrderDetailService( @Transactional public void saveOrderDetailWithOrder(CreateOrderDetailRequest createOrderDetailRequest, long orderId) { - OrderEntity orderEntity = orderRepository.findById(orderId) + Order order = orderUserRepository.findById(orderId) .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "주문 정보를 찾을 수 없습니다.")); ProductLineEntity productLineEntity = productLineJPARepository.findById(createOrderDetailRequest.getProductLineId()) @@ -61,25 +60,25 @@ public void saveOrderDetailWithOrder(CreateOrderDetailRequest createOrderDetailR throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문 개수가 재고보다 더 많습니다."); } - OrderDetailEntity orderDetailEntity = createOrderDetailRequest.toOrderDetailEntity(orderEntity, productLineEntity, productEntity); - orderDetailRepository.save(orderDetailEntity); + OrderDetail orderDetail = createOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity); + orderDetailRepository.save(orderDetail); // 주문 정보 업데이트: 주문 상세 생성에 따른, 총 상품 금액과 총 결제 금액 업데이트 - int newTotalProductsPrice = orderEntity.getTotalProductsPrice() + orderDetailEntity.getOneKindTotalPrice(); + int newTotalProductsPrice = order.getTotalPrice().getProducts() + orderDetail.getPrice().getOneKindTotalPrice(); int newTotalPaymentPrice = - orderEntity.getTotalProductsPrice() + orderEntity.getTotalShippingPrice() + orderDetailEntity.getOneKindTotalPrice(); + order.getTotalPrice().getProducts() + order.getTotalPrice().getShipping() + orderDetail.getPrice().getOneKindTotalPrice(); - orderEntity.updatePrices(newTotalProductsPrice, newTotalPaymentPrice); + order.getTotalPrice().updatePrices(newTotalProductsPrice, newTotalPaymentPrice); // 주문 수량만큼 상품 재고 차감 - updateProductStock(productEntity, orderDetailEntity.getQuantity()); + updateProductStock(productEntity, orderDetail.getQuantity()); } // 주문 상세 추가 생성 @Transactional public Long addOrderDetail(AddOrderDetailRequest addOrderDetailRequest) { - OrderEntity orderEntity = orderRepository.findById(addOrderDetailRequest.getOrderId()) + Order order = orderUserRepository.findById(addOrderDetailRequest.getOrderId()) .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "주문 정보를 찾을 수 없습니다.")); ProductLineEntity productLineEntity = productLineJPARepository.findById(addOrderDetailRequest.getProductLineId()) @@ -92,35 +91,35 @@ public Long addOrderDetail(AddOrderDetailRequest addOrderDetailRequest) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문 개수가 재고보다 더 많습니다."); } - if (!orderEntity.getStatus().equals(Status.WAITING)) { + if (!order.getStatus().equals(Status.WAITING)) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문이 이미 처리된 상태에서는 추가 주문이 불가능합니다."); } - OrderDetailEntity orderDetailEntity = addOrderDetailRequest.toOrderDetailEntity(orderEntity, productLineEntity, productEntity); - orderDetailRepository.save(orderDetailEntity); + OrderDetail orderDetail = addOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity); + orderDetailRepository.save(orderDetail); - int newTotalProductsPrice = orderEntity.getTotalProductsPrice() + orderDetailEntity.getOneKindTotalPrice(); + int newTotalProductsPrice = order.getTotalPrice().getProducts() + orderDetail.getPrice().getOneKindTotalPrice(); int newTotalPaymentPrice = - orderEntity.getTotalProductsPrice() + orderEntity.getTotalShippingPrice() + orderDetailEntity.getOneKindTotalPrice(); + order.getTotalPrice().getProducts() + order.getTotalPrice().getShipping() + orderDetail.getPrice().getOneKindTotalPrice(); - orderEntity.updatePrices(newTotalProductsPrice, newTotalPaymentPrice); + order.getTotalPrice().updatePrices(newTotalProductsPrice, newTotalPaymentPrice); - updateProductStock(productEntity, orderDetailEntity.getQuantity()); + updateProductStock(productEntity, orderDetail.getQuantity()); - return orderDetailEntity.getOrderDetailId(); + return orderDetail.getOrderDetailId(); } @Transactional public void updateDeleteAt(Long orderDetailId) { - OrderDetailEntity orderDetailEntity = orderDetailRepository.findById(orderDetailId) + OrderDetail orderDetail = orderDetailRepository.findById(orderDetailId) .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "주문상세 번호를 찾을 수 없습니다.")); - if (orderDetailEntity.getDeletedAt() != null) { + if (orderDetail.getDeletedAt() != null) { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "이미 삭제된 주문입니다."); } restoreStockByOrderDetail(orderDetailId); - orderDetailEntity.updateDeletedAt(); + orderDetail.updateDeletedAt(); } @Transactional @@ -131,14 +130,14 @@ public void updateProductStock(ProductEntity productEntity, int quantity) { @Transactional public void restoreStockByOrder(Long orderId) { - List orderDetailList = orderDetailRepository.findOrderDetailListByOrderId(orderId); + List orderDetailList = orderDetailRepository.findOrderDetailListByOrderId(orderId); productService.restoreProductStockByOrder(orderDetailList); } @Transactional public void restoreStockByOrderDetail(Long orderDetailId) { - OrderDetailEntity orderDetailEntity = orderDetailRepository.findById(orderDetailId) + OrderDetail orderDetail = orderDetailRepository.findById(orderDetailId) .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "주문상세 번호를 찾을 수 없습니다.")); - productService.restoreProductStockByOrderDetail(orderDetailEntity); + productService.restoreProductStockByOrderDetail(orderDetail); } } 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 dd64121e..6e66fba0 100644 --- a/src/main/java/org/store/clothstar/order/service/OrderSellerService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderSellerService.java @@ -9,14 +9,13 @@ 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.dto.reponse.OrderResponse; -import org.store.clothstar.order.entity.OrderEntity; -import org.store.clothstar.order.repository.order.OrderRepository; +import org.store.clothstar.order.repository.order.OrderUserRepository; import org.store.clothstar.order.repository.orderSeller.OrderSellerRepository; -import org.store.clothstar.order.type.Status; -import org.store.clothstar.orderDetail.dto.OrderDetailDTO; -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; -import org.store.clothstar.orderDetail.service.OrderDetailService; +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; @@ -30,7 +29,7 @@ public class OrderSellerService { private final OrderSellerRepository orderSellerRepository; - private final OrderRepository orderRepository; + private final OrderUserRepository orderUserRepository; private final OrderDetailService orderDetailService; private final MemberService memberService; private final AddressService addressService; @@ -39,13 +38,13 @@ public class OrderSellerService { public OrderSellerService( OrderSellerRepository orderSellerRepository - , OrderRepository orderRepository + , OrderUserRepository orderUserRepository , OrderDetailService orderDetailService , MemberService memberService, AddressService addressService , ProductService productService, ProductLineService productLineService ) { this.orderSellerRepository = orderSellerRepository; - this.orderRepository = orderRepository; + this.orderUserRepository = orderUserRepository; this.orderDetailService=orderDetailService; this.memberService = memberService; this.addressService = addressService; @@ -55,23 +54,23 @@ public OrderSellerService( @Transactional(readOnly = true) public List getWaitingOrder() { - List waitingOrders = orderSellerRepository.findWaitingOrders(); + List waitingOrders = orderSellerRepository.findWaitingOrders(); - List filteredOrders = waitingOrders.stream() - .filter(orderEntity -> orderEntity.getDeletedAt() == null) + List filteredOrders = waitingOrders.stream() + .filter(order -> order.getDeletedAt() == null) .toList(); return filteredOrders.stream() - .map(orderEntity -> { - Member member = memberService.getMemberByMemberId(orderEntity.getMemberId()); - Address address = addressService.getAddressById(orderEntity.getAddressId()); - OrderResponse orderResponse = OrderResponse.from(orderEntity, member, address); + .map(order -> { + Member member = memberService.getMemberByMemberId(order.getMemberId()); + Address address = addressService.getAddressById(order.getAddressId()); + OrderResponse orderResponse = OrderResponse.from(order, member, address); - List orderDetails = orderEntity.getOrderDetails().stream() - .filter(orderDetailEntity -> orderDetailEntity.getDeletedAt() == null) + List orderDetails = order.getOrderDetails().stream() + .filter(orderDetail -> orderDetail.getDeletedAt() == null) .toList(); - List productIds = orderDetails.stream().map(OrderDetailEntity::getProductId).collect(Collectors.toList()); - List productLineIds = orderDetails.stream().map(OrderDetailEntity::getProductLineId).collect(Collectors.toList()); + List productIds = orderDetails.stream().map(OrderDetail::getProductId).collect(Collectors.toList()); + List productLineIds = orderDetails.stream().map(OrderDetail::getProductLineId).collect(Collectors.toList()); List products = productService.findByIdIn(productIds); List productLines = productLineService.findByIdIn(productLineIds); @@ -79,10 +78,10 @@ public List getWaitingOrder() { Map productMap = products.stream().collect(Collectors.toMap(ProductEntity::getId, product -> product)); Map productLineMap = productLines.stream().collect(Collectors.toMap(ProductLineEntity::getId, productLine -> productLine)); - List orderDetailDTOList = orderDetails.stream().map(orderDetailEntity -> { - ProductEntity productEntity = productMap.get(orderDetailEntity.getProductId()); - ProductLineEntity productLineEntity = productLineMap.get(orderDetailEntity.getProductLineId()); - return OrderDetailDTO.from(orderDetailEntity, productEntity, productLineEntity); + List orderDetailDTOList = orderDetails.stream().map(orderDetail -> { + ProductEntity productEntity = productMap.get(orderDetail.getProductId()); + ProductLineEntity productLineEntity = productLineMap.get(orderDetail.getProductLineId()); + return OrderDetailDTO.from(orderDetail, productEntity, productLineEntity); }).collect(Collectors.toList()); orderResponse.setterOrderDetailList(orderDetailDTOList); @@ -97,8 +96,8 @@ public MessageDTO approveOrder(Long orderId) { MessageDTO messageDTO; // 주문 유효성 검사 - orderRepository.findById(orderId) - .filter(OrderEntity -> OrderEntity.getStatus() == Status.WAITING) + orderUserRepository.findById(orderId) + .filter(Order -> Order.getStatus() == Status.WAITING) .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문이 존재하지 않거나 상태가 'WAITING'이 아니어서 처리할 수 없습니다.")); orderSellerRepository.approveOrder(orderId); @@ -112,7 +111,7 @@ public MessageDTO cancelOrder(Long orderId) { MessageDTO messageDTO; // 주문 유효성 검사 - orderRepository.findById(orderId) + orderUserRepository.findById(orderId) .filter(Order -> Order.getStatus() == Status.WAITING) .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문이 존재하지 않거나 상태가 'WAITING'이 아니어서 처리할 수 없습니다.")); 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 eb0b855c..8f4ab4c9 100644 --- a/src/main/java/org/store/clothstar/order/service/OrderService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderService.java @@ -12,15 +12,14 @@ 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.dto.reponse.OrderResponse; import org.store.clothstar.order.dto.request.CreateOrderRequest; -import org.store.clothstar.order.entity.OrderEntity; -import org.store.clothstar.order.repository.order.OrderRepository; -import org.store.clothstar.order.type.Status; -import org.store.clothstar.orderDetail.dto.OrderDetailDTO; -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; -import org.store.clothstar.orderDetail.repository.OrderDetailRepository; -import org.store.clothstar.orderDetail.service.OrderDetailService; +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.product.entity.ProductEntity; import org.store.clothstar.product.service.ProductService; import org.store.clothstar.productLine.entity.ProductLineEntity; @@ -34,7 +33,7 @@ @Service public class OrderService { - private final OrderRepository orderRepository; + private final OrderUserRepository orderUserRepository; private final MemberService memberService; private final AddressService addressService; private final OrderDetailRepository orderDetailRepository; @@ -43,11 +42,11 @@ public class OrderService { private final ProductLineService productLineService; public OrderService( - OrderRepository orderRepository + OrderUserRepository orderUserRepository , MemberService memberService, AddressService addressService , OrderDetailService orderDetailService, OrderDetailRepository orderDetailRepository, ProductService productService, ProductLineService productLineService) { - this.orderRepository = orderRepository; + this.orderUserRepository = orderUserRepository; this.memberService = memberService; this.addressService = addressService; this.orderDetailRepository = orderDetailRepository; @@ -58,23 +57,23 @@ public OrderService( @Transactional(readOnly = true) public OrderResponse getOrder(Long orderId) { - OrderEntity orderEntity = orderRepository.findById(orderId) + Order order = orderUserRepository.findById(orderId) .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "주문을 찾을 수 없습니다")); - if (orderEntity.getDeletedAt() != null) { + if (order.getDeletedAt() != null) { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "삭제된 주문입니다."); } - Member member = memberService.getMemberByMemberId(orderEntity.getMemberId()); - Address address = addressService.getAddressById(orderEntity.getAddressId()); + Member member = memberService.getMemberByMemberId(order.getMemberId()); + Address address = addressService.getAddressById(order.getAddressId()); - OrderResponse orderResponse = OrderResponse.from(orderEntity,member,address); + OrderResponse orderResponse = OrderResponse.from(order,member,address); - List orderDetails = orderEntity.getOrderDetails().stream() - .filter(orderDetailEntity -> orderDetailEntity.getDeletedAt() == null) + List orderDetails = order.getOrderDetails().stream() + .filter(orderDetail -> orderDetail.getDeletedAt() == null) .toList(); - List productIds = orderDetails.stream().map(OrderDetailEntity::getProductId).collect(Collectors.toList()); - List productLineIds = orderDetails.stream().map(OrderDetailEntity::getProductLineId).collect(Collectors.toList()); + List productIds = orderDetails.stream().map(OrderDetail::getProductId).collect(Collectors.toList()); + List productLineIds = orderDetails.stream().map(OrderDetail::getProductLineId).collect(Collectors.toList()); List products = productService.findByIdIn(productIds); List productLines = productLineService.findByIdIn(productLineIds); @@ -82,10 +81,10 @@ public OrderResponse getOrder(Long orderId) { Map productMap = products.stream().collect(Collectors.toMap(ProductEntity::getId, product -> product)); Map productLineMap = productLines.stream().collect(Collectors.toMap(ProductLineEntity::getId, productLine -> productLine)); - List orderDetailDTOList = orderDetails.stream().map(orderDetailEntity -> { - ProductEntity productEntity = productMap.get(orderDetailEntity.getProductId()); - ProductLineEntity productLineEntity = productLineMap.get(orderDetailEntity.getProductLineId()); - return OrderDetailDTO.from(orderDetailEntity, productEntity, productLineEntity); + List orderDetailDTOList = orderDetails.stream().map(orderDetail -> { + ProductEntity productEntity = productMap.get(orderDetail.getProductId()); + ProductLineEntity productLineEntity = productLineMap.get(orderDetail.getProductLineId()); + return OrderDetailDTO.from(orderDetail, productEntity, productLineEntity); }).collect(Collectors.toList()); orderResponse.setterOrderDetailList(orderDetailDTOList); @@ -94,18 +93,18 @@ public OrderResponse getOrder(Long orderId) { } public Page getAllOrderOffsetPaging(Pageable pageable) { - Page orderEntities = orderRepository.findAll(pageable); + Page orderEntities = orderUserRepository.findAll(pageable); - return orderEntities.map(orderEntity -> { - Member member = memberService.getMemberByMemberId(orderEntity.getMemberId()); - Address address = addressService.getAddressById(orderEntity.getAddressId()); - OrderResponse orderResponse = OrderResponse.from(orderEntity, member, address); + return orderEntities.map(order -> { + Member member = memberService.getMemberByMemberId(order.getMemberId()); + Address address = addressService.getAddressById(order.getAddressId()); + OrderResponse orderResponse = OrderResponse.from(order, member, address); - List orderDetails = orderEntity.getOrderDetails().stream() - .filter(orderDetailEntity -> orderDetailEntity.getDeletedAt() == null) + List orderDetails = order.getOrderDetails().stream() + .filter(orderDetail -> orderDetail.getDeletedAt() == null) .toList(); - List productIds = orderDetails.stream().map(OrderDetailEntity::getProductId).collect(Collectors.toList()); - List productLineIds = orderDetails.stream().map(OrderDetailEntity::getProductLineId).collect(Collectors.toList()); + List productIds = orderDetails.stream().map(OrderDetail::getProductId).collect(Collectors.toList()); + List productLineIds = orderDetails.stream().map(OrderDetail::getProductLineId).collect(Collectors.toList()); List products = productService.findByIdIn(productIds); List productLines = productLineService.findByIdIn(productLineIds); @@ -113,10 +112,10 @@ public Page getAllOrderOffsetPaging(Pageable pageable) { Map productMap = products.stream().collect(Collectors.toMap(ProductEntity::getId, product -> product)); Map productLineMap = productLines.stream().collect(Collectors.toMap(ProductLineEntity::getId, productLine -> productLine)); - List orderDetailDTOList = orderDetails.stream().map(orderDetailEntity -> { - ProductEntity productEntity = productMap.get(orderDetailEntity.getProductId()); - ProductLineEntity productLineEntity = productLineMap.get(orderDetailEntity.getProductLineId()); - return OrderDetailDTO.from(orderDetailEntity, productEntity, productLineEntity); + List orderDetailDTOList = orderDetails.stream().map(orderDetail -> { + ProductEntity productEntity = productMap.get(orderDetail.getProductId()); + ProductLineEntity productLineEntity = productLineMap.get(orderDetail.getProductLineId()); + return OrderDetailDTO.from(orderDetail, productEntity, productLineEntity); }).collect(Collectors.toList()); orderResponse.setterOrderDetailList(orderDetailDTOList); @@ -126,18 +125,18 @@ public Page getAllOrderOffsetPaging(Pageable pageable) { } public Slice getAllOrderSlicePaging(Pageable pageable) { - Slice orderEntities = orderRepository.findAll(pageable); + Slice orderEntities = orderUserRepository.findAll(pageable); - return orderEntities.map(orderEntity -> { - Member member = memberService.getMemberByMemberId(orderEntity.getMemberId()); - Address address = addressService.getAddressById(orderEntity.getAddressId()); - OrderResponse orderResponse = OrderResponse.from(orderEntity, member, address); + return orderEntities.map(order -> { + Member member = memberService.getMemberByMemberId(order.getMemberId()); + Address address = addressService.getAddressById(order.getAddressId()); + OrderResponse orderResponse = OrderResponse.from(order, member, address); - List orderDetails = orderEntity.getOrderDetails().stream() - .filter(orderDetailEntity -> orderDetailEntity.getDeletedAt() == null) + List orderDetails = order.getOrderDetails().stream() + .filter(orderDetail -> orderDetail.getDeletedAt() == null) .toList(); - List productIds = orderDetails.stream().map(OrderDetailEntity::getProductId).collect(Collectors.toList()); - List productLineIds = orderDetails.stream().map(OrderDetailEntity::getProductLineId).collect(Collectors.toList()); + List productIds = orderDetails.stream().map(OrderDetail::getProductId).collect(Collectors.toList()); + List productLineIds = orderDetails.stream().map(OrderDetail::getProductLineId).collect(Collectors.toList()); List products = productService.findByIdIn(productIds); List productLines = productLineService.findByIdIn(productLineIds); @@ -145,10 +144,10 @@ public Slice getAllOrderSlicePaging(Pageable pageable) { Map productMap = products.stream().collect(Collectors.toMap(ProductEntity::getId, product -> product)); Map productLineMap = productLines.stream().collect(Collectors.toMap(ProductLineEntity::getId, productLine -> productLine)); - List orderDetailDTOList = orderDetails.stream().map(orderDetailEntity -> { - ProductEntity productEntity = productMap.get(orderDetailEntity.getProductId()); - ProductLineEntity productLineEntity = productLineMap.get(orderDetailEntity.getProductLineId()); - return OrderDetailDTO.from(orderDetailEntity, productEntity, productLineEntity); + List orderDetailDTOList = orderDetails.stream().map(orderDetail -> { + ProductEntity productEntity = productMap.get(orderDetail.getProductId()); + ProductLineEntity productLineEntity = productLineMap.get(orderDetail.getProductLineId()); + return OrderDetailDTO.from(orderDetail, productEntity, productLineEntity); }).collect(Collectors.toList()); orderResponse.setterOrderDetailList(orderDetailDTOList); @@ -163,37 +162,50 @@ public Long saveOrder(CreateOrderRequest createOrderRequest) { Member member = memberService.getMemberByMemberId(createOrderRequest.getMemberId()); Address address = addressService.getAddressById(createOrderRequest.getAddressId()); - OrderEntity orderEntity = createOrderRequest.toOrderEntity(member, address); - orderRepository.save(orderEntity); + Order order = createOrderRequest.toOrder(member, address); + orderUserRepository.save(order); - return orderEntity.getOrderId(); + return order.getOrderId(); } @Transactional - public void deliveredToConfirmOrder(Long orderId) { + public void confirmOrder(Long orderId) { - OrderEntity orderEntity = orderRepository.findById(orderId) + Order order = orderUserRepository.findById(orderId) .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문 정보를 찾을 수 없습니다.")); - if (orderEntity.getStatus() != Status.DELIVERED) { + if (order.getStatus() != Status.DELIVERED) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문 상태가 '배송완료'가 아니기 때문에 주문확정이 불가능합니다."); } - orderRepository.deliveredToConfirmOrder(orderId); + orderUserRepository.confirmOrder(orderId); + } + + public void cancelOrder(Long orderId) { + + Order order = orderUserRepository.findById(orderId) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문 정보를 찾을 수 없습니다.")); + + if (order.getStatus() != Status.WAITING && order.getStatus() != Status.APPROVE) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "'승인대기' 또는 '주문승인' 상태가 아니기 때문에 주문을 취소할 수 없습니다."); + } + + orderUserRepository.cancelOrder(orderId); } @Transactional public void updateDeleteAt(Long orderId) { - OrderEntity orderEntity = orderRepository.findById(orderId) + Order order = orderUserRepository.findById(orderId) .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "주문 번호를 찾을 수 없습니다.")); - if(orderEntity.getDeletedAt() != null){ + if(order.getDeletedAt() != null){ throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "이미 삭제된 주문입니다."); } - List orderDetailList = orderDetailRepository.findOrderDetailListByOrderId(orderId); - orderDetailList.forEach(OrderDetailEntity::updateDeletedAt); + List orderDetailList = orderDetailRepository.findOrderDetailListByOrderId(orderId); + orderDetailList.forEach(OrderDetail::updateDeletedAt); - orderEntity.updateDeletedAt(); + order.updateDeletedAt(); } + } \ No newline at end of file diff --git a/src/main/java/org/store/clothstar/orderDetail/dto/OrderDetailDTO.java b/src/main/java/org/store/clothstar/orderDetail/dto/OrderDetailDTO.java deleted file mode 100644 index 23a2611c..00000000 --- a/src/main/java/org/store/clothstar/orderDetail/dto/OrderDetailDTO.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.store.clothstar.orderDetail.dto; - -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; -import org.store.clothstar.product.entity.ProductEntity; -import org.store.clothstar.productLine.entity.ProductLineEntity; - -@Getter -@AllArgsConstructor -@NoArgsConstructor -@Builder -public class OrderDetailDTO { - - private Long orderDetailId; - private String productName; // 상품명 - private String optionName; - private String brandName; - private int productPrice; // 고정된 상품 가격 ( 주문 당시 가격 ) - private int extraCharge; - private int quantity; - private int totalPrice; // 상품 종류 하나당 총 가격 - - public static OrderDetailDTO from(OrderDetailEntity orderDetailEntity, ProductEntity productEntity, ProductLineEntity productLineEntity){ - return OrderDetailDTO.builder() - .orderDetailId(orderDetailEntity.getOrderDetailId()) - .productName(productLineEntity.getName()) - .optionName(productEntity.getName()) - .brandName(productLineEntity.getSeller().getBrandName()) - .productPrice(productLineEntity.getPrice()) - .extraCharge(productEntity.getExtraCharge()) - .quantity(orderDetailEntity.getQuantity()) - .totalPrice(orderDetailEntity.getOneKindTotalPrice()) - .build(); - } - -// @QueryProjection -// public OrderDetailDTO(OrderDetailEntity orderDetailEntity) { -// this.orderDetailId= orderDetailEntity.getOrderDetailId(); -// this.quantity = orderDetailEntity.getQuantity(); -// this.productPrice = orderDetailEntity.getFixedPrice(); -// this.totalPrice = orderDetailEntity.getOneKindTotalPrice(); -// this.productName = orderDetailEntity.getProductLine().getName(); -// this.optionName = orderDetailEntity.getProduct().getName(); -// this.brandName = orderDetailEntity.getProductLine().getSeller().getBrandName(); -// this.extraCharge = orderDetailEntity.getProduct().getExtraCharge(); -// } -} - diff --git a/src/main/java/org/store/clothstar/orderDetail/repository/OrderDetailRepository.java b/src/main/java/org/store/clothstar/orderDetail/repository/OrderDetailRepository.java deleted file mode 100644 index 3ff066b2..00000000 --- a/src/main/java/org/store/clothstar/orderDetail/repository/OrderDetailRepository.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.store.clothstar.orderDetail.repository; - -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; - -import java.util.List; -import java.util.Optional; - -public interface OrderDetailRepository { - Optional findById(Long orderDetailId); - - OrderDetailEntity save(OrderDetailEntity orderdetailEntity); - - List findOrderDetailListByOrderId(Long orderId); -} 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 af948062..3d2c1f58 100644 --- a/src/main/java/org/store/clothstar/product/service/ProductService.java +++ b/src/main/java/org/store/clothstar/product/service/ProductService.java @@ -7,7 +7,7 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.server.ResponseStatusException; -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; +import org.store.clothstar.order.domain.OrderDetail; import org.store.clothstar.product.dto.request.CreateProductRequest; import org.store.clothstar.product.dto.request.UpdateProductRequest; import org.store.clothstar.product.dto.response.ProductResponse; @@ -75,18 +75,18 @@ public void deleteProduct(Long productId) { } @Transactional - public void restoreProductStockByOrder(List orderDetailList) { - orderDetailList.forEach(orderDetailEntity -> { - ProductEntity productEntity = productRepository.findById(orderDetailEntity.getProductId()) + public void restoreProductStockByOrder(List orderDetailList) { + orderDetailList.forEach(orderDetail -> { + ProductEntity productEntity = productRepository.findById(orderDetail.getProductId()) .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 정보를 찾을 수 없습니다.")); - productEntity.restoreStock(orderDetailEntity.getQuantity()); + productEntity.restoreStock(orderDetail.getQuantity()); }); } - public void restoreProductStockByOrderDetail(OrderDetailEntity orderDetailEntity) { - ProductEntity productEntity = productRepository.findById(orderDetailEntity.getProductId()) + public void restoreProductStockByOrderDetail(OrderDetail orderDetail) { + ProductEntity productEntity = productRepository.findById(orderDetail.getProductId()) .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 정보를 찾을 수 없습니다.")); - productEntity.restoreStock(orderDetailEntity.getQuantity()); + productEntity.restoreStock(orderDetail.getQuantity()); } public List findByIdIn(List productIds) { diff --git a/src/test/java/org/store/clothstar/order/dto/CreateOrderRequestTest.java b/src/test/java/org/store/clothstar/order/dto/CreateOrderRequestTest.java index c82feb05..19e26423 100644 --- a/src/test/java/org/store/clothstar/order/dto/CreateOrderRequestTest.java +++ b/src/test/java/org/store/clothstar/order/dto/CreateOrderRequestTest.java @@ -7,8 +7,8 @@ import org.mockito.junit.jupiter.MockitoExtension; 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; -import org.store.clothstar.order.entity.OrderEntity; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.BDDMockito.given; @@ -30,9 +30,9 @@ void toOrder_test() { given(member.getMemberId()).willReturn(1L); //when - OrderEntity orderEntity = request.toOrderEntity(member, address); + Order order = request.toOrder(member, address); //then - assertEquals(member.getMemberId(), orderEntity.getMemberId()); + assertEquals(member.getMemberId(), order.getMemberId()); } } \ No newline at end of file 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 bf5b04ea..5852384f 100644 --- a/src/test/java/org/store/clothstar/order/service/OrderSellerServiceTest.java +++ b/src/test/java/org/store/clothstar/order/service/OrderSellerServiceTest.java @@ -16,14 +16,15 @@ import org.store.clothstar.member.domain.vo.AddressInfo; 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.vo.Price; +import org.store.clothstar.order.domain.vo.TotalPrice; import org.store.clothstar.order.dto.reponse.OrderResponse; -import org.store.clothstar.order.entity.OrderEntity; -import org.store.clothstar.order.repository.order.OrderRepository; +import org.store.clothstar.order.repository.order.OrderUserRepository; import org.store.clothstar.order.repository.orderSeller.OrderSellerRepository; -import org.store.clothstar.order.type.Status; -import org.store.clothstar.orderDetail.dto.OrderDetailDTO; -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; -import org.store.clothstar.orderDetail.service.OrderDetailService; +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; @@ -47,10 +48,7 @@ class OrderSellerServiceTest { private OrderSellerService orderSellerService; @Mock - private OrderEntity mockOrderEntity; - - @Mock - private OrderRepository orderRepository; + private OrderUserRepository orderUserRepository; @Mock private OrderSellerRepository orderSellerRepository; @@ -77,10 +75,10 @@ class OrderSellerServiceTest { private Address address; @Mock - private OrderEntity orderEntity; + private Order order; @Mock - private OrderDetailEntity orderDetailEntity; + private OrderDetail orderDetail; @Mock private ProductLineEntity productLineEntity; @@ -91,6 +89,12 @@ class OrderSellerServiceTest { @Mock private AddressInfo addressInfo; + @Mock + private TotalPrice totalPrice; + + @Mock + private Price price; + @Mock private Seller seller; @@ -103,29 +107,31 @@ void getWaitingOrder_test() { Long productId = 3L; Long productLineId = 4L; - List waitingOrders = List.of(orderEntity); + List waitingOrders = List.of(order); given(orderSellerRepository.findWaitingOrders()).willReturn(waitingOrders); - given(orderEntity.getMemberId()).willReturn(memberId); - given(orderEntity.getAddressId()).willReturn(addressId); - given(orderEntity.getCreatedAt()).willReturn(LocalDateTime.now()); - given(orderDetailEntity.getDeletedAt()).willReturn(null); - given(orderEntity.getOrderDetails()).willReturn(List.of(orderDetailEntity)); + given(order.getMemberId()).willReturn(memberId); + given(order.getAddressId()).willReturn(addressId); + given(order.getCreatedAt()).willReturn(LocalDateTime.now()); + given(orderDetail.getDeletedAt()).willReturn(null); + given(order.getOrderDetails()).willReturn(List.of(orderDetail)); given(memberService.getMemberByMemberId(memberId)).willReturn(member); given(addressService.getAddressById(addressId)).willReturn(address); given(address.getAddressInfo()).willReturn(addressInfo); - given(orderDetailEntity.getProductId()).willReturn(productId); - given(orderDetailEntity.getProductLineId()).willReturn(productLineId); + given(order.getTotalPrice()).willReturn(totalPrice); + given(orderDetail.getPrice()).willReturn(price); + given(orderDetail.getProductId()).willReturn(productId); + given(orderDetail.getProductLineId()).willReturn(productLineId); given(productService.findByIdIn(List.of(productId))).willReturn(List.of(productEntity)); given(productLineService.findByIdIn(List.of(productLineId))).willReturn(List.of(productLineEntity)); given(productEntity.getId()).willReturn(productId); given(productLineEntity.getId()).willReturn(productLineId); given(productLineEntity.getSeller()).willReturn(seller); - OrderResponse expectedOrderResponse = OrderResponse.from(orderEntity, member, address); - List orderDetails = List.of(orderDetailEntity); + OrderResponse expectedOrderResponse = OrderResponse.from(order, member, address); + List orderDetails = List.of(orderDetail); List orderDetailDTOList = orderDetails.stream() - .map(orderDetailEntity -> OrderDetailDTO.from(orderDetailEntity, productEntity, productLineEntity)) + .map(orderDetail -> OrderDetailDTO.from(orderDetail, productEntity, productLineEntity)) .collect(Collectors.toList()); expectedOrderResponse.setterOrderDetailList(orderDetailDTOList); @@ -148,15 +154,15 @@ void getWaitingOrder_test() { void approveOrder_verify_test() { //given Long orderId = 1L; - given(mockOrderEntity.getStatus()).willReturn(Status.WAITING); - given(orderRepository.findById(orderId)).willReturn(Optional.of(mockOrderEntity)); + given(order.getStatus()).willReturn(Status.WAITING); + given(orderUserRepository.findById(orderId)).willReturn(Optional.of(order)); //when MessageDTO messageDTO = orderSellerService.approveOrder(orderId); //then then(orderSellerRepository).should(times(1)).approveOrder(orderId); - then(orderRepository).should(times(1)).findById(orderId); + then(orderUserRepository).should(times(1)).findById(orderId); assertThat(messageDTO.getStatusCode()).isEqualTo(HttpStatus.OK.value()); assertThat(messageDTO.getMessage()).isEqualTo("주문이 정상적으로 승인 되었습니다."); } @@ -166,15 +172,15 @@ void approveOrder_verify_test() { void cancelOrder_verify_test() { // given Long orderId = 1L; - given(mockOrderEntity.getStatus()).willReturn(Status.WAITING); - given(orderRepository.findById(orderId)).willReturn(Optional.of(mockOrderEntity)); + given(order.getStatus()).willReturn(Status.WAITING); + given(orderUserRepository.findById(orderId)).willReturn(Optional.of(order)); //when MessageDTO messageDTO = orderSellerService.cancelOrder(orderId); //then then(orderSellerRepository).should(times(1)).cancelOrder(orderId); - then(orderRepository).should(times(1)).findById(orderId); + then(orderUserRepository).should(times(1)).findById(orderId); assertThat(messageDTO.getStatusCode()).isEqualTo(HttpStatus.OK.value()); assertThat(messageDTO.getMessage()).isEqualTo("주문이 정상적으로 취소 되었습니다."); } @@ -185,8 +191,8 @@ void approveOrder_NotWAITING_exception_test() { //given Long orderId = 1L; - given(orderRepository.findById(orderId)).willReturn(Optional.of(mockOrderEntity)); - given(mockOrderEntity.getStatus()).willReturn(Status.DELIVERED); + given(orderUserRepository.findById(orderId)).willReturn(Optional.of(order)); + given(order.getStatus()).willReturn(Status.DELIVERED); //when ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> @@ -203,8 +209,8 @@ void cancelOrder_NotWAITING_exception_test() { //given Long orderId = 1L; - given(orderRepository.findById(orderId)).willReturn(Optional.of(mockOrderEntity)); - given(mockOrderEntity.getStatus()).willReturn(Status.DELIVERED); + given(orderUserRepository.findById(orderId)).willReturn(Optional.of(order)); + given(order.getStatus()).willReturn(Status.DELIVERED); //when ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> 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 716acbc1..454e3086 100644 --- a/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java +++ b/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java @@ -17,15 +17,17 @@ import org.store.clothstar.member.domain.vo.AddressInfo; 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.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.entity.OrderEntity; -import org.store.clothstar.order.repository.order.OrderRepository; -import org.store.clothstar.order.type.Status; -import org.store.clothstar.orderDetail.dto.OrderDetailDTO; -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; -import org.store.clothstar.orderDetail.repository.OrderDetailRepository; +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.product.entity.ProductEntity; import org.store.clothstar.product.service.ProductService; import org.store.clothstar.productLine.entity.ProductLineEntity; @@ -66,10 +68,10 @@ class OrderServiceTest { private Address address; @Mock - private OrderEntity orderEntity; + private Order order; @Mock - private OrderDetailEntity orderDetailEntity; + private OrderDetail orderDetail; @Mock private ProductLineEntity productLineEntity; @@ -80,11 +82,17 @@ class OrderServiceTest { @Mock private AddressInfo addressInfo; + @Mock + private TotalPrice totalPrice; + + @Mock + private Price price; + @Mock private Seller seller; @Mock - private OrderRepository orderRepository; + private OrderUserRepository orderUserRepository; @Mock private OrderDetailRepository orderDetailRepository; @@ -99,26 +107,28 @@ void getOrder_test() { Long productId = 4L; Long productLineId = 5L; - given(orderRepository.findById(orderId)).willReturn(Optional.of(orderEntity)); - given(orderEntity.getMemberId()).willReturn(memberId); - given(orderEntity.getAddressId()).willReturn(addressId); - given(orderEntity.getCreatedAt()).willReturn(LocalDateTime.now()); - given(orderEntity.getOrderDetails()).willReturn(List.of(orderDetailEntity)); + given(orderUserRepository.findById(orderId)).willReturn(Optional.of(order)); + given(order.getMemberId()).willReturn(memberId); + given(order.getAddressId()).willReturn(addressId); + given(order.getCreatedAt()).willReturn(LocalDateTime.now()); + given(order.getOrderDetails()).willReturn(List.of(orderDetail)); given(memberService.getMemberByMemberId(memberId)).willReturn(member); given(addressService.getAddressById(addressId)).willReturn(address); given(address.getAddressInfo()).willReturn(addressInfo); - given(orderDetailEntity.getDeletedAt()).willReturn(null); - given(orderDetailEntity.getProductId()).willReturn(productId); - given(orderDetailEntity.getProductLineId()).willReturn(productLineId); + given(order.getTotalPrice()).willReturn(totalPrice); + given(orderDetail.getPrice()).willReturn(price); + given(orderDetail.getDeletedAt()).willReturn(null); + given(orderDetail.getProductId()).willReturn(productId); + given(orderDetail.getProductLineId()).willReturn(productLineId); given(productService.findByIdIn(List.of(productId))).willReturn(List.of(productEntity)); given(productLineService.findByIdIn(List.of(productLineId))).willReturn(List.of(productLineEntity)); given(productEntity.getId()).willReturn(productId); given(productLineEntity.getId()).willReturn(productLineId); given(productLineEntity.getSeller()).willReturn(seller); - OrderResponse expectedOrderResponse = OrderResponse.from(orderEntity, member, address); - expectedOrderResponse.setterOrderDetailList(List.of(OrderDetailDTO.from(orderDetailEntity, productEntity, productLineEntity))); + OrderResponse expectedOrderResponse = OrderResponse.from(order, member, address); + expectedOrderResponse.setterOrderDetailList(List.of(OrderDetailDTO.from(orderDetail, productEntity, productLineEntity))); // when OrderResponse orderResponse = orderService.getOrder(orderId); @@ -126,7 +136,7 @@ void getOrder_test() { // then assertThat(orderResponse).usingRecursiveComparison().isEqualTo(expectedOrderResponse); - then(orderRepository).should(times(1)).findById(orderId); + then(orderUserRepository).should(times(1)).findById(orderId); then(memberService).should(times(1)).getMemberByMemberId(memberId); then(addressService).should(times(1)).getAddressById(addressId); then(productService).should(times(1)).findByIdIn(List.of(productId)); @@ -145,30 +155,32 @@ void getAllOrderOffsetPaging_verify_test() { Long productId = 3L; Long productLineId = 4L; - List waitingOrders = List.of(orderEntity); - Page orderEntities = new PageImpl<>(waitingOrders, pageable, waitingOrders.size()); - given(orderRepository.findAll(pageable)).willReturn(orderEntities); - given(orderEntity.getMemberId()).willReturn(memberId); - given(orderEntity.getAddressId()).willReturn(addressId); - given(orderEntity.getCreatedAt()).willReturn(LocalDateTime.now()); - given(orderEntity.getOrderDetails()).willReturn(List.of(orderDetailEntity)); + List waitingOrders = List.of(order); + Page orderEntities = new PageImpl<>(waitingOrders, pageable, waitingOrders.size()); + given(orderUserRepository.findAll(pageable)).willReturn(orderEntities); + given(order.getMemberId()).willReturn(memberId); + given(order.getAddressId()).willReturn(addressId); + given(order.getCreatedAt()).willReturn(LocalDateTime.now()); + given(order.getOrderDetails()).willReturn(List.of(orderDetail)); given(memberService.getMemberByMemberId(memberId)).willReturn(member); given(addressService.getAddressById(addressId)).willReturn(address); given(address.getAddressInfo()).willReturn(addressInfo); - given(orderDetailEntity.getDeletedAt()).willReturn(null); - given(orderDetailEntity.getProductId()).willReturn(productId); - given(orderDetailEntity.getProductLineId()).willReturn(productLineId); + given(order.getTotalPrice()).willReturn(totalPrice); + given(orderDetail.getPrice()).willReturn(price); + given(orderDetail.getDeletedAt()).willReturn(null); + given(orderDetail.getProductId()).willReturn(productId); + given(orderDetail.getProductLineId()).willReturn(productLineId); given(productService.findByIdIn(List.of(productId))).willReturn(List.of(productEntity)); given(productLineService.findByIdIn(List.of(productLineId))).willReturn(List.of(productLineEntity)); given(productEntity.getId()).willReturn(productId); given(productLineEntity.getId()).willReturn(productLineId); given(productLineEntity.getSeller()).willReturn(seller); - OrderResponse expectedOrderResponse = OrderResponse.from(orderEntity, member, address); - List orderDetails = List.of(orderDetailEntity); + OrderResponse expectedOrderResponse = OrderResponse.from(order, member, address); + List orderDetails = List.of(orderDetail); List orderDetailDTOList = orderDetails.stream() - .map(orderDetailEntity -> OrderDetailDTO.from(orderDetailEntity, productEntity, productLineEntity)) + .map(orderDetail -> OrderDetailDTO.from(orderDetail, productEntity, productLineEntity)) .collect(Collectors.toList()); expectedOrderResponse.setterOrderDetailList(orderDetailDTOList); @@ -179,7 +191,7 @@ void getAllOrderOffsetPaging_verify_test() { assertThat(orderResponses).hasSize(waitingOrders.size()); assertThat(orderResponses.getContent().get(0)).usingRecursiveComparison().isEqualTo(expectedOrderResponse); - verify(orderRepository, times(1)).findAll(pageable); + verify(orderUserRepository, times(1)).findAll(pageable); verify(memberService, times(1)).getMemberByMemberId(memberId); verify(addressService, times(1)).getAddressById(addressId); verify(productService, times(1)).findByIdIn(List.of(productId)); @@ -197,30 +209,32 @@ void getAllOrderSlicePaging_verify_test() { Long productId = 3L; Long productLineId = 4L; - List waitingOrders = List.of(orderEntity); - Page pageOrderEntities = new PageImpl<>(waitingOrders, pageable, waitingOrders.size()); - given(orderRepository.findAll(pageable)).willReturn(pageOrderEntities); - given(orderEntity.getMemberId()).willReturn(memberId); - given(orderEntity.getAddressId()).willReturn(addressId); - given(orderEntity.getCreatedAt()).willReturn(LocalDateTime.now()); - given(orderEntity.getOrderDetails()).willReturn(List.of(orderDetailEntity)); + List waitingOrders = List.of(order); + Page pageOrderEntities = new PageImpl<>(waitingOrders, pageable, waitingOrders.size()); + given(orderUserRepository.findAll(pageable)).willReturn(pageOrderEntities); + given(order.getMemberId()).willReturn(memberId); + given(order.getAddressId()).willReturn(addressId); + given(order.getCreatedAt()).willReturn(LocalDateTime.now()); + given(order.getOrderDetails()).willReturn(List.of(orderDetail)); given(memberService.getMemberByMemberId(memberId)).willReturn(member); given(addressService.getAddressById(addressId)).willReturn(address); given(address.getAddressInfo()).willReturn(addressInfo); - given(orderDetailEntity.getDeletedAt()).willReturn(null); - given(orderDetailEntity.getProductId()).willReturn(productId); - given(orderDetailEntity.getProductLineId()).willReturn(productLineId); + given(order.getTotalPrice()).willReturn(totalPrice); + given(orderDetail.getPrice()).willReturn(price); + given(orderDetail.getDeletedAt()).willReturn(null); + given(orderDetail.getProductId()).willReturn(productId); + given(orderDetail.getProductLineId()).willReturn(productLineId); given(productService.findByIdIn(List.of(productId))).willReturn(List.of(productEntity)); given(productLineService.findByIdIn(List.of(productLineId))).willReturn(List.of(productLineEntity)); given(productEntity.getId()).willReturn(productId); given(productLineEntity.getId()).willReturn(productLineId); given(productLineEntity.getSeller()).willReturn(seller); - OrderResponse expectedOrderResponse = OrderResponse.from(orderEntity, member, address); - List orderDetails = List.of(orderDetailEntity); + OrderResponse expectedOrderResponse = OrderResponse.from(order, member, address); + List orderDetails = List.of(orderDetail); List orderDetailDTOList = orderDetails.stream() - .map(orderDetailEntity -> OrderDetailDTO.from(orderDetailEntity, productEntity, productLineEntity)) + .map(orderDetail -> OrderDetailDTO.from(orderDetail, productEntity, productLineEntity)) .collect(Collectors.toList()); expectedOrderResponse.setterOrderDetailList(orderDetailDTOList); @@ -231,7 +245,7 @@ void getAllOrderSlicePaging_verify_test() { assertThat(orderResponses).hasSize(waitingOrders.size()); assertThat(orderResponses.getContent().get(0)).usingRecursiveComparison().isEqualTo(expectedOrderResponse); - verify(orderRepository, times(1)).findAll(pageable); + verify(orderUserRepository, times(1)).findAll(pageable); verify(memberService, times(1)).getMemberByMemberId(memberId); verify(addressService, times(1)).getAddressById(addressId); verify(productService, times(1)).findByIdIn(List.of(productId)); @@ -242,19 +256,16 @@ void getAllOrderSlicePaging_verify_test() { @DisplayName("saveOrder: 주문 생성 - 메서드 호출 테스트") void saveOrder_verify_test() { //given - OrderEntity orderEntity = mock(OrderEntity.class); OrderRequestWrapper orderRequestWrapper = mock(OrderRequestWrapper.class); CreateOrderRequest createOrderRequest = mock(CreateOrderRequest.class); - Member mockmember = mock(Member.class); - Address mockAddress = mock(Address.class); given(orderRequestWrapper.getCreateOrderRequest()).willReturn(createOrderRequest); given(createOrderRequest.getMemberId()).willReturn(1L); given(createOrderRequest.getAddressId()).willReturn(2L); - given(memberService.getMemberByMemberId(createOrderRequest.getMemberId())).willReturn(mockmember); - given(addressService.getAddressById(createOrderRequest.getAddressId())).willReturn(mockAddress); - given(createOrderRequest.toOrderEntity(mockmember, mockAddress)).willReturn(orderEntity); + 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()); @@ -262,30 +273,27 @@ void saveOrder_verify_test() { //then then(memberService).should(times(1)).getMemberByMemberId(createOrderRequest.getMemberId()); then(addressService).should(times(1)).getAddressById(createOrderRequest.getAddressId()); - then(orderRepository).should(times(1)).save(orderEntity); - verify(orderEntity).getOrderId(); + then(orderUserRepository).should(times(1)).save(order); + verify(order).getOrderId(); } @Test @DisplayName("saveOrder: 주문 생성 - 반환값 테스트") void saveOrder_test() { //given - OrderEntity orderEntity = mock(OrderEntity.class); OrderRequestWrapper orderRequestWrapper = mock(OrderRequestWrapper.class); CreateOrderRequest createOrderRequest = mock(CreateOrderRequest.class); - Member mockmember = mock(Member.class); - Address mockAddress = mock(Address.class); - given(orderEntity.getOrderId()).willReturn(1L); + 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(mockmember); - given(addressService.getAddressById(2L)).willReturn(mockAddress); - given(createOrderRequest.toOrderEntity(mockmember, mockAddress)).willReturn(orderEntity); + 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()); @@ -295,53 +303,85 @@ void saveOrder_test() { } @Test - @DisplayName("deliveredToConfirmOrder: 구매 확정 - 성공 메서드 호출 테스트") - void deliveredToConfirmOrder_verify_test() { + @DisplayName("confirmOrder: 구매 확정 - 성공 메서드 호출 테스트") + void confirmOrder_verify_test() { //given Long orderId = 1L; - OrderEntity orderEntity = mock(OrderEntity.class); mock(OrderResponse.class); - given(orderRepository.findById(1L)).willReturn(Optional.of(orderEntity)); - given(orderEntity.getStatus()).willReturn(Status.DELIVERED); + given(orderUserRepository.findById(1L)).willReturn(Optional.of(order)); + given(order.getStatus()).willReturn(Status.DELIVERED); //when - orderService.deliveredToConfirmOrder(orderId); + orderService.confirmOrder(orderId); //then - then(orderRepository).should(times(1)).findById(orderId); - then(orderRepository).should().deliveredToConfirmOrder(orderId); + then(orderUserRepository).should(times(1)).findById(orderId); + then(orderUserRepository).should().confirmOrder(orderId); } @Test - @DisplayName("deliveredToConfirmOrder: 구매 확정 - 실패 예외처리 테스트") - void deliveredToConfirmOrder_fail_exception_test() { + @DisplayName("confirmOrder: 구매 확정 - 실패 예외처리 테스트") + void confirmOrder_fail_exception_test() { //given Long orderId = 1L; - OrderEntity mockOrder = mock(OrderEntity.class); - given(mockOrder.getStatus()).willReturn(Status.APPROVE); - given(orderRepository.findById(orderId)).willReturn(Optional.of(mockOrder)); + given(order.getStatus()).willReturn(Status.APPROVE); + given(orderUserRepository.findById(orderId)).willReturn(Optional.of(order)); //when ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> - orderService.deliveredToConfirmOrder(orderId)); + orderService.confirmOrder(orderId)); //then assertEquals("400 BAD_REQUEST \"주문 상태가 '배송완료'가 아니기 때문에 주문확정이 불가능합니다.\"", thrown.getMessage()); } + @Test + @DisplayName("cancelOrder: 구매 취소 - 성공 메서드 호출 테스트") + void cancelOrder_verify_test() { + //given + Long orderId = 1L; + mock(OrderResponse.class); + + given(orderUserRepository.findById(1L)).willReturn(Optional.of(order)); + given(order.getStatus()).willReturn(Status.APPROVE); + + //when + orderService.cancelOrder(orderId); + + //then + then(orderUserRepository).should(times(1)).findById(orderId); + then(orderUserRepository).should().cancelOrder(orderId); + } + + @Test + @DisplayName("cancelOrder: 구매 취소 - 실패 예외처리 테스트") + void cancelOrder_fail_exception_test() { + //given + Long orderId = 1L; + + given(order.getStatus()).willReturn(Status.DELIVERED); + given(orderUserRepository.findById(orderId)).willReturn(Optional.of(order)); + + //when + ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> + orderService.cancelOrder(orderId)); + + //then + assertEquals("400 BAD_REQUEST \"'승인대기' 또는 '주문승인' 상태가 아니기 때문에 주문을 취소할 수 없습니다.\"", thrown.getMessage()); + } + @Test @DisplayName("updateDeleteAt: 주문 삭제 - 메서드 호출 테스트") void updateDeleteAt_verify_test() { //given Long orderId = 1L; - OrderEntity orderEntity = mock(OrderEntity.class); - OrderDetailEntity mockOrderDetail1 = mock(OrderDetailEntity.class); - OrderDetailEntity mockOrderDetail2 = mock(OrderDetailEntity.class); - OrderDetailEntity mockOrderDetail3 = mock(OrderDetailEntity.class); - List orderDetailList = List.of(mockOrderDetail1, mockOrderDetail2, mockOrderDetail3); - given(orderRepository.findById(1L)).willReturn(Optional.of(orderEntity)); + OrderDetail mockOrderDetail1 = mock(OrderDetail.class); + OrderDetail mockOrderDetail2 = mock(OrderDetail.class); + OrderDetail mockOrderDetail3 = mock(OrderDetail.class); + List orderDetailList = List.of(mockOrderDetail1, mockOrderDetail2, mockOrderDetail3); + given(orderUserRepository.findById(1L)).willReturn(Optional.of(order)); given(orderDetailRepository.findOrderDetailListByOrderId(orderId)).willReturn(orderDetailList); //when @@ -351,17 +391,17 @@ void updateDeleteAt_verify_test() { verify(mockOrderDetail1, times(1)).updateDeletedAt(); verify(mockOrderDetail2, times(1)).updateDeletedAt(); verify(mockOrderDetail3, times(1)).updateDeletedAt(); - then(orderRepository).should(times(1)).findById(orderId); + then(orderUserRepository).should(times(1)).findById(orderId); then(orderDetailRepository).should().findOrderDetailListByOrderId(orderId); - then(orderEntity).should(times(1)).updateDeletedAt(); + then(order).should(times(1)).updateDeletedAt(); } @Test - @DisplayName("updateDeleteAt: 주문 삭제 - orderEntity null 예외처리 테스트") - void updateDeleteAt_orderEntityNull_exception_test() { + @DisplayName("updateDeleteAt: 주문 삭제 - order null 예외처리 테스트") + void updateDeleteAt_orderNull_exception_test() { //given Long orderId = 1L; - given(orderRepository.findById(1L)).willReturn(Optional.empty()); + given(orderUserRepository.findById(1L)).willReturn(Optional.empty()); //when ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> @@ -376,9 +416,8 @@ void updateDeleteAt_orderEntityNull_exception_test() { void updateDeleteAt_alreadyDelete_exception_test() { //given Long orderId = 1L; - OrderEntity orderEntity = mock(OrderEntity.class); - given(orderRepository.findById(1L)).willReturn(Optional.of(orderEntity)); - given(orderEntity.getDeletedAt()).willReturn(LocalDateTime.now()); + given(orderUserRepository.findById(1L)).willReturn(Optional.of(order)); + given(order.getDeletedAt()).willReturn(LocalDateTime.now()); //when ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> diff --git a/src/test/java/org/store/clothstar/orderDetail/service/OrderDetailServiceTest.java b/src/test/java/org/store/clothstar/orderDetail/service/OrderDetailServiceTest.java index 29794fec..f9c2b40a 100644 --- a/src/test/java/org/store/clothstar/orderDetail/service/OrderDetailServiceTest.java +++ b/src/test/java/org/store/clothstar/orderDetail/service/OrderDetailServiceTest.java @@ -7,13 +7,16 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.web.server.ResponseStatusException; -import org.store.clothstar.order.entity.OrderEntity; -import org.store.clothstar.order.repository.order.OrderRepository; -import org.store.clothstar.order.type.Status; -import org.store.clothstar.orderDetail.dto.request.AddOrderDetailRequest; -import org.store.clothstar.orderDetail.dto.request.CreateOrderDetailRequest; -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; -import org.store.clothstar.orderDetail.repository.OrderDetailRepository; +import org.store.clothstar.order.domain.Order; +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.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; import org.store.clothstar.product.service.ProductService; @@ -38,7 +41,7 @@ class OrderDetailServiceTest { private ProductService productService; @Mock - private OrderRepository orderRepository; + private OrderUserRepository orderUserRepository; @Mock private OrderDetailRepository orderDetailRepository; @@ -50,7 +53,22 @@ class OrderDetailServiceTest { private ProductJPARepository productJPARepository; @Mock - private OrderDetailEntity orderDetailEntity; + private OrderDetail orderDetail; + + @Mock + private Order order; + + @Mock + private ProductLineEntity productLine; + + @Mock + private ProductEntity product; + + @Mock + private TotalPrice totalPrice; + + @Mock + private Price price; @DisplayName("saveOrderDetailWithOrder: 주문상세 생성 - 메서드 호출 테스트") @Test @@ -58,25 +76,22 @@ void saveOrderDetailWithOrder_verify_test() { //given long orderId = 1L; CreateOrderDetailRequest mockRequest = mock(CreateOrderDetailRequest.class); - OrderDetailEntity mockOrderDetail = mock(OrderDetailEntity.class); - ProductLineEntity mockProductLine = mock(ProductLineEntity.class); - ProductEntity mockProduct = mock(ProductEntity.class); - OrderEntity mockOrder = mock(OrderEntity.class); - given(orderRepository.findById(orderId)).willReturn(Optional.of(mockOrder)); - given(productLineJPARepository.findById(mockRequest.getProductLineId())).willReturn(Optional.of(mockProductLine)); - given(productJPARepository.findById(mockRequest.getProductId())).willReturn(Optional.of(mockProduct)); - given(mockRequest.toOrderDetailEntity(mockOrder, mockProductLine, mockProduct)).willReturn(mockOrderDetail); + 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(orderRepository).should(times(1)).findById(orderId); + 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(mockOrderDetail); -// then(orderRepository).should(times(1)).updateOrderPrices(mockOrder); + then(orderDetailRepository).should(times(1)).save(orderDetail); } @DisplayName("saveOrderDetailWithOrder: 주문상세 생성 - 주문 수량이 상품 재고보다 클 때 예외처리 테스트") @@ -87,9 +102,8 @@ void saveOrderDetailWithOrder_exception_test() { CreateOrderDetailRequest mockRequest = mock(CreateOrderDetailRequest.class); ProductLineEntity mockProductLine = mock(ProductLineEntity.class); ProductEntity mockProduct = mock(ProductEntity.class); - OrderEntity mockOrder = mock(OrderEntity.class); - given(orderRepository.findById(orderId)).willReturn(Optional.of(mockOrder)); + 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); @@ -110,9 +124,8 @@ void getOrderDetail_quantityZero_exception_test() { AddOrderDetailRequest mockRequest = mock(AddOrderDetailRequest.class); ProductLineEntity mockProductLine = mock(ProductLineEntity.class); ProductEntity mockProduct = mock(ProductEntity.class); - OrderEntity mockOrder = mock(OrderEntity.class); - given(orderRepository.findById(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder)); + given(orderUserRepository.findById(mockRequest.getOrderId())).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); @@ -132,17 +145,17 @@ void getOrderDetail_quantityZero_exception_test() { void addOrderDetail_test() { //given AddOrderDetailRequest mockRequest = mock(AddOrderDetailRequest.class); - OrderDetailEntity mockOrderDetail = mock(OrderDetailEntity.class); ProductLineEntity mockProductLine = mock(ProductLineEntity.class); ProductEntity mockProduct = mock(ProductEntity.class); - OrderEntity mockOrder = mock(OrderEntity.class); - given(mockOrderDetail.getOrderDetailId()).willReturn(1L); - given(mockOrder.getStatus()).willReturn(Status.WAITING); - given(orderRepository.findById(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder)); + given(orderDetail.getOrderDetailId()).willReturn(1L); + given(order.getStatus()).willReturn(Status.WAITING); + given(order.getTotalPrice()).willReturn(totalPrice); + given(orderDetail.getPrice()).willReturn(price); + given(orderUserRepository.findById(mockRequest.getOrderId())).willReturn(Optional.of(order)); given(productLineJPARepository.findById(mockRequest.getProductLineId())).willReturn(Optional.of(mockProductLine)); given(productJPARepository.findById(mockRequest.getProductId())).willReturn(Optional.of(mockProduct)); - given(mockRequest.toOrderDetailEntity(mockOrder, mockProductLine, mockProduct)).willReturn(mockOrderDetail); + given(mockRequest.toOrderDetail(order, mockProductLine, mockProduct)).willReturn(orderDetail); //when Long orderDetailId = orderDetailService.addOrderDetail(mockRequest); @@ -156,25 +169,25 @@ void addOrderDetail_test() { void addOrderDetail_verify_test() { //given AddOrderDetailRequest mockRequest = mock(AddOrderDetailRequest.class); - OrderDetailEntity mockOrderDetail = mock(OrderDetailEntity.class); ProductLineEntity mockProductLine = mock(ProductLineEntity.class); ProductEntity mockProduct = mock(ProductEntity.class); - OrderEntity mockOrder = mock(OrderEntity.class); - given(mockOrder.getStatus()).willReturn(Status.WAITING); - given(orderRepository.findById(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder)); + given(order.getStatus()).willReturn(Status.WAITING); + given(order.getTotalPrice()).willReturn(totalPrice); + given(orderDetail.getPrice()).willReturn(price); + given(orderUserRepository.findById(mockRequest.getOrderId())).willReturn(Optional.of(order)); given(productLineJPARepository.findById(mockRequest.getProductLineId())).willReturn(Optional.of(mockProductLine)); given(productJPARepository.findById(mockRequest.getProductId())).willReturn(Optional.of(mockProduct)); - given(mockRequest.toOrderDetailEntity(mockOrder, mockProductLine, mockProduct)).willReturn(mockOrderDetail); + given(mockRequest.toOrderDetail(order, mockProductLine, mockProduct)).willReturn(orderDetail); //when orderDetailService.addOrderDetail(mockRequest); //then - then(orderRepository).should(times(1)).findById(mockRequest.getOrderId()); + then(orderUserRepository).should(times(1)).findById(mockRequest.getOrderId()); then(productLineJPARepository).should(times(1)).findById(mockRequest.getProductLineId()); then(productJPARepository).should(times(1)).findById(mockRequest.getProductId()); - then(orderDetailRepository).should(times(1)).save(mockOrderDetail); + then(orderDetailRepository).should(times(1)).save(orderDetail); } @DisplayName("addOrderDetail: 주문상세 추가 - 주문 유효성 검사 예외처리 테스트") @@ -184,9 +197,8 @@ void addOrderDetail_quantityZero_exception_test() { AddOrderDetailRequest mockRequest = mock(AddOrderDetailRequest.class); ProductLineEntity mockProductLine = mock(ProductLineEntity.class); ProductEntity mockProduct = mock(ProductEntity.class); - OrderEntity mockOrder = mock(OrderEntity.class); - given(orderRepository.findById(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder)); + given(orderUserRepository.findById(mockRequest.getOrderId())).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); @@ -207,10 +219,9 @@ void addOrderDetail_noWAITING_exception_test() { AddOrderDetailRequest mockRequest = mock(AddOrderDetailRequest.class); ProductLineEntity mockProductLine = mock(ProductLineEntity.class); ProductEntity mockProduct = mock(ProductEntity.class); - OrderEntity mockOrder = mock(OrderEntity.class); - given(mockOrder.getStatus()).willReturn(Status.CANCEL); - given(orderRepository.findById(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder)); + given(order.getStatus()).willReturn(Status.CANCEL); + given(orderUserRepository.findById(mockRequest.getOrderId())).willReturn(Optional.of(order)); given(productLineJPARepository.findById(mockRequest.getProductLineId())).willReturn(Optional.of(mockProductLine)); given(productJPARepository.findById(mockRequest.getProductId())).willReturn(Optional.of(mockProduct)); @@ -227,15 +238,15 @@ void addOrderDetail_noWAITING_exception_test() { void updateDeleteAt_verify_test() { //given long orderDetailId = 1L; - given(orderDetailRepository.findById(orderDetailId)).willReturn(Optional.of(orderDetailEntity)); + given(orderDetailRepository.findById(orderDetailId)).willReturn(Optional.of(orderDetail)); //when orderDetailService.updateDeleteAt(orderDetailId); //then then(orderDetailRepository).should(times(2)).findById(orderDetailId); - then(productService).should(times(1)).restoreProductStockByOrderDetail(orderDetailEntity); - then(orderDetailEntity).should(times(1)).updateDeletedAt(); + then(productService).should(times(1)).restoreProductStockByOrderDetail(orderDetail); + then(orderDetail).should(times(1)).updateDeletedAt(); } @Test @@ -258,8 +269,8 @@ void updateDeleteAt_null_exception_test() { void updateDeleteAt_alreadyDelete_exception_test() { //given long orderDetailId = 1L; - given(orderDetailRepository.findById(orderDetailId)).willReturn(Optional.of(orderDetailEntity)); - given(orderDetailEntity.getDeletedAt()).willReturn(LocalDateTime.now()); + given(orderDetailRepository.findById(orderDetailId)).willReturn(Optional.of(orderDetail)); + given(orderDetail.getDeletedAt()).willReturn(LocalDateTime.now()); //when ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> @@ -274,10 +285,10 @@ void updateDeleteAt_alreadyDelete_exception_test() { void restoreStockByOrder_verify_test() { //given long orderId = 1L; - OrderDetailEntity mockOrderDetail1 = mock(OrderDetailEntity.class); - OrderDetailEntity mockOrderDetail2 = mock(OrderDetailEntity.class); - OrderDetailEntity mockOrderDetail3 = mock(OrderDetailEntity.class); - List orderDetailList = List.of(mockOrderDetail1, mockOrderDetail2, mockOrderDetail3); + OrderDetail mockOrderDetail1 = mock(OrderDetail.class); + OrderDetail mockOrderDetail2 = mock(OrderDetail.class); + OrderDetail mockOrderDetail3 = mock(OrderDetail.class); + List orderDetailList = List.of(mockOrderDetail1, mockOrderDetail2, mockOrderDetail3); given(orderDetailRepository.findOrderDetailListByOrderId(orderId)).willReturn(orderDetailList); //when @@ -292,7 +303,7 @@ void restoreStockByOrder_verify_test() { void restoreStockByOrderDetail_verify_test() { //given long orderDetailId = 1L; - OrderDetailEntity mockOrderDetail = mock(OrderDetailEntity.class); + OrderDetail mockOrderDetail = mock(OrderDetail.class); given(orderDetailRepository.findById(orderDetailId)).willReturn(Optional.of(mockOrderDetail)); //when