-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
148 lines (122 loc) · 4.15 KB
/
store.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
package postgres
import (
"context"
"database/sql"
"time"
"github.com/jmoiron/sqlx"
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
"github.com/code-payments/code-server/pkg/code/data/account"
)
type store struct {
db *sqlx.DB
}
func New(db *sql.DB) account.Store {
return &store{
db: sqlx.NewDb(db, "pgx"),
}
}
// Put implements account.Store.Put
func (s *store) Put(ctx context.Context, record *account.Record) error {
model, err := toModel(record)
if err != nil {
return err
}
err = model.dbInsert(ctx, s.db)
if err != nil {
return err
}
updated := fromModel(model)
updated.CopyTo(record)
return nil
}
// Update implements account.Store.Update
func (s *store) Update(ctx context.Context, record *account.Record) error {
model, err := toModel(record)
if err != nil {
return err
}
err = model.dbUpdate(ctx, s.db)
if err != nil {
return err
}
updated := fromModel(model)
updated.CopyTo(record)
return nil
}
// GetByTokenAddress implements account.Store.GetByTokenAddress
func (s *store) GetByTokenAddress(ctx context.Context, address string) (*account.Record, error) {
model, err := dbGetByTokenAddress(ctx, s.db, address)
if err != nil {
return nil, err
}
return fromModel(model), nil
}
// GetByAuthorityAddress implements account.Store.GetByAuthorityAddress
func (s *store) GetByAuthorityAddress(ctx context.Context, address string) (*account.Record, error) {
model, err := dbGetByAuthorityAddress(ctx, s.db, address)
if err != nil {
return nil, err
}
return fromModel(model), nil
}
// GetLatestByOwnerAddress implements account.Store.GetLatestByOwnerAddress
func (s *store) GetLatestByOwnerAddress(ctx context.Context, address string) (map[commonpb.AccountType][]*account.Record, error) {
modelsByType, err := dbGetLatestByOwnerAddress(ctx, s.db, address)
if err != nil {
return nil, err
}
res := make(map[commonpb.AccountType][]*account.Record)
for _, model := range modelsByType {
record := fromModel(model)
res[record.AccountType] = append(res[record.AccountType], record)
}
return res, nil
}
// GetLatestByOwnerAddressAndType implements account.Store.GetLatestByOwnerAddressAndType
func (s *store) GetLatestByOwnerAddressAndType(ctx context.Context, address string, accountType commonpb.AccountType) (*account.Record, error) {
model, err := dbGetLatestByOwnerAddressAndType(ctx, s.db, address, accountType)
if err != nil {
return nil, err
}
return fromModel(model), nil
}
// GetRelationshipByOwnerAddress implements account.Store.GetRelationshipByOwnerAddress
func (s *store) GetRelationshipByOwnerAddress(ctx context.Context, address, relationshipTo string) (*account.Record, error) {
model, err := dbGetRelationshipByOwnerAddress(ctx, s.db, address, relationshipTo)
if err != nil {
return nil, err
}
return fromModel(model), nil
}
// GetPrioritizedRequiringDepositSync implements account.Store.GetPrioritizedRequiringDepositSync
func (s *store) GetPrioritizedRequiringDepositSync(ctx context.Context, limit uint64) ([]*account.Record, error) {
models, err := dbGetPrioritizedRequiringDepositSync(ctx, s.db, limit)
if err != nil {
return nil, err
}
res := make([]*account.Record, len(models))
for i, model := range models {
res[i] = fromModel(model)
}
return res, nil
}
// CountRequiringDepositSync implements account.Store.CountRequiringDepositSync
func (s *store) CountRequiringDepositSync(ctx context.Context) (uint64, error) {
return dbCountRequiringDepositSync(ctx, s.db)
}
// GetPrioritizedRequiringAutoReturnCheck implements account.Store.GetPrioritizedRequiringAutoReturnCheck
func (s *store) GetPrioritizedRequiringAutoReturnCheck(ctx context.Context, minAge time.Duration, limit uint64) ([]*account.Record, error) {
models, err := dbGetPrioritizedRequiringAutoReturnChecks(ctx, s.db, minAge, limit)
if err != nil {
return nil, err
}
res := make([]*account.Record, len(models))
for i, model := range models {
res[i] = fromModel(model)
}
return res, nil
}
// CountRequiringAutoReturnCheck implements account.Store.CountRequiringAutoReturnCheck
func (s *store) CountRequiringAutoReturnCheck(ctx context.Context) (uint64, error) {
return dbCountRequiringAutoReturnCheck(ctx, s.db)
}