Add notification service

Problem Context

We need to send notifications when orders are placed. Currently we use email, but we plan to add SMS and push notifications later.

Intermediate
100 points

File Changes (1)

src/main/java/com/example/notification/OrderNotificationService.java ADDED
@@ -0 +1 @@
1 +package com.example.notification;
2 +
3 +@Service
4 +public class OrderNotificationService {
5 +
6 + private EmailSender emailSender = new EmailSender();
7 + private final OrderRepository orderRepository;
8 +
9 + public OrderNotificationService(OrderRepository orderRepository) {
10 + this.orderRepository = orderRepository;
11 + }
12 +
13 + public void notifyOrderPlaced(Long orderId) {
14 + Order order = orderRepository.findById(orderId)
15 + .orElseThrow(() -> new OrderNotFoundException(orderId));
16 +
17 + String message = String.format(
18 + "Your order #%d has been placed. Total: $%.2f",
19 + orderId, order.getTotal());
20 +
21 + emailSender.send(order.getCustomerEmail(), "Order Confirmation", message);
22 + }
23 +
24 + public void notifyOrderShipped(Long orderId, String trackingNumber) {
25 + Order order = orderRepository.findById(orderId)
26 + .orElseThrow(() -> new OrderNotFoundException(orderId));
27 +
28 + String message = String.format(
29 + "Your order #%d has shipped! Tracking: %s",
30 + orderId, trackingNumber);
31 +
32 + emailSender.send(order.getCustomerEmail(), "Order Shipped", message);
33 + }
34 +}
Login Required: You must be registered to submit reviews and receive AI feedback. Register or login to start reviewing!

Your Review

Tip: Be thorough! Consider security, performance, code quality, and best practices.
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
Analyzing Your Review
Our AI is carefully evaluating your code review against best practices