Add order processing service
Problem Context
We need a service to handle order creation, including validation, payment processing, inventory updates, and email notifications.
Intermediate
100 points
File Changes (1)
src/main/java/com/example/service/OrderService.java
ADDED
@@ -0 +1 @@
| 1 | +package com.example.service; |
|
| 2 | + |
|
| 3 | +@Service |
|
| 4 | +public class OrderService { |
|
| 5 | + private final OrderRepository orderRepository; |
|
| 6 | + private final ProductRepository productRepository; |
|
| 7 | + private final JavaMailSender mailSender; |
|
| 8 | + private final StripeClient stripeClient; |
|
| 9 | + |
|
| 10 | + public Order createOrder(OrderRequest request) { |
|
| 11 | + // Validation logic |
|
| 12 | + if (request.getItems().isEmpty()) { |
|
| 13 | + throw new ValidationException("Order must have items"); |
|
| 14 | + } |
|
| 15 | + if (request.getShippingAddress() == null) { |
|
| 16 | + throw new ValidationException("Shipping address required"); |
|
| 17 | + } |
|
| 18 | + |
|
| 19 | + // Inventory check and update |
|
| 20 | + for (OrderItem item : request.getItems()) { |
|
| 21 | + Product product = productRepository.findById(item.getProductId()) |
|
| 22 | + .orElseThrow(() -> new ProductNotFoundException(item.getProductId())); |
|
| 23 | + if (product.getStock() < item.getQuantity()) { |
|
| 24 | + throw new InsufficientStockException(product.getName()); |
|
| 25 | + } |
|
| 26 | + product.setStock(product.getStock() - item.getQuantity()); |
|
| 27 | + productRepository.save(product); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + // Payment processing |
|
| 31 | + BigDecimal total = calculateTotal(request.getItems()); |
|
| 32 | + PaymentIntent intent = stripeClient.paymentIntents().create( |
|
| 33 | + PaymentIntentCreateParams.builder() |
|
| 34 | + .setAmount(total.multiply(new BigDecimal(100)).longValue()) |
|
| 35 | + .setCurrency("usd") |
|
| 36 | + .setPaymentMethod(request.getPaymentMethodId()) |
|
| 37 | + .build()); |
|
| 38 | + |
|
| 39 | + // Save order |
|
| 40 | + Order order = Order.builder() |
|
| 41 | + .userId(request.getUserId()) |
|
| 42 | + .items(request.getItems()) |
|
| 43 | + .total(total) |
|
| 44 | + .paymentIntentId(intent.getId()) |
|
| 45 | + .build(); |
|
| 46 | + orderRepository.save(order); |
|
| 47 | + |
|
| 48 | + // Send confirmation email |
|
| 49 | + MimeMessage message = mailSender.createMimeMessage(); |
|
| 50 | + MimeMessageHelper helper = new MimeMessageHelper(message); |
|
| 51 | + helper.setTo(request.getEmail()); |
|
| 52 | + helper.setSubject("Order Confirmation #" + order.getId()); |
|
| 53 | + helper.setText(buildOrderEmailHtml(order), true); |
|
| 54 | + mailSender.send(message); |
|
| 55 | + |
|
| 56 | + return order; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + private String buildOrderEmailHtml(Order order) { /* ... */ } |
|
| 60 | + private BigDecimal calculateTotal(List<OrderItem> items) { /* ... */ } |
|
| 61 | +} |
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