Slide 57
Slide 57 text
class OrderEntity extends PersistentEntity {
override type Command = POrderCommand
override type Event = POrderEvent
override type State = POrder
override def initialState = POrder(this.entityId, Seq.empty, None, None, false)
override def behavior: Behavior = Actions()
.onCommand[POrderCommand.CreateOrder, Done] {
case (POrderCommand.CreateOrder(orderLines, shippingInfo), ctx, state) => {
ctx.thenPersist(POrderEvent.OrderCreated(orderLines, shippingInfo)) {
evt => ctx.reply(Done)
}
}
}.onCommand[POrderCommand.AddPayment, Done] {
case (POrderCommand.AddPayment(reference), ctx, state) => {
ctx.thenPersistAll(POrderEvent.OrderPaid(reference), POrderEvent.OrderConfirmed) {
() => ctx.reply(Done)
}
}
}.onReadOnlyCommand[GetOrder.type, POrder] {
case (GetOrder, ctx, state) => ctx.reply(state)
}.onEvent {
case (POrderEvent.OrderCreated(lines, shippingInfo), state) =>
state.copy(orderLines = lines, shippingInfo = Some(shippingInfo))
case (POrderEvent.OrderPaid(reference), state) => state.copy(paymentReference = Some(reference))
case (POrderEvent.OrderConfirmed, state) => state.copy(confirmed = true)
}
}