-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
86 lines (68 loc) · 1.72 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
package postgres
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
"github.com/code-payments/code-server/pkg/code/data/webhook"
)
type store struct {
db *sqlx.DB
}
// New returns a new postgres-backed webhook.Store
func New(db *sql.DB) webhook.Store {
return &store{
db: sqlx.NewDb(db, "pgx"),
}
}
// Put implements webhook.Store.Put
func (s *store) Put(ctx context.Context, record *webhook.Record) error {
obj, err := toModel(record)
if err != nil {
return err
}
err = obj.dbPut(ctx, s.db)
if err != nil {
return err
}
res := fromModel(obj)
res.CopyTo(record)
return nil
}
// Update implements webhook.Store.Update
func (s *store) Update(ctx context.Context, record *webhook.Record) error {
obj, err := toModel(record)
if err != nil {
return err
}
err = obj.dbUpdate(ctx, s.db)
if err != nil {
return err
}
res := fromModel(obj)
res.CopyTo(record)
return nil
}
// Get implements webhook.Store.Get
func (s *store) Get(ctx context.Context, webhookId string) (*webhook.Record, error) {
model, err := dbGetByWebhookId(ctx, s.db, webhookId)
if err != nil {
return nil, err
}
return fromModel(model), nil
}
// CountByState implements webhook.Store.CountByState
func (s *store) CountByState(ctx context.Context, state webhook.State) (uint64, error) {
return dbCountByState(ctx, s.db, state)
}
// GetAllPendingReadyToSend implements webhook.Store.GetAllPendingReadyToSend
func (s *store) GetAllPendingReadyToSend(ctx context.Context, limit uint64) ([]*webhook.Record, error) {
models, err := dbGetAllPendingReadyToSend(ctx, s.db, limit)
if err != nil {
return nil, err
}
var res []*webhook.Record
for _, model := range models {
res = append(res, fromModel(model))
}
return res, nil
}