Skip to content

Commit

Permalink
better example
Browse files Browse the repository at this point in the history
  • Loading branch information
renatillas committed Aug 8, 2024
1 parent daa7ab5 commit f1f11e5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion birdie_snapshots/load_aggregate_entity.accepted
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ title: load aggregate entity
file: ./test/eventsourcing_test.gleam
test_name: load_aggregate_entity_test
---
Ok(BankAccount(True, 4.01))
Ok(BankAccount(4.01))
14 changes: 7 additions & 7 deletions test/eventsourcing_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn main() {
pub fn execute_test() {
let mem_store =
memory_store.new(
example_bank_account.BankAccount(opened: False, balance: 0.0),
example_bank_account.UnopenedBankAccount,
example_bank_account.handle,
example_bank_account.apply,
)
Expand All @@ -30,7 +30,7 @@ pub fn execute_test() {
pub fn query_test() {
let mem_store =
memory_store.new(
example_bank_account.BankAccount(opened: False, balance: 0.0),
example_bank_account.UnopenedBankAccount,
example_bank_account.handle,
example_bank_account.apply,
)
Expand All @@ -57,7 +57,7 @@ pub fn query_test() {
pub fn load_events_test() {
let mem_store =
memory_store.new(
example_bank_account.BankAccount(opened: False, balance: 0.0),
example_bank_account.UnopenedBankAccount,
example_bank_account.handle,
example_bank_account.apply,
)
Expand Down Expand Up @@ -97,7 +97,7 @@ pub fn load_events_test() {
pub fn load_events_with_metadata_test() {
let mem_store =
memory_store.new(
example_bank_account.BankAccount(opened: False, balance: 0.0),
example_bank_account.UnopenedBankAccount,
example_bank_account.handle,
example_bank_account.apply,
)
Expand All @@ -122,7 +122,7 @@ pub fn load_events_with_metadata_test() {
pub fn load_events_emtpy_aggregate_test() {
let mem_store =
memory_store.new(
example_bank_account.BankAccount(opened: False, balance: 0.0),
example_bank_account.UnopenedBankAccount,
example_bank_account.handle,
example_bank_account.apply,
)
Expand All @@ -137,7 +137,7 @@ pub fn load_events_emtpy_aggregate_test() {
pub fn load_aggregate_entity_test() {
let mem_store =
memory_store.new(
example_bank_account.BankAccount(opened: False, balance: 0.0),
example_bank_account.UnopenedBankAccount,
example_bank_account.handle,
example_bank_account.apply,
)
Expand Down Expand Up @@ -177,7 +177,7 @@ pub fn load_aggregate_entity_test() {
pub fn load_emtpy_aggregate_entity_test() {
let mem_store =
memory_store.new(
example_bank_account.BankAccount(opened: False, balance: 0.0),
example_bank_account.UnopenedBankAccount,
example_bank_account.handle,
example_bank_account.apply,
)
Expand Down
38 changes: 24 additions & 14 deletions test/example_bank_account.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import gleam/json
import gleam/result

pub type BankAccount {
BankAccount(opened: Bool, balance: Float)
BankAccount(balance: Float)
UnopenedBankAccount
}

pub const bank_account_type = "BankAccount"
Expand All @@ -23,34 +24,43 @@ pub type BankAccountEvent {

pub const bank_account_event_type = "BankAccountEvent"

pub type BankAccountError {
CantDepositNegativeAmount
CantOperateOnUnopenedAccount
CantWithdrawMoreThanCurrentBalance
}

pub fn handle(
bank_account: BankAccount,
command: BankAccountCommand,
) -> Result(List(BankAccountEvent), Nil) {
case command {
OpenAccount(account_id) -> Ok([AccountOpened(account_id)])
DepositMoney(amount) -> {
let balance = bank_account.balance +. amount
) -> Result(List(BankAccountEvent), BankAccountError) {
case bank_account, command {
UnopenedBankAccount, OpenAccount(account_id) ->
Ok([AccountOpened(account_id)])
BankAccount(balance), DepositMoney(amount) -> {
let balance = balance +. amount
case amount >. 0.0 {
True -> Ok([CustomerDepositedCash(amount:, balance:)])
False -> Error(Nil)
False -> Error(CantDepositNegativeAmount)
}
}
WithDrawMoney(amount) -> {
let balance = bank_account.balance -. amount
BankAccount(balance), WithDrawMoney(amount) -> {
let balance = balance -. amount
case amount >. 0.0 && balance >. 0.0 {
True -> Ok([CustomerWithdrewCash(amount:, balance:)])
False -> Error(Nil)
False -> Error(CantWithdrawMoreThanCurrentBalance)
}
}
_, _ -> Error(CantOperateOnUnopenedAccount)
}
}

pub fn apply(bank_account: BankAccount, event: BankAccountEvent) {
case event {
AccountOpened(_) -> BankAccount(..bank_account, opened: True)
CustomerDepositedCash(_, balance) -> BankAccount(..bank_account, balance:)
CustomerWithdrewCash(_, balance) -> BankAccount(..bank_account, balance:)
case bank_account, event {
UnopenedBankAccount, AccountOpened(_) -> BankAccount(0.0)
BankAccount(_), CustomerDepositedCash(_, balance) -> BankAccount(balance:)
BankAccount(_), CustomerWithdrewCash(_, balance) -> BankAccount(balance:)
_, _ -> panic
}
}

Expand Down

0 comments on commit f1f11e5

Please sign in to comment.