-
Notifications
You must be signed in to change notification settings - Fork 6
/
bank_account.go
53 lines (44 loc) · 881 Bytes
/
bank_account.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
package main
import (
"errors"
"strconv"
)
type BankAccount struct {
Id string
Name string
Balance int
}
func FetchAccount(id string) (*BankAccount, error) {
cmd := Redis.HGetAll(id)
if err := cmd.Err(); err != nil {
return nil, err
}
data := cmd.Val()
if len(data) == 0 {
return nil, nil
} else {
return ToAccount(data)
}
}
func updateAccount(id string, data map[string]interface{}) (*BankAccount, error) {
cmd := Redis.HMSet(id, data)
if err := cmd.Err(); err != nil {
return nil, err
} else {
return FetchAccount(id)
}
}
func ToAccount(m map[string]string) (*BankAccount, error) {
balance, err := strconv.Atoi(m["Balance"])
if err != nil {
return nil, err
}
if _, ok := m["Id"]; !ok {
return nil, errors.New("Missing account ID")
}
return &BankAccount{
Id: m["Id"],
Name: m["Name"],
Balance: balance,
}, nil
}