-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprocessor.go
65 lines (60 loc) · 1.47 KB
/
processor.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
package main
import (
"errors"
)
func (e CreateEvent) Process() (*BankAccount, error) {
return updateAccount(e.AccId, map[string]interface{}{
"Id": e.AccId,
"Name": e.AccName,
"Balance": "0",
})
}
func (e DepositEvent) Process() (*BankAccount, error) {
if acc, err := FetchAccount(e.AccId); err != nil {
return nil, err
} else {
newBalance := acc.Balance + e.Amount
return updateAccount(e.AccId, map[string]interface{}{
"Balance": newBalance,
})
}
}
func (e WithdrawEvent) Process() (*BankAccount, error) {
if acc, err := FetchAccount(e.AccId); err != nil {
return nil, err
} else {
if acc.Balance >= e.Amount {
newBalance := acc.Balance - e.Amount
return updateAccount(e.AccId, map[string]interface{}{
"Balance": newBalance,
})
} else {
return nil, errors.New("Insufficient amount")
}
}
}
func (e TransferEvent) Process() (*BankAccount, error) {
if acc, err := FetchAccount(e.AccId); err != nil {
return nil, err
} else {
if destAcc, err := FetchAccount(e.TargetId); err != nil {
return nil, err
} else {
if acc.Balance >= e.Amount {
acc.Balance -= e.Amount
destAcc.Balance += e.Amount
if _, err := updateAccount(destAcc.Id, map[string]interface{}{
"Balance": destAcc.Balance,
}); err != nil {
return nil, err
} else {
return updateAccount(acc.Id, map[string]interface{}{
"Balance": acc.Balance,
})
}
} else {
return nil, errors.New("Insufficient amount")
}
}
}
}