Skip to content

Commit

Permalink
Merge pull request #101 from ClothingStoreService/refactor/#97-order-…
Browse files Browse the repository at this point in the history
…cascade

Refactor/#97 order cascade
  • Loading branch information
axhtl authored Jul 20, 2024
2 parents 0138753 + fc0d945 commit 2fe4abd
Show file tree
Hide file tree
Showing 32 changed files with 390 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.store.clothstar.common.dto.SaveResponseDTO;
import org.store.clothstar.order.dto.reponse.OrderResponse;
import org.store.clothstar.order.dto.request.OrderRequestWrapper;
import org.store.clothstar.order.service.OrderApplicationService;
import org.store.clothstar.order.service.OrderService;

@Tag(name = "Order", description = "주문(Order) 정보 관리에 대한 API 입니다.")
Expand All @@ -25,7 +24,6 @@
public class OrderUserController {

private final OrderService orderService;
private final OrderApplicationService orderApplicationService;

@Operation(summary = "단일 주문 조회", description = "단일 주문의 정보를 조회한다.")
@GetMapping("/{orderId}")
Expand Down Expand Up @@ -53,7 +51,7 @@ public ResponseEntity<Slice<OrderResponse>> getAllOrderSlicePaging(
@Operation(summary = "주문 생성", description = "단일 주문을 생성한다.")
@PostMapping
public ResponseEntity<SaveResponseDTO> saveOrder(@RequestBody @Validated OrderRequestWrapper orderRequestWrapper) {
Long orderId = orderApplicationService.saveOrderWithTransaction(orderRequestWrapper);
Long orderId = orderService.saveOrder(orderRequestWrapper);
return ResponseEntity.ok(new SaveResponseDTO(
orderId, HttpStatus.OK.value(), "주문이 정상적으로 생성되었습니다."));
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/store/clothstar/order/domain/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.store.clothstar.order.domain.vo.TotalPrice;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
Expand Down Expand Up @@ -42,7 +43,13 @@ public class Order extends BaseEntity {
TotalPrice totalPrice;



public void addOrderDetail(OrderDetail orderDetail) {
if (orderDetails == null) {
orderDetails = new ArrayList<>();
}
orderDetails.add(orderDetail);
orderDetail.setterOrder(this);
}


public void updateDeletedAt() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public class OrderDetail extends BaseEntity {
public void updateDeletedAt() {
this.deletedAt = LocalDateTime.now();
}

public void setterOrder(Order order) {
this.order = order;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.store.clothstar.order.domain.type.Status;
import org.store.clothstar.order.domain.vo.AddressDTO;
import org.store.clothstar.order.domain.vo.OrderDetailDTO;
import org.store.clothstar.order.domain.vo.Price;
import org.store.clothstar.order.domain.vo.TotalPrice;

import java.time.LocalDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
import org.springframework.transaction.annotation.Transactional;
import org.store.clothstar.order.domain.Order;

import java.util.Optional;

public interface JpaOrderUserRepository extends JpaRepository<Order, Long>, OrderUserRepository {

Optional<Order> findByOrderIdAndDeletedAtIsNull(Long orderId);

@Transactional
@Modifying
@Query("UPDATE orders o SET o.status ='CONFIRM' WHERE o.orderId = :orderId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface OrderUserRepository {

Optional<Order> findById(Long orderId);

Optional<Order> findByOrderIdAndDeletedAtIsNull(Long orderId);

Page<Order> findAll(Pageable pageable);

Order save(Order order);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public interface JpaOrderSellerRepository extends JpaRepository<Order, Long>, OrderSellerRepository {

@Query("SELECT o FROM orders o WHERE o.status = 'WAITING'")
@Query("SELECT o FROM orders o WHERE o.status = 'WAITING' AND o.deletedAt IS NULL")
List<Order> findWaitingOrders();

@Transactional
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
import org.store.clothstar.order.domain.Order;
import org.store.clothstar.order.repository.order.OrderUserRepository;
import org.store.clothstar.order.domain.type.Status;
import org.store.clothstar.order.domain.OrderDetail;
import org.store.clothstar.order.domain.type.Status;
import org.store.clothstar.order.dto.request.AddOrderDetailRequest;
import org.store.clothstar.order.dto.request.CreateOrderDetailRequest;
import org.store.clothstar.order.repository.order.OrderDetailRepository;
import org.store.clothstar.order.repository.order.OrderUserRepository;
import org.store.clothstar.product.entity.ProductEntity;
import org.store.clothstar.product.repository.ProductJPARepository;
import org.store.clothstar.product.service.ProductService;
Expand Down Expand Up @@ -42,38 +41,6 @@ public OrderDetailService(
this.productLineJPARepository = productLineJPARepository;
}

// 주문 생성시 같이 호출되는 주문 상세 생성 메서드 - 하나의 트랜잭션으로 묶임
@Transactional
public void saveOrderDetailWithOrder(CreateOrderDetailRequest createOrderDetailRequest, long orderId) {

Order order = orderUserRepository.findById(orderId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "주문 정보를 찾을 수 없습니다."));

ProductLineEntity productLineEntity = productLineJPARepository.findById(createOrderDetailRequest.getProductLineId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 옵션 정보를 찾을 수 없습니다."));

ProductEntity productEntity = productJPARepository.findById(createOrderDetailRequest.getProductId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 정보를 찾을 수 없습니다."));

// 주문상세 생성 유효성 검사: 주문 수량이 상품 재고보다 클 경우, 주문이 생성되지 않는다.
if (createOrderDetailRequest.getQuantity() > productEntity.getStock()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문 개수가 재고보다 더 많습니다.");
}

OrderDetail orderDetail = createOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity);
orderDetailRepository.save(orderDetail);

// 주문 정보 업데이트: 주문 상세 생성에 따른, 총 상품 금액과 총 결제 금액 업데이트
int newTotalProductsPrice = order.getTotalPrice().getProducts() + orderDetail.getPrice().getOneKindTotalPrice();
int newTotalPaymentPrice =
order.getTotalPrice().getProducts() + order.getTotalPrice().getShipping() + orderDetail.getPrice().getOneKindTotalPrice();

order.getTotalPrice().updatePrices(newTotalProductsPrice, newTotalPaymentPrice);

// 주문 수량만큼 상품 재고 차감
updateProductStock(productEntity, orderDetail.getQuantity());
}

// 주문 상세 추가 생성
@Transactional
public Long addOrderDetail(AddOrderDetailRequest addOrderDetailRequest) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.store.clothstar.order.service.OrderSave;

import org.store.clothstar.member.domain.Address;
import org.store.clothstar.member.domain.Member;
import org.store.clothstar.order.domain.Order;
import org.store.clothstar.order.dto.request.CreateOrderRequest;

public interface OrderCreator {
Order createOrder(CreateOrderRequest request,Member member,Address address);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.store.clothstar.order.service.OrderSave;

import org.springframework.stereotype.Service;
import org.store.clothstar.member.domain.Address;
import org.store.clothstar.member.domain.Member;
import org.store.clothstar.order.domain.Order;
import org.store.clothstar.order.dto.request.CreateOrderRequest;


@Service
public class OrderCreatorImpl implements OrderCreator {

@Override
public Order createOrder(CreateOrderRequest request,Member member,Address address) {
return request.toOrder(member, address);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.store.clothstar.order.service.OrderSave;

import org.store.clothstar.order.domain.Order;
import org.store.clothstar.order.domain.OrderDetail;

public interface OrderDetailAdder {
void addOrderDetail(Order order, OrderDetail orderDetail);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.store.clothstar.order.service.OrderSave;

import org.springframework.stereotype.Service;
import org.store.clothstar.order.domain.Order;
import org.store.clothstar.order.domain.OrderDetail;

@Service
public class OrderDetailAdderImpl implements OrderDetailAdder {
@Override
public void addOrderDetail(Order order, OrderDetail orderDetail) {
order.addOrderDetail(orderDetail);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.store.clothstar.order.service.OrderSave;

import org.store.clothstar.order.domain.Order;
import org.store.clothstar.order.domain.OrderDetail;
import org.store.clothstar.order.dto.request.CreateOrderDetailRequest;
import org.store.clothstar.product.entity.ProductEntity;
import org.store.clothstar.productLine.entity.ProductLineEntity;

public interface OrderDetailCreator {
OrderDetail createOrderDetail(CreateOrderDetailRequest createOrderDetailRequest, Order order, ProductLineEntity productLineEntity, ProductEntity productEntity);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.store.clothstar.order.service.OrderSave;

import org.springframework.stereotype.Service;
import org.store.clothstar.order.domain.Order;
import org.store.clothstar.order.domain.OrderDetail;
import org.store.clothstar.order.dto.request.CreateOrderDetailRequest;
import org.store.clothstar.product.entity.ProductEntity;
import org.store.clothstar.productLine.entity.ProductLineEntity;

@Service
public class OrderDetailCreatorImpl implements OrderDetailCreator {
@Override
public OrderDetail createOrderDetail(CreateOrderDetailRequest createOrderDetailRequest, Order order, ProductLineEntity productLineEntity, ProductEntity productEntity) {
return createOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.store.clothstar.order.service.OrderSave;

import org.store.clothstar.order.dto.request.CreateOrderDetailRequest;
import org.store.clothstar.product.entity.ProductEntity;

public interface OrderDetailValidator {
void validateOrderDetail(CreateOrderDetailRequest request, ProductEntity product);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.store.clothstar.order.service.OrderSave;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import org.store.clothstar.order.dto.request.CreateOrderDetailRequest;
import org.store.clothstar.product.entity.ProductEntity;

@Service
public class OrderDetailValidatorImpl implements OrderDetailValidator{
@Override
public void validateOrderDetail(CreateOrderDetailRequest request, ProductEntity product) {
if (request.getQuantity() > product.getStock()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문 개수가 재고보다 더 많습니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.store.clothstar.order.service.OrderSave;

import org.store.clothstar.order.domain.Order;
import org.store.clothstar.order.domain.OrderDetail;

public interface OrderPriceUpdater {
void updateOrderPrice(Order order, OrderDetail orderDetail);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.store.clothstar.order.service.OrderSave;

import org.springframework.stereotype.Service;
import org.store.clothstar.order.domain.Order;
import org.store.clothstar.order.domain.OrderDetail;

@Service
public class OrderPriceUpdaterImpl implements OrderPriceUpdater {
@Override
public void updateOrderPrice(Order order, OrderDetail orderDetail) {
int newTotalProductsPrice = order.getTotalPrice().getProducts() + orderDetail.getPrice().getOneKindTotalPrice();
int newTotalPaymentPrice =
order.getTotalPrice().getProducts() + order.getTotalPrice().getShipping() + orderDetail.getPrice().getOneKindTotalPrice();

order.getTotalPrice().updatePrices(newTotalProductsPrice, newTotalPaymentPrice);
}
}
Loading

0 comments on commit 2fe4abd

Please sign in to comment.