-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.go
156 lines (124 loc) · 3.66 KB
/
model.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
package postgres
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
"github.com/jmoiron/sqlx"
"github.com/code-payments/code-server/pkg/phone"
"github.com/code-payments/code-server/pkg/code/data/user"
pgutil "github.com/code-payments/code-server/pkg/database/postgres"
)
const (
tableName = "codewallet__core_contactlist"
)
type model struct {
Id sql.NullInt64 `db:"id"`
OwnerID string `db:"owner_id"`
Contact string `db:"contact"`
}
func newModel(owner *user.DataContainerID, contact string) (*model, error) {
if err := owner.Validate(); err != nil {
return nil, err
}
if !phone.IsE164Format(contact) {
return nil, errors.New("contact phone number doesn't match E.164 standard")
}
return &model{
OwnerID: owner.String(),
// todo: Drop this eventually.
Contact: contact,
}, nil
}
func newModels(owner *user.DataContainerID, contacts []string) ([]*model, error) {
models := make([]*model, len(contacts))
for i, contact := range contacts {
model, err := newModel(owner, contact)
if err != nil {
return nil, err
}
models[i] = model
}
return models, nil
}
func (m *model) dbAdd(ctx context.Context, db *sqlx.DB) error {
query := `INSERT INTO ` + tableName + `
(owner_id, contact)
VALUES ($1, $2)
ON CONFLICT DO NOTHING
RETURNING id, owner_id, contact`
err := db.QueryRowxContext(ctx, query, m.OwnerID, m.Contact).StructScan(m)
return pgutil.CheckNoRows(err, nil)
}
func (m *model) dbRemove(ctx context.Context, db *sqlx.DB) error {
query := `DELETE FROM ` + tableName + `
WHERE owner_id = $1 and contact = $2
RETURNING id, owner_id, contact`
err := db.QueryRowxContext(ctx, query, m.OwnerID, m.Contact).StructScan(m)
return pgutil.CheckNoRows(err, nil)
}
func dbBatchAdd(ctx context.Context, db *sqlx.DB, owner *user.DataContainerID, contacts []string) error {
if len(contacts) == 0 {
return nil
}
// Mostly doing this to reuse basic validation logic
models, err := newModels(owner, contacts)
if err != nil {
return err
}
// todo: is there a better way to construct the query?
query := fmt.Sprintf("INSERT INTO %s (owner_id, contact) VALUES \n", tableName)
var entries []string
for _, model := range models {
entries = append(entries, fmt.Sprintf("('%s', '%s')", model.OwnerID, model.Contact))
}
query += strings.Join(entries, ",")
query += "\nON CONFLICT (owner_id, contact) DO NOTHING"
_, err = db.ExecContext(ctx, query)
return err
}
func dbBatchRemove(ctx context.Context, db *sqlx.DB, owner *user.DataContainerID, contacts []string) error {
if len(contacts) == 0 {
return nil
}
// Mostly doing this to reuse basic validation logic
models, err := newModels(owner, contacts)
if err != nil {
return err
}
contactArgs := make([]string, len(models))
for i, model := range models {
contactArgs[i] = fmt.Sprintf("'%s'", model.Contact)
}
// todo: is there a better way to construct the query?
query := fmt.Sprintf(
"DELETE FROM %s WHERE owner_id = '%s' AND contact IN (%s)\n",
tableName,
models[0].OwnerID,
strings.Join(contactArgs, ","),
)
_, err = db.ExecContext(ctx, query)
return err
}
func dbGetByOwner(ctx context.Context, db *sqlx.DB, owner *user.DataContainerID, exclusiveLowerBoundID uint64, limit uint32) ([]*model, bool, error) {
var res []*model
var isLastPage bool
query := `
SELECT id, owner_id, contact FROM ` + tableName + `
WHERE owner_id = $1 AND id > $2
ORDER BY id ASC
LIMIT $3
`
err := db.SelectContext(ctx, &res, query, owner.String(), exclusiveLowerBoundID, limit+1)
if err != nil {
return nil, false, err
}
if len(res) > int(limit) {
res = res[:limit]
isLastPage = false
} else {
isLastPage = true
}
return res, isLastPage, nil
}