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 140588d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 38 deletions.
46 changes: 31 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ gleam add eventsourcing
import eventsourcing
import eventsourcing/memory_store
import gleam/result
pub const bank_account_event_type = "BankAccountEvent"
pub const bank_account_type = "BankAccount"
pub type BankAccount {
BankAccount(opened: Bool, balance: Float)
BankAccount(balance: Float)
UnopenedBankAccount
}
pub type BankAccountCommand {
Expand All @@ -26,40 +32,50 @@ pub type BankAccountEvent {
CustomerWithdrewCash(amount: Float, balance: Float)
}
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
}
}
pub fn main() {
let mem_store =
memory_store.new(BankAccount(opened: False, balance: 0.0), handle, apply)
memory_store.new(UnopenedBankAccount, handle, apply)
let query = fn(
aggregate_id: String,
events: List(eventsourcing.EventEnvelop(BankAccountEvent)),
Expand Down
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))
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "eventsourcing"
version = "3.0.0"
version = "3.0.1"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
Expand Down
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 140588d

Please sign in to comment.