Created
January 19, 2020 04:18
-
-
Save justinhj/5eaaa6fbac187eb47fe51b1ac4afb5b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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