From f1f11e56a06ff57759fd7b5f36b0f2ef117f8951 Mon Sep 17 00:00:00 2001 From: Renata Amutio Herrero Date: Thu, 8 Aug 2024 07:44:43 +0200 Subject: [PATCH] better example --- .../load_aggregate_entity.accepted | 2 +- test/eventsourcing_test.gleam | 14 +++---- test/example_bank_account.gleam | 38 ++++++++++++------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/birdie_snapshots/load_aggregate_entity.accepted b/birdie_snapshots/load_aggregate_entity.accepted index ed3d299..faf8d38 100644 --- a/birdie_snapshots/load_aggregate_entity.accepted +++ b/birdie_snapshots/load_aggregate_entity.accepted @@ -4,4 +4,4 @@ title: load aggregate entity file: ./test/eventsourcing_test.gleam test_name: load_aggregate_entity_test --- -Ok(BankAccount(True, 4.01)) \ No newline at end of file +Ok(BankAccount(4.01)) \ No newline at end of file diff --git a/test/eventsourcing_test.gleam b/test/eventsourcing_test.gleam index b796a27..f03d009 100644 --- a/test/eventsourcing_test.gleam +++ b/test/eventsourcing_test.gleam @@ -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, ) @@ -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, ) @@ -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, ) @@ -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, ) @@ -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, ) @@ -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, ) @@ -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, ) diff --git a/test/example_bank_account.gleam b/test/example_bank_account.gleam index f5669c1..ca6103c 100644 --- a/test/example_bank_account.gleam +++ b/test/example_bank_account.gleam @@ -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" @@ -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 } }