-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathOrderHandler.kt
106 lines (94 loc) · 4.74 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
package com.drestaurant.query.handler
import com.drestaurant.order.domain.api.*
import com.drestaurant.order.domain.api.model.OrderState
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.springframework.messaging.simp.SimpMessageSendingOperations
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 messagingTemplate: SimpMessageSendingOperations) {
@EventHandler
@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)
}
orderRepository.save(OrderEntity(event.aggregateIdentifier.identifier, aggregateVersion, orderItems, null, null, null, OrderState.CREATE_PENDING))
broadcastUpdates()
}
@EventHandler
@AllowReplay(true)
fun handle(event: OrderVerifiedByCustomerEvent, @SequenceNumber aggregateVersion: Long) {
val orderEntity = orderRepository.findById(event.aggregateIdentifier.identifier).get()
val customerEntity = customerRepository.findById(event.customerId.identifier).get()
orderEntity.customer = customerEntity
orderEntity.state = OrderState.VERIFIED_BY_CUSTOMER
orderEntity.aggregateVersion = aggregateVersion
orderRepository.save(orderEntity)
broadcastUpdates()
}
@EventHandler
@AllowReplay(true)
fun handle(event: OrderVerifiedByRestaurantEvent, @SequenceNumber aggregateVersion: Long) {
val orderEntity = orderRepository.findById(event.aggregateIdentifier.identifier).get()
val restaurantEntity = restaurantRepository.findById(event.restaurantId.identifier).get()
orderEntity.aggregateVersion = aggregateVersion
orderEntity.restaurant = restaurantEntity
orderEntity.state = OrderState.VERIFIED_BY_RESTAURANT
orderRepository.save(orderEntity)
broadcastUpdates()
}
@EventHandler
@AllowReplay(true)
fun handle(event: OrderPreparedEvent, @SequenceNumber aggregateVersion: Long) {
val orderEntity = orderRepository.findById(event.aggregateIdentifier.identifier).get()
orderEntity.aggregateVersion = aggregateVersion
orderEntity.state = OrderState.PREPARED
orderRepository.save(orderEntity)
broadcastUpdates()
}
@EventHandler
@AllowReplay(true)
fun handle(event: OrderReadyForDeliveryEvent, @SequenceNumber aggregateVersion: Long) {
val orderEntity = orderRepository.findById(event.aggregateIdentifier.identifier).get()
orderEntity.aggregateVersion = aggregateVersion
orderEntity.state = OrderState.READY_FOR_DELIVERY
orderRepository.save(orderEntity)
broadcastUpdates()
}
@EventHandler
@AllowReplay(true)
fun handle(event: OrderDeliveredEvent, @SequenceNumber aggregateVersion: Long) {
val orderEntity = orderRepository.findById(event.aggregateIdentifier.identifier).get()
orderEntity.aggregateVersion = aggregateVersion
orderEntity.state = OrderState.DELIVERED
orderRepository.save(orderEntity)
broadcastUpdates()
}
@EventHandler
@AllowReplay(true)
fun handle(event: OrderRejectedEvent, @SequenceNumber aggregateVersion: Long) {
val orderEntity = orderRepository.findById(event.aggregateIdentifier.identifier).get()
orderEntity.aggregateVersion = aggregateVersion
orderEntity.state = OrderState.REJECTED
orderRepository.save(orderEntity)
broadcastUpdates()
}
/* Will be called before replay/reset starts. Do pre-reset logic, like clearing out the Projection table */
@ResetHandler
fun onReset() = orderRepository.deleteAll()
private fun broadcastUpdates() = messagingTemplate.convertAndSend("/topic/orders.updates", orderRepository.findAll())
}