-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
161 lines (128 loc) · 5.18 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
149
150
151
152
153
154
155
156
157
158
159
160
161
package postgres
import (
"context"
"database/sql"
"time"
"github.com/jmoiron/sqlx"
"github.com/code-payments/code-server/pkg/code/data/phone"
)
type store struct {
db *sqlx.DB
}
// New returns a postgres backed phone.Store.
func New(db *sql.DB) phone.Store {
return &store{
db: sqlx.NewDb(db, "pgx"),
}
}
// SaveVerification implements phone.Store.SaveVerification
func (s *store) SaveVerification(ctx context.Context, v *phone.Verification) error {
model, err := toVerificationModel(v)
if err != nil {
return err
}
return model.dbSave(ctx, s.db)
}
// GetVerification implements phone.Store.GetVerification
func (s *store) GetVerification(ctx context.Context, account, phoneNumber string) (*phone.Verification, error) {
model, err := dbGetVerification(ctx, s.db, account, phoneNumber)
if err != nil {
return nil, err
}
return fromVerificationModel(model), nil
}
// GetLatestVerificationForAccount implements phone.Store.GetLatestVerificationForAccount
func (s *store) GetLatestVerificationForAccount(ctx context.Context, account string) (*phone.Verification, error) {
model, err := dbGetLatestVerificationForAccount(ctx, s.db, account)
if err != nil {
return nil, err
}
return fromVerificationModel(model), nil
}
// GetLatestVerificationForNumber implements phone.Store.GetLatestVerificationForNumber
func (s *store) GetLatestVerificationForNumber(ctx context.Context, phoneNumber string) (*phone.Verification, error) {
model, err := dbGetLatestVerificationForNumber(ctx, s.db, phoneNumber)
if err != nil {
return nil, err
}
return fromVerificationModel(model), nil
}
// GetAllVerificationsForNumber implements phone.Store.GetAllVerificationsForNumber
func (s *store) GetAllVerificationsForNumber(ctx context.Context, phoneNumber string) ([]*phone.Verification, error) {
models, err := dbGetAllVerificationsForNumber(ctx, s.db, phoneNumber)
if err != nil {
return nil, err
}
verifications := make([]*phone.Verification, len(models))
for i, model := range models {
verifications[i] = fromVerificationModel(model)
}
return verifications, nil
}
// SaveLinkingToken implements phone.Store.SaveLinkingToken
func (s *store) SaveLinkingToken(ctx context.Context, token *phone.LinkingToken) error {
model, err := toLinkingTokenModel(token)
if err != nil {
return err
}
return model.dbSave(ctx, s.db)
}
// UseLinkingToken implements phone.Store.UseLinkingToken
func (s *store) UseLinkingToken(ctx context.Context, phoneNumber, code string) error {
return dbUseLinkingToken(ctx, s.db, phoneNumber, code)
}
// FilterVerifiedNumbers implements phone.Store.FilterVerifiedNumbers
func (s *store) FilterVerifiedNumbers(ctx context.Context, phoneNumbers []string) ([]string, error) {
return dbFilterVerifiedNumbers(ctx, s.db, phoneNumbers)
}
// GetSettings implements phone.Store.GetSettings
func (s *store) GetSettings(ctx context.Context, phoneNumber string) (*phone.Settings, error) {
models, err := dbGetOwnerAccountSettings(ctx, s.db, phoneNumber)
if err != nil {
return nil, err
}
settings := &phone.Settings{
PhoneNumber: phoneNumber,
ByOwnerAccount: make(map[string]*phone.OwnerAccountSetting),
}
for _, model := range models {
settings.ByOwnerAccount[model.OwnerAccount] = fromOwnerAccountSettingModel(model)
}
return settings, nil
}
// SaveOwnerAccountSetting implements phone.Store.SaveOwnerAccountSetting
func (s *store) SaveOwnerAccountSetting(ctx context.Context, phoneNumber string, newSettings *phone.OwnerAccountSetting) error {
model, err := toOwnerAccountSettingModel(phoneNumber, newSettings)
if err != nil {
return err
}
return model.dbSave(ctx, s.db)
}
// PutEvent implements phone.Store.PutEvent
func (s *store) PutEvent(ctx context.Context, event *phone.Event) error {
model, err := toEventModel(event)
if err != nil {
return err
}
return model.dbSave(ctx, s.db)
}
// GetLatestEventForNumberByType implements phone.Store.GetLatestEventForNumberByType
func (s *store) GetLatestEventForNumberByType(ctx context.Context, phoneNumber string, eventType phone.EventType) (*phone.Event, error) {
model, err := dbGetLatestEventForNumberByType(ctx, s.db, phoneNumber, eventType)
if err != nil {
return nil, err
}
return fromEventModel(model), nil
}
// CountEventsForVerificationByType implements phone.Store.CountEventsForVerificationByType
func (s *store) CountEventsForVerificationByType(ctx context.Context, verification string, eventType phone.EventType) (uint64, error) {
return dbCountEventsForVerificationByType(ctx, s.db, verification, eventType)
}
// CountEventsForNumberByTypeSinceTimestamp implements phone.Store.CountEventsForNumberByTypeSinceTimestamp
func (s *store) CountEventsForNumberByTypeSinceTimestamp(ctx context.Context, phoneNumber string, eventType phone.EventType, since time.Time) (uint64, error) {
return dbCountEventsForNumberByTypeSinceTimestamp(ctx, s.db, phoneNumber, eventType, since)
}
// CountUniqueVerificationIdsForNumberSinceTimestamp implements phone.Store.CountUniqueVerificationIdsForNumberSinceTimestamp
func (s *store) CountUniqueVerificationIdsForNumberSinceTimestamp(ctx context.Context, phoneNumber string, since time.Time) (uint64, error) {
return dbCountUniqueVerificationIdsForNumberSinceTimestamp(ctx, s.db, phoneNumber, since)
}