-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
125 lines (98 loc) · 3.44 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
package postgres
import (
"context"
"database/sql"
"github.com/code-payments/code-server/pkg/database/query"
"github.com/code-payments/code-server/pkg/code/data/payment"
"github.com/jmoiron/sqlx"
)
type store struct {
db *sqlx.DB
}
func New(db *sql.DB) payment.Store {
return &store{
db: sqlx.NewDb(db, "pgx"),
}
}
func (s *store) Get(ctx context.Context, txId string, index uint32) (*payment.Record, error) {
obj, err := dbGet(ctx, s.db, txId, index)
if err != nil {
return nil, err
}
return fromModel(obj), nil
}
func (s *store) Put(ctx context.Context, data *payment.Record) error {
return toModel(data).dbSave(ctx, s.db)
}
func (s *store) Update(ctx context.Context, data *payment.Record) error {
return toModel(data).dbUpdate(ctx, s.db)
}
func (s *store) GetAllForTransaction(ctx context.Context, txId string) ([]*payment.Record, error) {
list, err := dbGetAllForTransaction(ctx, s.db, txId)
if err != nil {
return nil, err
}
res := []*payment.Record{}
for _, item := range list {
res = append(res, fromModel(item))
}
return res, nil
}
func (s *store) GetAllForAccount(ctx context.Context, account string, cursor uint64, limit uint, ordering query.Ordering) ([]*payment.Record, error) {
list, err := dbGetAllForAccount(ctx, s.db, account, cursor, limit, ordering)
if err != nil {
return nil, err
}
res := []*payment.Record{}
for _, item := range list {
res = append(res, fromModel(item))
}
return res, nil
}
func (s *store) GetAllForAccountByType(ctx context.Context, account string, cursor uint64, limit uint, ordering query.Ordering, paymentType payment.PaymentType) ([]*payment.Record, error) {
list, err := dbGetAllForAccountByType(ctx, s.db, account, cursor, limit, ordering, paymentType)
if err != nil {
return nil, err
}
res := []*payment.Record{}
for _, item := range list {
res = append(res, fromModel(item))
}
return res, nil
}
func (s *store) GetAllForAccountByTypeAfterBlock(ctx context.Context, account string, block uint64, cursor uint64, limit uint, ordering query.Ordering, paymentType payment.PaymentType) ([]*payment.Record, error) {
list, err := dbGetAllForAccountByTypeAfterBlock(ctx, s.db, account, block, cursor, limit, ordering, paymentType)
if err != nil {
return nil, err
}
res := []*payment.Record{}
for _, item := range list {
res = append(res, fromModel(item))
}
return res, nil
}
func (s *store) GetAllForAccountByTypeWithinBlockRange(ctx context.Context, account string, lowerBound, upperBound uint64, cursor uint64, limit uint, ordering query.Ordering, paymentType payment.PaymentType) ([]*payment.Record, error) {
list, err := dbGetAllForAccountByTypeWithinBlockRange(ctx, s.db, account, lowerBound, upperBound, cursor, limit, ordering, paymentType)
if err != nil {
return nil, err
}
res := []*payment.Record{}
for _, item := range list {
res = append(res, fromModel(item))
}
return res, nil
}
func (s *store) GetAllExternalDepositsAfterBlock(ctx context.Context, account string, block uint64, cursor uint64, limit uint, ordering query.Ordering) ([]*payment.Record, error) {
list, err := dbGetAllExternalDepositsAfterBlock(ctx, s.db, account, block, cursor, limit, ordering)
if err != nil {
return nil, err
}
res := []*payment.Record{}
for _, item := range list {
res = append(res, fromModel(item))
}
return res, nil
}
func (s *store) GetExternalDepositAmount(ctx context.Context, account string) (uint64, error) {
return dbGetExternalDepositAmount(ctx, s.db, account)
}