forked from idugalic/digital-restaurant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrderHandler.kt
212 lines (185 loc) · 9.79 KB
/
OrderHandler.kt
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
210
211
212
package com.drestaurant.query.handler
import com.drestaurant.order.domain.api.*
import com.drestaurant.order.domain.api.model.OrderState
import com.drestaurant.query.FindAllOrdersQuery
import com.drestaurant.query.FindOrderQuery
import com.drestaurant.query.model.OrderEntity
import com.drestaurant.query.model.OrderItemEmbedable
import com.drestaurant.query.repository.CourierRepository
import com.drestaurant.query.repository.CustomerRepository
import com.drestaurant.query.repository.OrderRepository
import com.drestaurant.query.repository.RestaurantRepository
import org.axonframework.config.ProcessingGroup
import org.axonframework.eventhandling.AllowReplay
import org.axonframework.eventhandling.EventHandler
import org.axonframework.eventhandling.ResetHandler
import org.axonframework.eventhandling.SequenceNumber
import org.axonframework.queryhandling.QueryHandler
import org.axonframework.queryhandling.QueryUpdateEmitter
import org.springframework.stereotype.Component
import java.util.*
@Component
@ProcessingGroup("order")
internal class OrderHandler(private val orderRepository: OrderRepository, private val customerRepository: CustomerRepository, private val restaurantRepository: RestaurantRepository, private val courierRepository: CourierRepository, private val queryUpdateEmitter: QueryUpdateEmitter) {
@EventHandler
/* It is possible to allow or prevent some handlers from being replayed/reset */
@AllowReplay(true)
fun handle(event: OrderCreationInitiatedEvent, @SequenceNumber aggregateVersion: Long) {
val orderItems = ArrayList<OrderItemEmbedable>()
for (item in event.orderDetails.lineItems) {
val orderItem = OrderItemEmbedable(item.menuItemId, item.name, item.price.amount, item.quantity)
orderItems.add(orderItem)
}
val record = OrderEntity(event.aggregateIdentifier.identifier, aggregateVersion, orderItems, null, null, null, OrderState.CREATE_PENDING)
orderRepository.save(record)
/* sending it to subscription queries of type FindOrderQuery, but only if the order id matches. */
queryUpdateEmitter.emit(
FindOrderQuery::class.java,
{ query -> query.orderId == event.aggregateIdentifier },
record
)
/* sending it to subscription queries of type FindAllOrders. */
queryUpdateEmitter.emit(
FindAllOrdersQuery::class.java,
{ true },
record
)
}
@EventHandler
/* It is possible to allow or prevent some handlers from being replayed/reset */
@AllowReplay(true)
fun handle(event: OrderVerifiedByCustomerEvent, @SequenceNumber aggregateVersion: Long) {
val orderEntity = orderRepository.findById(event.aggregateIdentifier.identifier).orElseThrow { UnsupportedOperationException("Order with id '" + event.aggregateIdentifier + "' not found") }
val customerEntity = customerRepository.findById(event.customerId.identifier).orElseThrow { UnsupportedOperationException("Customer with id '" + event.customerId + "' not found") }
orderEntity.customer = customerEntity
orderEntity.state = OrderState.VERIFIED_BY_CUSTOMER
orderEntity.aggregateVersion = aggregateVersion
orderRepository.save(orderEntity)
/* sending it to subscription queries of type FindOrderQuery, but only if the order id matches. */
queryUpdateEmitter.emit(
FindOrderQuery::class.java,
{ query -> query.orderId == event.aggregateIdentifier },
orderEntity
)
/* sending it to subscription queries of type FindAllOrders. */
queryUpdateEmitter.emit(
FindAllOrdersQuery::class.java,
{ true },
orderEntity
)
}
@EventHandler
/* It is possible to allow or prevent some handlers from being replayed/reset */
@AllowReplay(true)
fun handle(event: OrderVerifiedByRestaurantEvent, @SequenceNumber aggregateVersion: Long) {
val orderEntity = orderRepository.findById(event.aggregateIdentifier.identifier).orElseThrow { UnsupportedOperationException("Order with id '" + event.aggregateIdentifier + "' not found") }
val restaurantEntity = restaurantRepository.findById(event.restaurantId.identifier).orElseThrow { UnsupportedOperationException("Restaurant with id '" + event.restaurantId + "' not found") }
orderEntity.aggregateVersion = aggregateVersion
orderEntity.restaurant = restaurantEntity
orderEntity.state = OrderState.VERIFIED_BY_RESTAURANT
orderRepository.save(orderEntity)
/* sending it to subscription queries of type FindOrderQuery, but only if the order id matches. */
queryUpdateEmitter.emit(
FindOrderQuery::class.java,
{ query -> query.orderId == event.aggregateIdentifier },
orderEntity
)
/* sending it to subscription queries of type FindAllOrders. */
queryUpdateEmitter.emit(
FindAllOrdersQuery::class.java,
{ true },
orderEntity
)
}
@EventHandler
/* It is possible to allow or prevent some handlers from being replayed/reset */
@AllowReplay(true)
fun handle(event: OrderPreparedEvent, @SequenceNumber aggregateVersion: Long) {
val orderEntity = orderRepository.findById(event.aggregateIdentifier.identifier).orElseThrow { UnsupportedOperationException("Order with id '" + event.aggregateIdentifier + "' not found") }
orderEntity.aggregateVersion = aggregateVersion
orderEntity.state = OrderState.PREPARED
orderRepository.save(orderEntity)
/* sending it to subscription queries of type FindOrderQuery, but only if the order id matches. */
queryUpdateEmitter.emit(
FindOrderQuery::class.java,
{ query -> query.orderId == event.aggregateIdentifier },
orderEntity
)
/* sending it to subscription queries of type FindAllOrders. */
queryUpdateEmitter.emit(
FindAllOrdersQuery::class.java,
{ true },
orderEntity
)
}
@EventHandler
/* It is possible to allow or prevent some handlers from being replayed/reset */
@AllowReplay(true)
fun handle(event: OrderReadyForDeliveryEvent, @SequenceNumber aggregateVersion: Long) {
val orderEntity = orderRepository.findById(event.aggregateIdentifier.identifier).orElseThrow { UnsupportedOperationException("Order with id '" + event.aggregateIdentifier + "' not found") }
orderEntity.aggregateVersion = aggregateVersion
orderEntity.state = OrderState.READY_FOR_DELIVERY
orderRepository.save(orderEntity)
/* sending it to subscription queries of type FindOrderQuery, but only if the order id matches. */
queryUpdateEmitter.emit(
FindOrderQuery::class.java,
{ query -> query.orderId == event.aggregateIdentifier },
orderEntity
)
/* sending it to subscription queries of type FindAllOrders. */
queryUpdateEmitter.emit(
FindAllOrdersQuery::class.java,
{ true },
orderEntity
)
}
@EventHandler
/* It is possible to allow or prevent some handlers from being replayed/reset */
@AllowReplay(true)
fun handle(event: OrderDeliveredEvent, @SequenceNumber aggregateVersion: Long) {
val orderEntity = orderRepository.findById(event.aggregateIdentifier.identifier).orElseThrow { UnsupportedOperationException("Order with id '" + event.aggregateIdentifier + "' not found") }
orderEntity.aggregateVersion = aggregateVersion
orderEntity.state = OrderState.DELIVERED
orderRepository.save(orderEntity)
/* sending it to subscription queries of type FindOrderQuery, but only if the order id matches. */
queryUpdateEmitter.emit(
FindOrderQuery::class.java,
{ query -> query.orderId == event.aggregateIdentifier },
orderEntity
)
/* sending it to subscription queries of type FindAllOrders. */
queryUpdateEmitter.emit(
FindAllOrdersQuery::class.java,
{ true },
orderEntity
)
}
@EventHandler
/* It is possible to allow or prevent some handlers from being replayed/reset */
@AllowReplay(true)
fun handle(event: OrderRejectedEvent, @SequenceNumber aggregateVersion: Long) {
val orderEntity = orderRepository.findById(event.aggregateIdentifier.identifier).orElseThrow { UnsupportedOperationException("Order with id '" + event.aggregateIdentifier + "' not found") }
orderEntity.aggregateVersion = aggregateVersion
orderEntity.state = OrderState.REJECTED
orderRepository.save(orderEntity)
/* sending it to subscription queries of type FindOrderQuery, but only if the order id matches. */
queryUpdateEmitter.emit(
FindOrderQuery::class.java,
{ query -> query.orderId == event.aggregateIdentifier },
orderEntity
)
/* sending it to subscription queries of type FindAllOrders. */
queryUpdateEmitter.emit(
FindAllOrdersQuery::class.java,
{ true },
orderEntity
)
}
/* Will be called before replay/reset starts. Do pre-reset logic, like clearing out the Projection table */
@ResetHandler
fun onReset() = orderRepository.deleteAll()
@QueryHandler
fun handle(query: FindOrderQuery): OrderEntity = orderRepository.findById(query.orderId.identifier).orElseThrow { UnsupportedOperationException("Order with id '" + query.orderId + "' not found") }
@QueryHandler
fun handle(query: FindAllOrdersQuery): MutableIterable<OrderEntity> = orderRepository.findAll()
}