-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Ticket.java
209 lines (176 loc) · 5.98 KB
/
Ticket.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package net.chrisrichardson.ftgo.kitchenservice.domain;
import io.eventuate.tram.events.aggregates.ResultWithDomainEvents;
import net.chrisrichardson.ftgo.common.NotYetImplementedException;
import net.chrisrichardson.ftgo.common.RevisedOrderLineItem;
import net.chrisrichardson.ftgo.common.UnsupportedStateTransitionException;
import net.chrisrichardson.ftgo.kitchenservice.api.TicketDetails;
import net.chrisrichardson.ftgo.kitchenservice.api.TicketLineItem;
import net.chrisrichardson.ftgo.kitchenservice.api.events.TicketAcceptedEvent;
import net.chrisrichardson.ftgo.kitchenservice.api.events.TicketCancelled;
import net.chrisrichardson.ftgo.kitchenservice.api.events.TicketDomainEvent;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
@Entity
@Table(name = "tickets")
@Access(AccessType.FIELD)
public class Ticket {
@Id
private Long id;
@Enumerated(EnumType.STRING)
private TicketState state;
private TicketState previousState;
private Long restaurantId;
@ElementCollection
@CollectionTable(name = "ticket_line_items")
private List<TicketLineItem> lineItems;
private LocalDateTime readyBy;
private LocalDateTime acceptTime;
private LocalDateTime preparingTime;
private LocalDateTime pickedUpTime;
private LocalDateTime readyForPickupTime;
public static ResultWithDomainEvents<Ticket, TicketDomainEvent> create(long restaurantId, Long id, TicketDetails details) {
return new ResultWithDomainEvents<>(new Ticket(restaurantId, id, details));
}
private Ticket() {
}
public Ticket(long restaurantId, Long id, TicketDetails details) {
this.restaurantId = restaurantId;
this.id = id;
this.state = TicketState.CREATE_PENDING;
this.lineItems = details.getLineItems();
}
public List<TicketDomainEvent> confirmCreate() {
switch (state) {
case CREATE_PENDING:
state = TicketState.AWAITING_ACCEPTANCE;
return singletonList(new TicketCreatedEvent(id, new TicketDetails()));
default:
throw new UnsupportedStateTransitionException(state);
}
}
public List<TicketDomainEvent> cancelCreate() {
throw new NotYetImplementedException();
}
public List<TicketDomainEvent> accept(LocalDateTime readyBy) {
switch (state) {
case AWAITING_ACCEPTANCE:
// Verify that readyBy is in the futurestate = TicketState.ACCEPTED;
this.acceptTime = LocalDateTime.now();
if (!acceptTime.isBefore(readyBy))
throw new IllegalArgumentException(String.format("readyBy %s is not after now %s", readyBy, acceptTime));
this.readyBy = readyBy;
return singletonList(new TicketAcceptedEvent(readyBy));
default:
throw new UnsupportedStateTransitionException(state);
}
}
// TODO reject()
// TODO cancel()
public List<TicketDomainEvent> preparing() {
switch (state) {
case ACCEPTED:
this.state = TicketState.PREPARING;
this.preparingTime = LocalDateTime.now();
return singletonList(new TicketPreparationStartedEvent());
default:
throw new UnsupportedStateTransitionException(state);
}
}
public List<TicketDomainEvent> readyForPickup() {
switch (state) {
case PREPARING:
this.state = TicketState.READY_FOR_PICKUP;
this.readyForPickupTime = LocalDateTime.now();
return singletonList(new TicketPreparationCompletedEvent());
default:
throw new UnsupportedStateTransitionException(state);
}
}
public List<TicketDomainEvent> pickedUp() {
switch (state) {
case READY_FOR_PICKUP:
this.state = TicketState.PICKED_UP;
this.pickedUpTime = LocalDateTime.now();
return singletonList(new TicketPickedUpEvent());
default:
throw new UnsupportedStateTransitionException(state);
}
}
public void changeLineItemQuantity() {
switch (state) {
case AWAITING_ACCEPTANCE:
// TODO
break;
case PREPARING:
// TODO - too late
break;
default:
throw new UnsupportedStateTransitionException(state);
}
}
public List<TicketDomainEvent> cancel() {
switch (state) {
case AWAITING_ACCEPTANCE:
case ACCEPTED:
this.previousState = state;
this.state = TicketState.CANCEL_PENDING;
return emptyList();
default:
throw new UnsupportedStateTransitionException(state);
}
}
public Long getId() {
return id;
}
public List<TicketDomainEvent> confirmCancel() {
switch (state) {
case CANCEL_PENDING:
this.state = TicketState.CANCELLED;
return singletonList(new TicketCancelled());
default:
throw new UnsupportedStateTransitionException(state);
}
}
public List<TicketDomainEvent> undoCancel() {
switch (state) {
case CANCEL_PENDING:
this.state = this.previousState;
return emptyList();
default:
throw new UnsupportedStateTransitionException(state);
}
}
public List<TicketDomainEvent> beginReviseOrder(List<RevisedOrderLineItem> revisedOrderLineItems) {
switch (state) {
case AWAITING_ACCEPTANCE:
case ACCEPTED:
this.previousState = state;
this.state = TicketState.REVISION_PENDING;
return emptyList();
default:
throw new UnsupportedStateTransitionException(state);
}
}
public List<TicketDomainEvent> undoBeginReviseOrder() {
switch (state) {
case REVISION_PENDING:
this.state = this.previousState;
return emptyList();
default:
throw new UnsupportedStateTransitionException(state);
}
}
public List<TicketDomainEvent> confirmReviseTicket(List<RevisedOrderLineItem> revisedOrderLineItems) {
switch (state) {
case REVISION_PENDING:
this.state = this.previousState;
return singletonList(new TicketRevised());
default:
throw new UnsupportedStateTransitionException(state);
}
}
}