-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdoc_test.go
More file actions
137 lines (114 loc) · 3.79 KB
/
doc_test.go
File metadata and controls
137 lines (114 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package example
import (
"context"
"fmt"
"os"
"github.com/google/uuid"
"github.com/Rhymond/go-money"
qdaptor "github.com/screwyprof/payment/internal/pkg/adaptor/query_handler"
"github.com/screwyprof/payment/internal/pkg/bus"
"github.com/screwyprof/payment/internal/pkg/cqrs"
"github.com/screwyprof/payment/internal/pkg/observer"
"github.com/screwyprof/payment/internal/pkg/reporting"
"github.com/screwyprof/payment/pkg/command"
"github.com/screwyprof/payment/pkg/domain"
"github.com/screwyprof/payment/pkg/domain/account"
"github.com/screwyprof/payment/pkg/event_handler"
"github.com/screwyprof/payment/pkg/query"
"github.com/screwyprof/payment/pkg/query_handler"
"github.com/screwyprof/payment/pkg/report"
)
func Example() {
// init deps
cqrs.RegisterAggregate(func(id uuid.UUID) domain.Aggregate {
return account.Create(id)
})
accountReporter := reporting.NewInMemoryAccountReporter()
notifier := observer.NewNotifier()
notifier.Register(event_handler.NewAccountOpened(accountReporter))
notifier.Register(event_handler.NewMoneyTransfered(accountReporter))
notifier.Register(event_handler.NewMoneyReceived(accountReporter))
eventStore := cqrs.NewInMemoryEventStore()
commandHandler := cqrs.NewEventSourceHandler(eventStore, notifier)
accountQueryer := query_handler.NewGetAccountShortInfo(accountReporter)
queryBus := newQueryBus(accountQueryer)
// Run test script
// create a couple of accounts
acc1ID := uuid.New()
acc1Number := "ACC500" //account.GenerateAccNumber()
openAccount(commandHandler, acc1ID, acc1Number, *money.New(50000, "USD"))
acc2ID := uuid.New()
acc2Number := "ACC300" //account.GenerateAccNumber()
openAccount(commandHandler, acc2ID, acc2Number, *money.New(30000, "USD"))
// get accounts info
queryAccount(queryBus, string(acc1Number))
queryAccount(queryBus, string(acc2Number))
// transfer money and show results
transferMoney(commandHandler, acc1ID, acc2ID, acc1Number, acc2Number, *money.New(30000, "USD"))
queryAccount(queryBus, string(acc1Number))
queryAccount(queryBus, string(acc2Number))
// Output:
//
// #ACC500: $500.00
// Ledgers:
// #1. Deposit, $500.00
// #ACC300: $300.00
// Ledgers:
// #1. Deposit, $300.00
// #ACC500: $200.00
// Ledgers:
// #1. Deposit, $500.00
// #2. Transfer to ACC300, $300.00
// #ACC300: $600.00
// Ledgers:
// #1. Deposit, $300.00
// #2. Transfer from ACC500, $300.00
}
func newQueryBus(accountQueryer domain.QueryHandler) domain.QueryHandler {
queryBus := bus.NewQueryHandlerBus()
queryBus.Register("GetAccountShortInfo", qdaptor.FromDomain(accountQueryer))
return qdaptor.ToDomain(queryBus)
}
func openAccount(commandBus domain.CommandHandler, ID uuid.UUID, number string, balance money.Money) {
openAccountCmd := command.OpenAccount{
AggID: ID,
Number: number,
Balance: balance,
}
failOnError(commandBus.Handle(context.Background(), openAccountCmd))
}
func transferMoney(commandBus domain.CommandHandler, fromID, toID uuid.UUID, from, to string, amount money.Money) {
transferCmd := command.TransferMoney{
AggID: fromID,
From: from,
To: to,
Amount: amount,
}
failOnError(commandBus.Handle(context.Background(), transferCmd))
receiveCmd := command.ReceiveMoney{
AggID: toID,
From: from,
To: to,
Amount: amount,
}
failOnError(commandBus.Handle(context.Background(), receiveCmd))
}
func queryAccount(queryBus domain.QueryHandler, number string) {
acc := &report.Account{}
err := queryBus.Handle(context.Background(), query.GetAccountShortInfo{Number: number}, acc)
failOnError(err)
fmt.Println(acc.ToString())
if len(acc.Ledgers) < 1 {
return
}
fmt.Println("Ledgers:")
for idx, ledger := range acc.Ledgers {
fmt.Printf("#%d. %s\n", idx+1, ledger.ToString())
}
}
func failOnError(err error) {
if err != nil {
fmt.Printf("an error occured: %v", err)
os.Exit(1)
}
}