-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
82 lines (66 loc) · 2.09 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
package postgres
import (
"context"
"database/sql"
"encoding/binary"
"errors"
"github.com/jmoiron/sqlx"
"github.com/code-payments/code-server/pkg/code/data/contact"
"github.com/code-payments/code-server/pkg/code/data/user"
)
type store struct {
db *sqlx.DB
}
// New returns a new postgres backed contact.Store
func New(db *sql.DB) contact.Store {
return &store{
db: sqlx.NewDb(db, "pgx"),
}
}
// Add implements contact.Store.Add
func (s *store) Add(ctx context.Context, owner *user.DataContainerID, contact string) error {
model, err := newModel(owner, contact)
if err != nil {
return err
}
return model.dbAdd(ctx, s.db)
}
// BatchAdd implements contact.Store.BatchAdd
func (s *store) BatchAdd(ctx context.Context, owner *user.DataContainerID, contacts []string) error {
return dbBatchAdd(ctx, s.db, owner, contacts)
}
// Remove implements contact.Store.Remove
func (s *store) Remove(ctx context.Context, owner *user.DataContainerID, contact string) error {
model, err := newModel(owner, contact)
if err != nil {
return err
}
return model.dbRemove(ctx, s.db)
}
// BatchRemove implements contact.Store.BatchRemove
func (s *store) BatchRemove(ctx context.Context, owner *user.DataContainerID, contacts []string) error {
return dbBatchRemove(ctx, s.db, owner, contacts)
}
// Get implements contact.Store.Get
func (s *store) Get(ctx context.Context, owner *user.DataContainerID, limit uint32, pageToken []byte) ([]string, []byte, error) {
var exclusiveLowerBoundID uint64
if len(pageToken) == 8 {
exclusiveLowerBoundID = binary.LittleEndian.Uint64(pageToken)
} else if len(pageToken) != 0 {
return nil, nil, errors.New("invalid page token bytes")
}
models, isLastPage, err := dbGetByOwner(ctx, s.db, owner, exclusiveLowerBoundID, limit)
if err != nil {
return nil, nil, err
}
var nextPageToken []byte
if !isLastPage {
nextPageToken = make([]byte, 8)
binary.LittleEndian.PutUint64(nextPageToken, uint64(models[len(models)-1].Id.Int64))
}
contacts := make([]string, len(models))
for i, model := range models {
contacts[i] = model.Contact
}
return contacts, nextPageToken, nil
}