Add order fulfillment service
Problem Context
When an order is paid, we need to deduct inventory, update order status, and create a shipping record. All operations must succeed together.
Advanced
200 points
File Changes (1)
src/main/java/com/example/service/OrderFulfillmentService.java
ADDED
@@ -0 +1 @@
| 1 | +package com.example.service; |
|
| 2 | + |
|
| 3 | +import org.springframework.stereotype.Service; |
|
| 4 | +import lombok.RequiredArgsConstructor; |
|
| 5 | + |
|
| 6 | +@Service |
|
| 7 | +@RequiredArgsConstructor |
|
| 8 | +public class OrderFulfillmentService { |
|
| 9 | + |
|
| 10 | + private final OrderRepository orderRepository; |
|
| 11 | + private final InventoryService inventoryService; |
|
| 12 | + private final ShippingService shippingService; |
|
| 13 | + |
|
| 14 | + public void fulfillOrder(Long orderId) { |
|
| 15 | + Order order = orderRepository.findById(orderId) |
|
| 16 | + .orElseThrow(() -> new OrderNotFoundException(orderId)); |
|
| 17 | + |
|
| 18 | + // Deduct inventory for each item |
|
| 19 | + for (OrderItem item : order.getItems()) { |
|
| 20 | + inventoryService.deductStock(item.getProductId(), item.getQuantity()); |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + // Create shipping record |
|
| 24 | + shippingService.createShippingRecord(order); |
|
| 25 | + |
|
| 26 | + // Update order status |
|
| 27 | + order.setStatus(OrderStatus.FULFILLED); |
|
| 28 | + orderRepository.save(order); |
|
| 29 | + } |
|
| 30 | +} |
Login Required: You must be registered to submit reviews and receive AI feedback.
Register or
login to start reviewing!
Your Review
Review Tips
- Look for security vulnerabilities (SQL injection, XSS, etc.)
- Check for null pointer exceptions and error handling
- Consider performance implications
- Evaluate code maintainability and readability
- Check for proper resource management
- Look for logic errors or edge cases