package com.networknt.example.sagas.ordersandcustomers.order.service;


;
import com.networknt.example.sagas.ordersandcustomers.order.command.ApproveOrderCommand;
import com.networknt.example.sagas.ordersandcustomers.order.command.RejectOrderCommand;
import com.networknt.example.sagas.ordersandcustomers.order.domain.Order;
import com.networknt.example.sagas.ordersandcustomers.order.domain.OrderRepository;
import com.networknt.saga.participant.SagaCommandHandlersBuilder;
import com.networknt.tram.command.consumer.CommandHandlers;
import com.networknt.tram.command.consumer.CommandMessage;
import com.networknt.tram.message.common.Message;

import static com.networknt.tram.command.consumer.CommandHandlerReplyBuilder.withSuccess;


public class OrderCommandHandler {

  private OrderRepository orderRepository;

  public OrderCommandHandler(OrderRepository orderRepository) {
    this.orderRepository = orderRepository;
  }

  public CommandHandlers commandHandlerDefinitions() {
    return SagaCommandHandlersBuilder
            .fromChannel("orderService")
            .onMessage(ApproveOrderCommand.class, this::approve)
            .onMessage(RejectOrderCommand.class, this::reject)
            .build();
  }

  public Message approve(CommandMessage<ApproveOrderCommand> cm) {
    long orderId = cm.getCommand().getOrderId();
    Order order = orderRepository.findOne(orderId);
    order.noteCreditReserved();
    System.out.println("order with new status-->" + order.getState().name());
    orderRepository.update(order);
    return withSuccess();
  }

  public Message reject(CommandMessage<RejectOrderCommand> cm) {
    long orderId = cm.getCommand().getOrderId();
    Order order = orderRepository.findOne(orderId);
    order.noteCreditReservationFailed();
    System.out.println("order with new status-->" + order.getState().name());
    orderRepository.update(order);
    return withSuccess();
  }

}

@@ -464,24 +464,87 @@
 ersBuilder;%0A
+import com.networknt.saga.participant.SagaReplyMessageBuilder;%0A
 import com.n
@@ -1591,32 +1591,121 @@
 er);%0A    return 
+SagaReplyMessageBuilder.withLock(Order.class, order.getId()).withSuccess();%0A    //return 
 withSuccess();%0A 
@@ -2035,24 +2035,113 @@
 %0A    return 
+SagaReplyMessageBuilder.withLock(Order.class, order.getId()).withSuccess();%0A    //return 
 withSuccess(

change the handle for lock release