-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
47 lines (37 loc) · 921 Bytes
/
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
package postgres
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
"github.com/code-payments/code-server/pkg/code/data/balance"
)
type store struct {
db *sqlx.DB
}
// New returns a new postgres balance.Store
func New(db *sql.DB) balance.Store {
return &store{
db: sqlx.NewDb(db, "pgx"),
}
}
// SaveCheckpoint implements balance.Store.SaveCheckpoint
func (s *store) SaveCheckpoint(ctx context.Context, record *balance.Record) error {
model, err := toModel(record)
if err != nil {
return err
}
if err := model.dbSave(ctx, s.db); err != nil {
return err
}
res := fromModel(model)
res.CopyTo(record)
return nil
}
// GetCheckpoint implements balance.Store.GetCheckpoint
func (s *store) GetCheckpoint(ctx context.Context, account string) (*balance.Record, error) {
model, err := dbGetCheckpoint(ctx, s.db, account)
if err != nil {
return nil, err
}
return fromModel(model), nil
}