-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.go
152 lines (126 loc) · 3.28 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
package postgres
import (
"context"
"database/sql"
"errors"
"time"
"github.com/jmoiron/sqlx"
pgutil "github.com/code-payments/code-server/pkg/database/postgres"
"github.com/code-payments/code-server/pkg/code/data/login"
)
const (
tableName = "codewallet__core_applogin"
)
type model struct {
Id sql.NullInt64 `db:"id"`
AppInstallId string `db:"app_install_id"`
Owner string `db:"owner"`
LastUpdatedAt time.Time `db:"last_updated_at"`
}
func toModels(item *login.MultiRecord) ([]*model, error) {
if err := item.Validate(); err != nil {
return nil, err
}
var res []*model
for _, owner := range item.Owners {
res = append(res, &model{
AppInstallId: item.AppInstallId,
Owner: owner,
LastUpdatedAt: item.LastUpdatedAt,
})
}
return res, nil
}
func fromModel(m *model) *login.Record {
return &login.Record{
AppInstallId: m.AppInstallId,
Owner: m.Owner,
LastUpdatedAt: m.LastUpdatedAt,
}
}
func fromModels(appInstallId string, models []*model) (*login.MultiRecord, error) {
res := &login.MultiRecord{
AppInstallId: appInstallId,
LastUpdatedAt: time.Now(),
}
var lastUpdatedAt time.Time
for _, model := range models {
if model.AppInstallId != appInstallId {
return nil, errors.New("models are not from the expected app install")
}
res.Owners = append(res.Owners, model.Owner)
if model.LastUpdatedAt.After(lastUpdatedAt) {
lastUpdatedAt = model.LastUpdatedAt
}
}
if len(models) > 0 {
res.LastUpdatedAt = lastUpdatedAt
}
return res, nil
}
func (m *model) dbSaveInTx(ctx context.Context, tx *sqlx.Tx) error {
m.LastUpdatedAt = time.Now()
err := dbDeleteAllByInstallIdInTx(ctx, tx, m.AppInstallId)
if err != nil {
return err
}
err = dbDeleteAllByOwnerInTx(ctx, tx, m.Owner)
if err != nil {
return err
}
query := `INSERT INTO ` + tableName + `
(app_install_id, owner, last_updated_at)
VALUES ($1, $2, $3)
RETURNING id, app_install_id, owner, last_updated_at`
_, err = tx.ExecContext(
ctx,
query,
m.AppInstallId,
m.Owner,
m.LastUpdatedAt,
)
return err
}
func dbDeleteAllByInstallIdInTx(ctx context.Context, tx *sqlx.Tx, appInstallId string) error {
query := `DELETE FROM ` + tableName + `
WHERE app_install_id = $1`
_, err := tx.ExecContext(
ctx,
query,
appInstallId,
)
return err
}
func dbDeleteAllByOwnerInTx(ctx context.Context, tx *sqlx.Tx, owner string) error {
query := `DELETE FROM ` + tableName + `
WHERE owner = $1`
_, err := tx.ExecContext(
ctx,
query,
owner,
)
return err
}
func dbGetAllByInstallId(ctx context.Context, db *sqlx.DB, appInstallId string) ([]*model, error) {
var res []*model
query := `SELECT id, app_install_id, owner, last_updated_at FROM ` + tableName + `
WHERE app_install_id = $1`
err := db.SelectContext(ctx, &res, query, appInstallId)
if err != nil {
return nil, pgutil.CheckNoRows(err, login.ErrLoginNotFound)
}
if len(res) == 0 {
return nil, login.ErrLoginNotFound
}
return res, nil
}
func dbGetLatestByOwner(ctx context.Context, db *sqlx.DB, owner string) (*model, error) {
var res model
query := `SELECT id, app_install_id, owner, last_updated_at FROM ` + tableName + `
WHERE owner = $1`
err := db.GetContext(ctx, &res, query, owner)
if err != nil {
return nil, pgutil.CheckNoRows(err, login.ErrLoginNotFound)
}
return &res, nil
}