Skip to content

Instantly share code, notes, and snippets.

@monkey-codes
Created April 8, 2021 02:15
Show Gist options
  • Select an option

  • Save monkey-codes/35f44de8a77a35d9dffe11477434d880 to your computer and use it in GitHub Desktop.

Select an option

Save monkey-codes/35f44de8a77a35d9dffe11477434d880 to your computer and use it in GitHub Desktop.
Axon Aggregate
@Aggregate
final class Waiter() {
...
@CommandHandler
constructor(command: HireWaiterCommand) : this() {
AggregateLifecycle.apply(WaiterHiredEvent(id = command.id, name = command.name))
}
@CommandHandler
fun handle(command: TakeOrderCommand) {
if(command.items.any { this.outOfStockItems.contains(it.name) }){
throw ItemsOutOfStockException(this.outOfStockItems.toList())
}
AggregateLifecycle.apply(OrderTakenEvent(command.waiterId, command.orderId, items = command.items))
}
@EventSourcingHandler
fun on(event: WaiterHiredEvent) {
this.id = event.id
}
@EventSourcingHandler
fun on(event: OrderTakenEvent) {
this.orders.add( Order(event.orderId, event.items))
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment