-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
OrderCommandHandlers.java
105 lines (83 loc) · 3.9 KB
/
OrderCommandHandlers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package net.chrisrichardson.ftgo.orderservice.service;
import io.eventuate.tram.commands.consumer.CommandHandlerReplyBuilder;
import net.chrisrichardson.ftgo.orderservice.domain.OrderRepository;
import net.chrisrichardson.ftgo.orderservice.domain.OrderRevision;
import net.chrisrichardson.ftgo.orderservice.domain.OrderService;
import net.chrisrichardson.ftgo.orderservice.domain.RevisedOrder;
import net.chrisrichardson.ftgo.orderservice.sagaparticipants.*;
import io.eventuate.tram.commands.consumer.CommandHandlers;
import io.eventuate.tram.commands.consumer.CommandMessage;
import io.eventuate.tram.events.publisher.DomainEventPublisher;
import io.eventuate.tram.messaging.common.Message;
import io.eventuate.tram.sagas.participant.SagaCommandHandlersBuilder;
import net.chrisrichardson.ftgo.common.UnsupportedStateTransitionException;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Optional;
import static io.eventuate.tram.commands.consumer.CommandHandlerReplyBuilder.withFailure;
import static io.eventuate.tram.commands.consumer.CommandHandlerReplyBuilder.withSuccess;
public class OrderCommandHandlers {
@Autowired
private OrderService orderService;
public CommandHandlers commandHandlers() {
return SagaCommandHandlersBuilder
.fromChannel("orderService")
.onMessage(ApproveOrderCommand.class, this::approveOrder)
.onMessage(RejectOrderCommand.class, this::rejectOrder)
.onMessage(BeginCancelCommand.class, this::beginCancel)
.onMessage(UndoBeginCancelCommand.class, this::undoCancel)
.onMessage(ConfirmCancelOrderCommand.class, this::confirmCancel)
.onMessage(BeginReviseOrderCommand.class, this::beginReviseOrder)
.onMessage(UndoBeginReviseOrderCommand.class, this::undoPendingRevision)
.onMessage(ConfirmReviseOrderCommand.class, this::confirmRevision)
.build();
}
public Message approveOrder(CommandMessage<ApproveOrderCommand> cm) {
long orderId = cm.getCommand().getOrderId();
orderService.approveOrder(orderId);
return withSuccess();
}
public Message rejectOrder(CommandMessage<RejectOrderCommand> cm) {
long orderId = cm.getCommand().getOrderId();
orderService.rejectOrder(orderId);
return withSuccess();
}
public Message beginCancel(CommandMessage<BeginCancelCommand> cm) {
long orderId = cm.getCommand().getOrderId();
try {
orderService.beginCancel(orderId);
return withSuccess();
} catch (UnsupportedStateTransitionException e) {
return withFailure();
}
}
public Message undoCancel(CommandMessage<UndoBeginCancelCommand> cm) {
long orderId = cm.getCommand().getOrderId();
orderService.undoCancel(orderId);
return withSuccess();
}
public Message confirmCancel(CommandMessage<ConfirmCancelOrderCommand> cm) {
long orderId = cm.getCommand().getOrderId();
orderService.confirmCancelled(orderId);
return withSuccess();
}
public Message beginReviseOrder(CommandMessage<BeginReviseOrderCommand> cm) {
long orderId = cm.getCommand().getOrderId();
OrderRevision revision = cm.getCommand().getRevision();
try {
return orderService.beginReviseOrder(orderId, revision).map(result -> withSuccess(new BeginReviseOrderReply(result.getChange().getNewOrderTotal()))).orElseGet(CommandHandlerReplyBuilder::withFailure);
} catch (UnsupportedStateTransitionException e) {
return withFailure();
}
}
public Message undoPendingRevision(CommandMessage <UndoBeginReviseOrderCommand> cm) {
long orderId = cm.getCommand().getOrderId();
orderService.undoPendingRevision(orderId);
return withSuccess();
}
public Message confirmRevision(CommandMessage<ConfirmReviseOrderCommand> cm) {
long orderId = cm.getCommand().getOrderId();
OrderRevision revision = cm.getCommand().getRevision();
orderService.confirmRevision(orderId, revision);
return withSuccess();
}
}