Skip to content

Commit

Permalink
feat: implement create order api
Browse files Browse the repository at this point in the history
  • Loading branch information
Kang1221 committed Aug 18, 2024
1 parent eec4668 commit 961b651
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class OrderResponseDto {
private Integer originPrice;
private AddressInfo addressInfo;
private PayMethod paymentMethod;
private LocalDateTime completedAt;
private LocalDateTime paidAt;
private Integer discountPrice;
private Integer charge;
private Integer totalPrice;
Expand Down
75 changes: 56 additions & 19 deletions src/main/java/co/orange/ddanzi/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import co.orange.ddanzi.common.exception.DiscountNotFoundException;
import co.orange.ddanzi.common.exception.PaymentNotFoundException;
import co.orange.ddanzi.domain.order.Order;
import co.orange.ddanzi.domain.order.OrderAgreement;
import co.orange.ddanzi.domain.order.Payment;
import co.orange.ddanzi.domain.order.enums.PayStatus;
import co.orange.ddanzi.domain.order.pk.OrderAgreementId;
import co.orange.ddanzi.domain.product.Discount;
import co.orange.ddanzi.domain.product.Item;
import co.orange.ddanzi.domain.product.OptionDetail;
import co.orange.ddanzi.domain.product.Product;
import co.orange.ddanzi.domain.product.enums.ItemStatus;
import co.orange.ddanzi.domain.term.TermOrder;
import co.orange.ddanzi.domain.user.Address;
import co.orange.ddanzi.domain.user.User;
import co.orange.ddanzi.dto.AddressInfo;
Expand Down Expand Up @@ -44,9 +47,13 @@ public class OrderService {
private final AddressRepository addressRepository;
private final OptionDetailRepository optionDetailRepository;


@Autowired
AddressService addressService;

@Autowired
TermService termService;


@Transactional
public ApiResponse<?> checkOrderProduct(String productId){
Expand Down Expand Up @@ -80,35 +87,44 @@ public ApiResponse<?> checkOrderProduct(String productId){

@Transactional
public ApiResponse<?> createOrder(CreateOrderRequestDto requestDto){
log.info("Checking the payment is done.");
Payment payment = paymentRepository.findById(requestDto.getPaymentId()).orElseThrow(() -> new PaymentNotFoundException());
if(!payment.getPayStatus().equals(PayStatus.PAID))
return ApiResponse.onFailure(Error.PAYMENT_REQUIRED,null);

User user = authUtils.getUser();
if(!payment.getBuyer().equals(user))
return ApiResponse.onFailure(Error.PAYMENT_CANNOT_CHANGE,null);

Item item = itemRepository.findById(requestDto.getItemId()).orElseThrow(() -> new ItemNotFoundException());
item.updateStatus(ItemStatus.CLOSED);
log.info("Updated item status.");

OptionDetail optionDetail = optionDetailRepository.findById(requestDto.getSelectedOptionDetailId()).orElseThrow(null);
OptionDetail optionDetail = new OptionDetail();
if(requestDto.getSelectedOptionDetailId() != null) {
log.info("Selected option detail id : " + requestDto.getSelectedOptionDetailId());
optionDetail = optionDetailRepository.findById(requestDto.getSelectedOptionDetailId()).orElseThrow(null);
}
else
optionDetail = null;
log.info("Setting selected optionDetail");

String orderId = createOrderId(requestDto.getItemId());
Order order = requestDto.toOrder(orderId, authUtils.getUser(), item, optionDetail);
order = orderRepository.save(order);

Product product = item.getProduct();
Discount discount = discountRepository.findById(product.getId()).orElseThrow(() -> new DiscountNotFoundException());
Order order = createOrderRecord(requestDto, user, item, optionDetail);

termService.createOrderAgreement(order, 1L, requestDto.getOrderTerm1());
termService.createOrderAgreement(order, 2L, requestDto.getOrderTerm2());
termService.createOrderAgreement(order, 3L, requestDto.getOrderTerm3());

return ApiResponse.onSuccess(Success.CREATE_ORDER_SUCCESS, OrderResponseDto.builder()
.orderId(order.getId())
.orderStatus(order.getStatus())
.productName(product.getName())
.imgUrl(product.getImgUrl())
.originPrice(product.getOriginPrice())
.addressInfo(addressService.setAddressInfo(authUtils.getUser()))
.paymentMethod(payment.getMethod())
.completedAt(order.getCompletedAt())
.discountPrice(discount.getDiscountPrice())
.charge(payment.getCharge())
.totalPrice(payment.getTotalPrice())
.build());
return ApiResponse.onSuccess(Success.CREATE_ORDER_SUCCESS, setOrderResponseDto(user, order, item, payment));
}

private Order createOrderRecord(CreateOrderRequestDto requestDto, User user, Item item, OptionDetail optionDetail){
String orderId = createOrderId(requestDto.getItemId());
Order order = requestDto.toOrder(orderId, user, item, optionDetail);
order = orderRepository.save(order);
log.info("Create new order, order_id: {}", orderId);
return order;
}

private String createOrderId(String itemId){
Expand All @@ -117,4 +133,25 @@ private String createOrderId(String itemId){
char secondChar = (char) ('A' + random.nextInt(26));
return itemId + firstChar + secondChar;
}

private OrderResponseDto setOrderResponseDto(User user, Order order, Item item, Payment payment){
Product product = item.getProduct();
Discount discount = discountRepository.findById(product.getId()).orElseThrow(() -> new DiscountNotFoundException());

OrderResponseDto responseDto = OrderResponseDto.builder()
.orderId(order.getId())
.orderStatus(order.getStatus())
.productName(product.getName())
.imgUrl(product.getImgUrl())
.originPrice(product.getOriginPrice())
.addressInfo(addressService.setAddressInfo(user))
.paymentMethod(payment.getMethod())
.paidAt(payment.getEndedAt())
.discountPrice(discount.getDiscountPrice())
.charge(payment.getCharge())
.totalPrice(payment.getTotalPrice())
.build();

return responseDto;
}
}

0 comments on commit 961b651

Please sign in to comment.