forked from idugalic/digital-restaurant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CourierOrderHandler.kt
112 lines (97 loc) · 5.41 KB
/
CourierOrderHandler.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
package com.drestaurant.query.handler
import com.drestaurant.courier.domain.api.CourierOrderAssignedEvent
import com.drestaurant.courier.domain.api.CourierOrderCreatedEvent
import com.drestaurant.courier.domain.api.CourierOrderDeliveredEvent
import com.drestaurant.courier.domain.api.CourierOrderNotAssignedEvent
import com.drestaurant.courier.domain.api.model.CourierOrderState
import com.drestaurant.query.FindAllCourierOrdersQuery
import com.drestaurant.query.FindCourierOrderQuery
import com.drestaurant.query.model.CourierOrderEntity
import com.drestaurant.query.repository.CourierOrderRepository
import com.drestaurant.query.repository.CourierRepository
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
@Component
@ProcessingGroup("courierorder")
internal class CourierOrderHandler(private val repository: CourierOrderRepository, 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: CourierOrderCreatedEvent, @SequenceNumber aggregateVersion: Long) {
val record = CourierOrderEntity(event.aggregateIdentifier.identifier, aggregateVersion, null, CourierOrderState.CREATED)
repository.save(record)
}
@EventHandler
/* It is possible to allow or prevent some handlers from being replayed/reset */
@AllowReplay(true)
fun handle(event: CourierOrderAssignedEvent, @SequenceNumber aggregateVersion: Long) {
val courierEntity = courierRepository.findById(event.courierId.identifier).orElseThrow { UnsupportedOperationException("Courier with id '" + event.courierId + "' not found") }
val record = repository.findById(event.aggregateIdentifier.identifier).orElseThrow { UnsupportedOperationException("Courier order with id '" + event.aggregateIdentifier + "' not found") }
record.state = CourierOrderState.ASSIGNED
record.courier = courierEntity
repository.save(record)
/* sending it to subscription queries of type FindCourierOrderQuery, but only if the courier order id matches. */
queryUpdateEmitter.emit(
FindCourierOrderQuery::class.java,
{ query -> query.courierOrderId == event.aggregateIdentifier },
record
)
/* sending it to subscription queries of type FindAllCourierOrders. */
queryUpdateEmitter.emit(
FindAllCourierOrdersQuery::class.java,
{ true },
record
)
}
@EventHandler
/* It is possible to allow or prevent some handlers from being replayed/reset */
@AllowReplay(true)
fun handle(event: CourierOrderNotAssignedEvent, @SequenceNumber aggregateVersion: Long) {
val record = repository.findById(event.aggregateIdentifier.identifier).orElseThrow { UnsupportedOperationException("Courier order with id '" + event.aggregateIdentifier + "' not found") }
/* sending it to subscription queries of type FindCourierOrderQuery, but only if the courier order id matches. */
queryUpdateEmitter.emit(
FindCourierOrderQuery::class.java,
{ query -> query.courierOrderId == event.aggregateIdentifier },
record
)
/* sending it to subscription queries of type FindAllCourierOrders. */
queryUpdateEmitter.emit(
FindAllCourierOrdersQuery::class.java,
{ true },
record
)
}
@EventHandler
/* It is possible to allow or prevent some handlers from being replayed/reset */
@AllowReplay(true)
fun handle(event: CourierOrderDeliveredEvent, @SequenceNumber aggregateVersion: Long) {
val record = repository.findById(event.aggregateIdentifier.identifier).orElseThrow { UnsupportedOperationException("Courier order with id '" + event.aggregateIdentifier + "' not found") }
record.state = CourierOrderState.DELIVERED
repository.save(record)
/* sending it to subscription queries of type FindCourierOrderQuery, but only if the courier order id matches. */
queryUpdateEmitter.emit(
FindCourierOrderQuery::class.java,
{ query -> query.courierOrderId == event.aggregateIdentifier },
record
)
/* sending it to subscription queries of type FindAllCourierOrders. */
queryUpdateEmitter.emit(
FindAllCourierOrdersQuery::class.java,
{ true },
record
)
}
/* Will be called before replay/reset starts. Do pre-reset logic, like clearing out the Projection table */
@ResetHandler
fun onReset() = repository.deleteAll()
@QueryHandler
fun handle(query: FindCourierOrderQuery): CourierOrderEntity = repository.findById(query.courierOrderId.identifier).orElseThrow { UnsupportedOperationException("Courier order with id '" + query.courierOrderId + "' not found") }
@QueryHandler
fun handle(query: FindAllCourierOrdersQuery): MutableIterable<CourierOrderEntity> = repository.findAll()
}