Last active
December 31, 2018 14:36
-
-
Save shalvah/3ed485fe6ac122ebd0deb6a19beec6db 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
// without event sourcing | |
function transferMoneyBetweenAccounts(amount, fromAccount, toAccount) { | |
BankAccount.where({ id: fromAccount.id }) | |
.decrement({ amount }); | |
BankAccount.where({ id: toAccount.id }) | |
.increment({ amount }); | |
} | |
function makeOnlinePayment(account, amount) { | |
BankAccount.where({ id: account.id }) | |
.decrement({ amount }); | |
} | |
// with event sourcing | |
function transferMoneyBetweenAccounts(amount, fromAccount, toAccount) { | |
dispatchEvent(new TransferFrom(fromAccount, amount, toAccount)); | |
dispatchEvent(new TransferTo(toAccount, amount, fromAccount)); | |
} | |
function makeOnlinePayment(account, amount) { | |
dispatchEvent(new OnlinePaymentFrom(account, amount)); | |
} | |
class TransferFrom extends Event { | |
constructor(account, amount, toAccount) { | |
this.account = account; | |
this.amount = amount; | |
this.toAccount = toAccount; | |
} | |
handle() { | |
// store new OutwardTransfer event in database | |
OutwardTransfer.create({ from: this.account, to: this.toAccount, amount: this.amount, date: Date.now() }); | |
// also update the current state of the account | |
BankAccount.where({ id: this.account.id }) | |
.decrement({ amount: this.amount }); | |
} | |
} | |
class TransferTo extends Event { | |
constructor(account, amount, fromAccount) { | |
this.account = account; | |
this.amount = amount; | |
this.fromAccount = fromAccount; | |
} | |
handle() { | |
// store new InwardTransfer event in database | |
InwardTransfer.create({ from: this.fromAccount, to: this.account, amount: this.amount, date: Date.now() }); | |
// also update the current state of the account | |
BankAccount.where({ id: this.account.id }) | |
.increment({ amount: this.amount }); | |
} | |
} | |
class OnlinePaymentFrom extends Event { | |
constructor(account, amount) { | |
this.account = account; | |
this.amount = amount; | |
} | |
handle() { | |
// store new OnlinePayment event in database | |
OnlinePayment.create({ from: this.account, amount: this.amount, date: Date.now() }); | |
// also update the current state of the account | |
BankAccount.where({ id: this.account.id }) | |
.decrement({ amount: this.amount }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment