onCommand
fun onCommand(command: CommandMessage<CreateOrder>, @Name(value = "createOrderSpec") specification: CreateOrderSpec, commandResultAccessor: CommandResultAccessor): Mono<OrderCreated>
Creates an order from its first command while the aggregate is uninitialized. The conventional onCommand name makes the command-handler annotation optional.
Kotlin coroutine style
suspend fun onCommand(
command: CommandMessage<CreateOrder>,
@Name("createOrderSpec") specification: CreateOrderSpec,
commandResultAccessor: CommandResultAccessor
): OrderCreated {
val createOrder = command.body
require(createOrder.items.isNotEmpty()) {
"items can not be empty."
}
createOrder.items.asFlow().collect {
specification.require(it).awaitSingle()
}
val orderCreated = OrderCreated(
orderId = command.aggregateId.id,
items = createOrder.items.map {
OrderItem(
id = GlobalIdGenerator.generateAsString(),
productId = it.productId,
price = it.price,
quantity = it.quantity,
)
},
address = createOrder.address,
fromCart = createOrder.fromCart,
)
commandResultAccessor.setCommandResult(
OrderState::totalAmount.name,
orderCreated.items.sumOf { it.totalPrice }
)
return orderCreated
}Content copied to clipboard
Parameters
command
The create-order command message.
specification
The creation rules injected by the IoC container.
commandResultAccessor
Records the total amount in the command result.
Applies a payment-service event after it has been adapted to PayOrder.