-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6805309
commit b32ef1e
Showing
7 changed files
with
317 additions
and
173 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
version: 1.1.8 | ||
title: memory store load events | ||
file: ./test/eventsourcing_test.gleam | ||
test_name: memory_store_load_events_test | ||
--- | ||
[ | ||
MemoryStoreEventEnvelop( | ||
"92085b42-032c-4d7a-84de-a86d67123858", | ||
1, | ||
AccountOpened("92085b42-032c-4d7a-84de-a86d67123858"), | ||
), | ||
MemoryStoreEventEnvelop( | ||
"92085b42-032c-4d7a-84de-a86d67123858", | ||
2, | ||
CustomerDepositedCash(10.0, 10.0), | ||
), | ||
MemoryStoreEventEnvelop( | ||
"92085b42-032c-4d7a-84de-a86d67123858", | ||
3, | ||
CustomerWithdrewCash(5.99, 4.01), | ||
), | ||
] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
version: 1.1.8 | ||
title: sqlite store load events | ||
file: ./test/sqlite_store_test.gleam | ||
test_name: sqlite_store_load_events_test | ||
--- | ||
[ | ||
SerializedEventEnvelop( | ||
"92085b42-032c-4d7a-84de-a86d67123858", | ||
1, | ||
AccountOpened("92085b42-032c-4d7a-84de-a86d67123858"), | ||
"BankAccountEvent", | ||
"1.0", | ||
"BankAccount", | ||
), | ||
SerializedEventEnvelop( | ||
"92085b42-032c-4d7a-84de-a86d67123858", | ||
2, | ||
CustomerDepositedCash(10.0, 10.0), | ||
"BankAccountEvent", | ||
"1.0", | ||
"BankAccount", | ||
), | ||
SerializedEventEnvelop( | ||
"92085b42-032c-4d7a-84de-a86d67123858", | ||
3, | ||
CustomerWithdrewCash(5.99, 4.01), | ||
"BankAccountEvent", | ||
"1.0", | ||
"BankAccount", | ||
), | ||
] |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import decode | ||
import gleam/dynamic | ||
import gleam/json | ||
import gleam/result | ||
|
||
pub type BankAccount { | ||
BankAccount(opened: Bool, balance: Float) | ||
} | ||
|
||
pub const bank_account_type = "BankAccount" | ||
|
||
pub type BankAccountCommand { | ||
OpenAccount(account_id: String) | ||
DepositMoney(amount: Float) | ||
WithDrawMoney(amount: Float) | ||
} | ||
|
||
pub type BankAccountEvent { | ||
AccountOpened(account_id: String) | ||
CustomerDepositedCash(amount: Float, balance: Float) | ||
CustomerWithdrewCash(amount: Float, balance: Float) | ||
} | ||
|
||
pub const bank_account_event_type = "BankAccountEvent" | ||
|
||
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 | ||
case amount >. 0.0 { | ||
True -> Ok([CustomerDepositedCash(amount:, balance:)]) | ||
False -> Error(Nil) | ||
} | ||
} | ||
WithDrawMoney(amount) -> { | ||
let balance = bank_account.balance -. amount | ||
case amount >. 0.0 && balance >. 0.0 { | ||
True -> Ok([CustomerWithdrewCash(amount:, balance:)]) | ||
False -> Error(Nil) | ||
} | ||
} | ||
} | ||
} | ||
|
||
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:) | ||
} | ||
} | ||
|
||
pub fn event_encoder(event: BankAccountEvent) -> String { | ||
case event { | ||
AccountOpened(account_id) -> | ||
json.object([ | ||
#("event-type", json.string("account-opened")), | ||
#("account-id", json.string(account_id)), | ||
]) | ||
CustomerDepositedCash(amount, balance) -> | ||
json.object([ | ||
#("event-type", json.string("customer-deposited-cash")), | ||
#("amount", json.float(amount)), | ||
#("balance", json.float(balance)), | ||
]) | ||
CustomerWithdrewCash(amount, balance) -> | ||
json.object([ | ||
#("event-type", json.string("customer-withdrew-cash")), | ||
#("amount", json.float(amount)), | ||
#("balance", json.float(balance)), | ||
]) | ||
} | ||
|> json.to_string | ||
} | ||
|
||
pub fn event_decoder( | ||
string: String, | ||
) -> Result(BankAccountEvent, List(dynamic.DecodeError)) { | ||
let account_opened_decoder = | ||
decode.into({ | ||
use account_id <- decode.parameter | ||
AccountOpened(account_id) | ||
}) | ||
|> decode.field("account-id", decode.string) | ||
|
||
let customer_deposited_cash = | ||
decode.into({ | ||
use amount <- decode.parameter | ||
use balance <- decode.parameter | ||
CustomerDepositedCash(amount, balance) | ||
}) | ||
|> decode.field("amount", decode.float) | ||
|> decode.field("balance", decode.float) | ||
|
||
let customer_withdrew_cash = | ||
decode.into({ | ||
use amount <- decode.parameter | ||
use balance <- decode.parameter | ||
CustomerWithdrewCash(amount, balance) | ||
}) | ||
|> decode.field("amount", decode.float) | ||
|> decode.field("balance", decode.float) | ||
|
||
let decoder = | ||
decode.at(["event-type"], decode.string) | ||
|> decode.then(fn(event_type) { | ||
case event_type { | ||
"account-opened" -> account_opened_decoder | ||
"customer-deposited-cash" -> customer_deposited_cash | ||
"customer-withdrew-cash" -> customer_withdrew_cash | ||
_ -> decode.fail("event-type") | ||
} | ||
}) | ||
json.decode(from: string, using: decode.from(decoder, _)) | ||
|> result.map_error(fn(_) { [] }) | ||
} |
Oops, something went wrong.