forked from andrewwebber/cqrs
/
cqrs-testentities_test.go
193 lines (148 loc) · 4.38 KB
/
cqrs-testentities_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
package cqrs_test
import (
"errors"
"fmt"
"log"
"github.com/andrewwebber/cqrs"
"golang.org/x/crypto/bcrypt"
)
type ChangeEmailAddressCommand struct {
AccountID string
NewEmailAddress string
}
type CreateAccountCommand struct {
FirstName string
LastName string
EmailAddress string
PasswordHash []byte
InitialBalance float64
}
type ChangePasswordCommand struct {
AccountID string
NewPassword string
}
type CreditAccountCommand struct {
AccountID string
Amount float64
}
type DebitAccountCommand struct {
AccountID string
Amount float64
}
type AccountCreatedEvent struct {
FirstName string
LastName string
EmailAddress string
PasswordHash []byte
InitialBalance float64
}
type EmailAddressChangedEvent struct {
PreviousEmailAddress string
NewEmailAddress string
}
type PasswordChangedEvent struct {
NewPasswordHash []byte
}
type AccountCreditedEvent struct {
Amount float64
}
type AccountDebitedEvent struct {
Amount float64
}
type Account struct {
cqrs.EventSourceBased
FirstName string
LastName string
EmailAddress string
PasswordHash []byte
Balance float64
}
func (account *Account) String() string {
return fmt.Sprintf("Account %s with Email Address %s has balance %f", account.ID(), account.EmailAddress, account.Balance)
}
func NewAccount(firstName string, lastName string, emailAddress string, passwordHash []byte, initialBalance float64) *Account {
account := new(Account)
account.EventSourceBased = cqrs.NewEventSourceBased(account)
event := AccountCreatedEvent{firstName, lastName, emailAddress, passwordHash, initialBalance}
account.Update(event)
return account
}
func NewAccountFromHistory(id string, repository cqrs.EventSourcingRepository) (*Account, error) {
account := new(Account)
account.EventSourceBased = cqrs.NewEventSourceBasedWithID(account, id)
if error := repository.Get(id, account); error != nil {
return account, error
}
return account, nil
}
func (account *Account) HandleAccountCreatedEvent(event AccountCreatedEvent) {
account.EmailAddress = event.EmailAddress
account.FirstName = event.FirstName
account.LastName = event.LastName
account.PasswordHash = event.PasswordHash
}
func (account *Account) ChangeEmailAddress(newEmailAddress string) error {
if len(newEmailAddress) < 1 {
return errors.New("invalid newEmailAddress length")
}
account.Update(EmailAddressChangedEvent{account.EmailAddress, newEmailAddress})
return nil
}
func (account *Account) HandleEmailAddressChangedEvent(event EmailAddressChangedEvent) {
account.EmailAddress = event.NewEmailAddress
}
func (account *Account) CheckPassword(password string) bool {
passwordBytes := []byte(password)
// Comparing the password with the hash
err := bcrypt.CompareHashAndPassword(account.PasswordHash, passwordBytes)
fmt.Println(err) // nil means it is a match
return err == nil
}
func (account *Account) ChangePassword(newPassword string) error {
if len(newPassword) < 1 {
return errors.New("invalid newPassword length")
}
hashedPassword, err := GetHashForPassword(newPassword)
if err != nil {
panic(err)
}
account.Update(PasswordChangedEvent{hashedPassword})
return nil
}
func GetHashForPassword(password string) ([]byte, error) {
passwordBytes := []byte(password)
// Hashing the password with the cost of 10
hashedPassword, err := bcrypt.GenerateFromPassword(passwordBytes, 10)
if err != nil {
log.Println("Error getting password hash: ", err)
return nil, err
}
fmt.Println(string(hashedPassword))
return hashedPassword, nil
}
func (account *Account) HandlePasswordChangedEvent(event PasswordChangedEvent) {
account.PasswordHash = event.NewPasswordHash
}
func (account *Account) Credit(amount float64) error {
if amount <= 0 {
return errors.New("invalid amount - negative credits not supported")
}
account.Update(AccountCreditedEvent{amount})
return nil
}
func (account *Account) HandleAccountCredited(event AccountCreditedEvent) {
account.Balance += event.Amount
}
func (account *Account) Debit(amount float64) error {
if amount <= 0 {
return errors.New("invalid amount - negative credits not supported")
}
if projection := account.Balance - amount; projection < 0 {
return errors.New("negative balance not supported")
}
account.Update(AccountDebitedEvent{amount})
return nil
}
func (account *Account) HandleAccountDebitedEvent(event AccountDebitedEvent) {
account.Balance -= event.Amount
}