-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
KitchenServiceCommandHandler.java
102 lines (81 loc) · 4.31 KB
/
KitchenServiceCommandHandler.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
package net.chrisrichardson.ftgo.kitchenservice.messagehandlers;
import io.eventuate.tram.commands.consumer.CommandHandlers;
import io.eventuate.tram.commands.consumer.CommandMessage;
import io.eventuate.tram.messaging.common.Message;
import io.eventuate.tram.sagas.participant.SagaCommandHandlersBuilder;
import net.chrisrichardson.ftgo.kitchenservice.api.*;
import net.chrisrichardson.ftgo.kitchenservice.domain.RestaurantDetailsVerificationException;
import net.chrisrichardson.ftgo.kitchenservice.domain.Ticket;
import net.chrisrichardson.ftgo.kitchenservice.domain.KitchenService;
import org.springframework.beans.factory.annotation.Autowired;
import static io.eventuate.tram.commands.consumer.CommandHandlerReplyBuilder.withFailure;
import static io.eventuate.tram.commands.consumer.CommandHandlerReplyBuilder.withSuccess;
import static io.eventuate.tram.sagas.participant.SagaReplyMessageBuilder.withLock;
public class KitchenServiceCommandHandler {
@Autowired
private KitchenService kitchenService;
public CommandHandlers commandHandlers() {
return SagaCommandHandlersBuilder
.fromChannel(KitchenServiceChannels.COMMAND_CHANNEL)
.onMessage(CreateTicket.class, this::createTicket)
.onMessage(ConfirmCreateTicket.class, this::confirmCreateTicket)
.onMessage(CancelCreateTicket.class, this::cancelCreateTicket)
.onMessage(BeginCancelTicketCommand.class, this::beginCancelTicket)
.onMessage(ConfirmCancelTicketCommand.class, this::confirmCancelTicket)
.onMessage(UndoBeginCancelTicketCommand.class, this::undoBeginCancelTicket)
.onMessage(BeginReviseTicketCommand.class, this::beginReviseTicket)
.onMessage(UndoBeginReviseTicketCommand.class, this::undoBeginReviseTicket)
.onMessage(ConfirmReviseTicketCommand.class, this::confirmReviseTicket)
.build();
}
private Message createTicket(CommandMessage<CreateTicket>
cm) {
CreateTicket command = cm.getCommand();
long restaurantId = command.getRestaurantId();
Long ticketId = command.getOrderId();
TicketDetails ticketDetails = command.getTicketDetails();
try {
Ticket ticket = kitchenService.createTicket(restaurantId, ticketId, ticketDetails);
CreateTicketReply reply = new CreateTicketReply(ticket.getId());
return withLock(Ticket.class, ticket.getId()).withSuccess(reply);
} catch (RestaurantDetailsVerificationException e) {
return withFailure();
}
}
private Message confirmCreateTicket
(CommandMessage<ConfirmCreateTicket> cm) {
Long ticketId = cm.getCommand().getTicketId();
kitchenService.confirmCreateTicket(ticketId);
return withSuccess();
}
private Message cancelCreateTicket
(CommandMessage<CancelCreateTicket> cm) {
Long ticketId = cm.getCommand().getTicketId();
kitchenService.cancelCreateTicket(ticketId);
return withSuccess();
}
private Message beginCancelTicket(CommandMessage<BeginCancelTicketCommand> cm) {
kitchenService.cancelTicket(cm.getCommand().getRestaurantId(), cm.getCommand().getOrderId());
return withSuccess();
}
private Message confirmCancelTicket(CommandMessage<ConfirmCancelTicketCommand> cm) {
kitchenService.confirmCancelTicket(cm.getCommand().getRestaurantId(), cm.getCommand().getOrderId());
return withSuccess();
}
private Message undoBeginCancelTicket(CommandMessage<UndoBeginCancelTicketCommand> cm) {
kitchenService.undoCancel(cm.getCommand().getRestaurantId(), cm.getCommand().getOrderId());
return withSuccess();
}
public Message beginReviseTicket(CommandMessage<BeginReviseTicketCommand> cm) {
kitchenService.beginReviseOrder(cm.getCommand().getRestaurantId(), cm.getCommand().getOrderId(), cm.getCommand().getRevisedOrderLineItems());
return withSuccess();
}
public Message undoBeginReviseTicket(CommandMessage<UndoBeginReviseTicketCommand> cm) {
kitchenService.undoBeginReviseOrder(cm.getCommand().getRestaurantId(), cm.getCommand().getOrderId());
return withSuccess();
}
public Message confirmReviseTicket(CommandMessage<ConfirmReviseTicketCommand> cm) {
kitchenService.confirmReviseTicket(cm.getCommand().getRestaurantId(), cm.getCommand().getOrderId(), cm.getCommand().getRevisedOrderLineItems());
return withSuccess();
}
}