-
Notifications
You must be signed in to change notification settings - Fork 87
/
CourierOrder.kt
99 lines (82 loc) · 3.65 KB
/
CourierOrder.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
package com.drestaurant.courier.domain
import com.drestaurant.courier.domain.api.*
import com.drestaurant.courier.domain.api.model.CourierId
import com.drestaurant.courier.domain.api.model.CourierOrderId
import com.drestaurant.courier.domain.api.model.CourierOrderState
import org.apache.commons.lang.builder.EqualsBuilder
import org.apache.commons.lang.builder.HashCodeBuilder
import org.apache.commons.lang.builder.ToStringBuilder
import org.axonframework.commandhandling.CommandHandler
import org.axonframework.eventsourcing.EventSourcingHandler
import org.axonframework.modelling.command.AggregateIdentifier
import org.axonframework.modelling.command.AggregateLifecycle.apply
import org.axonframework.spring.stereotype.Aggregate
@Aggregate(snapshotTriggerDefinition = "courierOrderSnapshotTriggerDefinition")
internal class CourierOrder {
@AggregateIdentifier
private lateinit var id: CourierOrderId
private lateinit var cuourierId: CourierId
private lateinit var state: CourierOrderState
constructor()
@CommandHandler
constructor(command: CreateCourierOrderCommand) {
apply(CourierOrderCreatedEvent(command.targetAggregateIdentifier, command.auditEntry))
}
@EventSourcingHandler
fun on(event: CourierOrderCreatedEvent) {
id = event.aggregateIdentifier
state = CourierOrderState.CREATED
}
@CommandHandler
fun assignToCourier(command: AssignCourierOrderToCourierCommand) {
if (CourierOrderState.CREATED == state) {
apply(CourierOrderAssigningInitiatedInternalEvent(command.courierId, command.targetAggregateIdentifier, command.auditEntry))
} else {
throw UnsupportedOperationException("The current state is not CREATED")
}
}
@EventSourcingHandler
fun on(event: CourierOrderAssigningInitiatedInternalEvent) {
state = CourierOrderState.ASSIGN_PENDING
}
@CommandHandler
fun markOrderAsAssigned(command: MarkCourierOrderAsAssignedInternalCommand) {
if (CourierOrderState.ASSIGN_PENDING == state) {
apply(CourierOrderAssignedEvent(command.targetAggregateIdentifier, command.courierId, command.auditEntry))
} else {
throw UnsupportedOperationException("The current state is not ASSIGN_PENDING")
}
}
@EventSourcingHandler
fun on(event: CourierOrderAssignedEvent) {
cuourierId = event.courierId
state = CourierOrderState.ASSIGNED
}
@CommandHandler
fun markOrderAsNotAssigned(command: MarkCourierOrderAsNotAssignedInternalCommand) {
if (CourierOrderState.ASSIGN_PENDING == state) {
apply(CourierOrderNotAssignedEvent(command.targetAggregateIdentifier, command.auditEntry))
} else {
throw UnsupportedOperationException("The current state is not ASSIGN_PENDING")
}
}
@EventSourcingHandler
fun on(event: CourierOrderNotAssignedEvent) {
state = CourierOrderState.CREATED
}
@CommandHandler
fun markOrderAsDelivered(command: MarkCourierOrderAsDeliveredCommand) {
if (CourierOrderState.ASSIGNED == state) {
apply(CourierOrderDeliveredEvent(command.targetAggregateIdentifier, command.auditEntry))
} else {
throw UnsupportedOperationException("The current state is not ASSIGNED")
}
}
@EventSourcingHandler
fun on(event: CourierOrderDeliveredEvent) {
state = CourierOrderState.DELIVERED
}
override fun toString(): String = ToStringBuilder.reflectionToString(this)
override fun equals(other: Any?): Boolean = EqualsBuilder.reflectionEquals(this, other)
override fun hashCode(): Int = HashCodeBuilder.reflectionHashCode(this)
}