Skip to content

Instantly share code, notes, and snippets.

@justinhj
Created January 19, 2020 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinhj/5eaaa6fbac187eb47fe51b1ac4afb5b8 to your computer and use it in GitHub Desktop.
Save justinhj/5eaaa6fbac187eb47fe51b1ac4afb5b8 to your computer and use it in GitHub Desktop.
case class AccountEntity(id: Int, state: AccountState)
extends PersistentEntity[Int, AccountEntity] {
override type Command = BankAccountCommand
override type Event = BankAccountEvent
override type State = AccountState
// Applies the command to the current state, returning a list of events
def processCommand(command: Command) : List[Event] = {
command match {
case DepositCmd(time, amount) =>
List(DepositEvt(time, amount))
case PurchaseCmd(time, amount) =>
if(amount <= state.balance)
List(PurchaseEvt(time, amount))
else
List.empty
case AssignAccountHolderCmd(time, accountHolder) =>
List(AssignAccountHolderEvt(time, accountHolder))
}
}
// Processing an event applies changes to the current state
// Since our entity is immutable we return a new one
def processEvent(event: Event) : AccountEntity = {
event match {
case DepositEvt(time, amount) =>
AccountEntity(id, AccountState(state.balance + amount, state.accountHolder))
case PurchaseEvt(time, amount) =>
AccountEntity(id, AccountState(state.balance - amount, state.accountHolder))
case AssignAccountHolderEvt(time, accountHolder) =>
AccountEntity(id, AccountState(state.balance, accountHolder.some))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment