#308 Code Review of Order Status Update

Companies:
Innotech
Themes:
SpringTransactionsCode Review

The following code updates an order status. Find the problems and modify the code:

@Service
public class OrderService {

    @Autowired
    private OrderRepository orderRepository;

    private final NotificationService notificationService;

    @Autowired
    public OrderService(NotificationService notificationService) {
        this.notificationService = notificationService;
    }

    public void updateOrderStatus(Long orderId, String newStatus) {
        Order order = orderRepository.findById(orderId).get();

        if (newStatus.equals("COMPLETED")) {
            order.setStatus("COMPLETED");
            orderRepository.save(order);
            notificationService.notifyUser(order.getUserId(), "Your order is completed");
        } else if (newStatus.equals("CANCELLED")) {
            order.setStatus("CANCELLED");
            orderRepository.save(order);
            notificationService.notifyUser(order.getUserId(), "Your order is cancelled");
        } else if (newStatus.equals("PENDING")) {
            order.setStatus("PENDING");
            orderRepository.save(order);
        } else if (newStatus.equals("IN_PROGRESS")) {
            order.setStatus("IN_PROGRESS");
            orderRepository.save(order);
        } else {
            throw new IllegalArgumentException("Unsupported status: " + newStatus);
        }
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in