-
Notifications
You must be signed in to change notification settings - Fork 24
/
cqrs-testreadmodels_test.go
197 lines (155 loc) · 5.48 KB
/
cqrs-testreadmodels_test.go
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package cqrs_test
import (
"fmt"
"sync"
"github.com/andrewwebber/cqrs"
)
type AccountReadModel struct {
ID string
FirstName string
LastName string
EmailAddress string
Balance float64
}
func (account *AccountReadModel) String() string {
return fmt.Sprintf("AccountModel::Account %s with Email Address %s has balance %f", account.ID, account.EmailAddress, account.Balance)
}
type ReadModelAccounts struct {
accounts map[string]*AccountReadModel
lock sync.RWMutex
}
func (model *ReadModelAccounts) String() string {
model.lock.RLock()
defer model.lock.RUnlock()
result := "Account Model::"
for key := range model.accounts {
result += model.accounts[key].String() + "\n"
}
return result
}
func NewReadModelAccounts() *ReadModelAccounts {
return &ReadModelAccounts{make(map[string]*AccountReadModel), sync.RWMutex{}}
}
func NewReadModelAccountsFromHistory(events []cqrs.VersionedEvent) (*ReadModelAccounts, error) {
publisher := NewReadModelAccounts()
if error := publisher.UpdateViewModel(events); error != nil {
return nil, error
}
return publisher, nil
}
func (model *ReadModelAccounts) GetAccount(id string) *AccountReadModel {
model.lock.RLock()
defer model.lock.RUnlock()
account, _ := model.accounts[id]
return account
}
func (model *ReadModelAccounts) UpdateViewModel(events []cqrs.VersionedEvent) error {
model.lock.Lock()
defer model.lock.Unlock()
for _, event := range events {
cqrs.PackageLogger().Debugf("Accounts Model received event : " + event.EventType)
switch event.Event.(type) {
default:
return nil
case AccountCreatedEvent:
model.UpdateViewModelOnAccountCreatedEvent(event.SourceID, event.Event.(AccountCreatedEvent))
case AccountCreditedEvent:
model.UpdateViewModelOnAccountCreditedEvent(event.SourceID, event.Event.(AccountCreditedEvent))
case AccountDebitedEvent:
model.UpdateViewModelOnAccountDebitedEvent(event.SourceID, event.Event.(AccountDebitedEvent))
case EmailAddressChangedEvent:
model.UpdateViewModelOnEmailAddressChangedEvent(event.SourceID, event.Event.(EmailAddressChangedEvent))
}
}
return nil
}
func (model *ReadModelAccounts) UpdateViewModelOnAccountCreatedEvent(accountID string, event AccountCreatedEvent) {
model.accounts[accountID] = &AccountReadModel{accountID, event.FirstName, event.LastName, event.EmailAddress, event.InitialBalance}
}
func (model *ReadModelAccounts) UpdateViewModelOnAccountCreditedEvent(accountID string, event AccountCreditedEvent) {
if model.accounts[accountID] == nil {
cqrs.PackageLogger().Debugf("Could not find account with ID " + accountID)
return
}
model.accounts[accountID].Balance += event.Amount
}
func (model *ReadModelAccounts) UpdateViewModelOnAccountDebitedEvent(accountID string, event AccountDebitedEvent) {
if model.accounts[accountID] == nil {
cqrs.PackageLogger().Debugf("Could not find account with ID " + accountID)
return
}
model.accounts[accountID].Balance -= event.Amount
}
func (model *ReadModelAccounts) UpdateViewModelOnEmailAddressChangedEvent(accountID string, event EmailAddressChangedEvent) {
if model.accounts[accountID] == nil {
cqrs.PackageLogger().Debugf("Could not find account with ID " + accountID)
return
}
model.accounts[accountID].EmailAddress = event.NewEmailAddress
}
type User struct {
ID string
FirstName string
LastName string
EmailAddress string
PasswordHash []byte
}
func (user *User) String() string {
return fmt.Sprintf("UserModel::User %s with Email Address %s and Password Hash %v", user.ID, user.EmailAddress, user.PasswordHash)
}
type UsersModel struct {
Users map[string]*User
lock sync.Mutex
}
func (model *UsersModel) String() string {
model.lock.Lock()
defer model.lock.Unlock()
result := "User Model::"
for key := range model.Users {
result += model.Users[key].String() + "\n"
}
return result
}
func NewUsersModel() *UsersModel {
return &UsersModel{make(map[string]*User), sync.Mutex{}}
}
func NewUsersModelFromHistory(events []cqrs.VersionedEvent) (*UsersModel, error) {
publisher := NewUsersModel()
if error := publisher.UpdateViewModel(events); error != nil {
return nil, error
}
return publisher, nil
}
func (model *UsersModel) UpdateViewModel(events []cqrs.VersionedEvent) error {
model.lock.Lock()
defer model.lock.Unlock()
for _, event := range events {
cqrs.PackageLogger().Debugf("User Model received event : ", event.EventType)
switch event.Event.(type) {
default:
return nil
case AccountCreatedEvent:
model.UpdateViewModelOnAccountCreatedEvent(event.SourceID, event.Event.(AccountCreatedEvent))
case EmailAddressChangedEvent:
model.UpdateViewModelOnEmailAddressChangedEvent(event.SourceID, event.Event.(EmailAddressChangedEvent))
case PasswordChangedEvent:
model.UpdateViewModelOnPasswordChangedEvent(event.SourceID, event.Event.(PasswordChangedEvent))
}
}
return nil
}
func (model *UsersModel) UpdateViewModelOnAccountCreatedEvent(accountID string, event AccountCreatedEvent) {
model.Users[accountID] = &User{accountID, event.FirstName, event.LastName, event.EmailAddress, event.PasswordHash}
}
func (model *UsersModel) UpdateViewModelOnEmailAddressChangedEvent(accountID string, event EmailAddressChangedEvent) {
if model.Users[accountID] == nil {
return
}
model.Users[accountID].EmailAddress = event.NewEmailAddress
}
func (model *UsersModel) UpdateViewModelOnPasswordChangedEvent(accountID string, event PasswordChangedEvent) {
if model.Users[accountID] == nil {
return
}
model.Users[accountID].PasswordHash = event.NewPasswordHash
}