Untitled

 avatar
unknown
java
5 months ago
1.3 kB
5
Indexable
@Service
public class OrderProcessingService {

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private NotificationService notificationService;

    public void processOrder(Long orderId) {
        // Fetch order
        Order order = orderRepository.findById(orderId).orElseThrow(() -> new RuntimeException("Order not found"));

        // Fetch products
        List<Product> products = new ArrayList<>();
        for (Long productId : order.getProductIds()) {
            Product product = productRepository.findById(productId)
                    .orElseThrow(() -> new RuntimeException("Product not found"));
            products.add(product);
        }

        // Calculate total price
        double totalPrice = 0;
        for (Product product : products) {
            totalPrice += product.getPrice();
        }

        // Update order status
        order.setTotalPrice(totalPrice);
        order.setStatus("PROCESSED");
        orderRepository.save(order);

        // Send notification
        notificationService.sendNotification(order.getCustomerId(), "Your order has been processed!");

        System.out.println("Order processed successfully!");
    }
}
Editor is loading...
Leave a Comment