|
1 | 1 | package com.next.app.api.order.service; |
2 | 2 |
|
3 | | -import com.next.app.api.cart.entity.Cart; |
4 | | -import com.next.app.api.cart.entity.CartItem; |
5 | | -import com.next.app.api.cart.repository.CartRepository; |
6 | 3 | import com.next.app.api.order.controller.dto.OrderRequest; |
7 | 4 | import com.next.app.api.order.entity.Order; |
8 | 5 | import com.next.app.api.order.entity.OrderItem; |
|
17 | 14 |
|
18 | 15 | import java.math.BigDecimal; |
19 | 16 | import java.time.LocalDateTime; |
20 | | -import java.util.List; |
21 | 17 |
|
22 | 18 | @Service |
23 | 19 | @RequiredArgsConstructor |
24 | 20 | @Transactional |
25 | 21 | public class OrderService { |
26 | 22 |
|
27 | 23 | private final OrderRepository orderRepository; |
28 | | - private final CartRepository cartRepository; |
29 | 24 | private final ProductRepository productRepository; |
30 | 25 |
|
31 | | - @Transactional(readOnly = true) |
32 | | - public Order getOrderOrThrow(Long id) { |
33 | | - return orderRepository.findById(id) |
34 | | - .orElseThrow(() -> new EntityNotFoundException("주문을 찾을 수 없습니다.")); |
35 | | - } |
36 | | - |
37 | | - @Transactional(readOnly = true) |
38 | | - public List<Order> listByUser(Long userId) { |
39 | | - return orderRepository.findByUserIdOrderByCreatedAtDesc(userId); |
40 | | - } |
41 | | - |
42 | | - public Order createOrderFromCart(Long userId, String deliveryAddress) { |
43 | | - Cart cart = cartRepository.findByUserId(userId) |
44 | | - .orElseThrow(() -> new EntityNotFoundException("장바구니가 없습니다.")); |
45 | | - |
46 | | - if (cart.getItems().isEmpty()) { |
47 | | - throw new IllegalStateException("장바구니가 비어 있습니다."); |
48 | | - } |
49 | | - |
50 | | - Order order = new Order(); |
51 | | - order.setUserId(userId); |
52 | | - order.setDelivery_address(deliveryAddress); |
53 | | - order.setStatus(OrderStatus.PENDING); |
54 | | - order.setCreatedAt(LocalDateTime.now()); |
55 | | - |
56 | | - BigDecimal total = BigDecimal.ZERO; |
57 | | - |
58 | | - for (CartItem cartItem : cart.getItems()) { |
59 | | - OrderItem orderItem = new OrderItem(); |
60 | | - orderItem.setOrder(order); |
61 | | - |
62 | | - Product product = productRepository.findById(cartItem.getProductId()) |
63 | | - .orElseThrow(() -> new EntityNotFoundException("상품을 찾을 수 없습니다.")); |
64 | | - orderItem.setProduct(product); |
65 | | - orderItem.setQuantity(cartItem.getQuantity()); |
66 | | - orderItem.setPrice(cartItem.getPrice() != null ? cartItem.getPrice() : BigDecimal.ZERO); |
67 | | - |
68 | | - total = total.add(orderItem.getPrice().multiply(BigDecimal.valueOf(orderItem.getQuantity()))); |
69 | | - order.getOrderItems().add(orderItem); |
70 | | - } |
71 | | - |
72 | | - order.setTotalPrice(total); |
73 | | - |
74 | | - Order savedOrder = orderRepository.save(order); |
75 | | - |
76 | | - cart.getItems().clear(); |
77 | | - cart.calculateTotalPrice(); |
78 | | - cartRepository.save(cart); |
79 | | - |
80 | | - return savedOrder; |
81 | | - } |
82 | | - |
83 | 26 | public Order createOrder(OrderRequest request, Long userId, String deliveryAddress) { |
84 | 27 | if (request == null) |
85 | 28 | throw new IllegalArgumentException("주문 요청이 누락되었습니다."); |
@@ -116,21 +59,4 @@ public Order createOrder(OrderRequest request, Long userId, String deliveryAddre |
116 | 59 |
|
117 | 60 | return orderRepository.save(order); |
118 | 61 | } |
119 | | - |
120 | | - public void updateOrderStatus(Long id, OrderStatus status) { |
121 | | - Order order = orderRepository.findById(id) |
122 | | - .orElseThrow(() -> new EntityNotFoundException("해당 주문을 찾을 수 없습니다.")); |
123 | | - order.setStatus(status); |
124 | | - orderRepository.save(order); |
125 | | - } |
126 | | - |
127 | | - public void deleteOrder(Long id) { |
128 | | - orderRepository.deleteById(id); |
129 | | - } |
130 | | - |
131 | | - public boolean isOrderOwner(Long orderId, Long userId) { |
132 | | - return orderRepository.findById(orderId) |
133 | | - .map(o -> userId.equals(o.getUserId())) |
134 | | - .orElse(false); |
135 | | - } |
136 | 62 | } |
0 commit comments