-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.go
165 lines (137 loc) · 3.88 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
157
158
159
160
161
162
163
164
165
package postgres
import (
"context"
"database/sql"
"time"
"github.com/jmoiron/sqlx"
pgutil "github.com/code-payments/code-server/pkg/database/postgres"
"github.com/code-payments/code-server/pkg/pointer"
"github.com/code-payments/code-server/pkg/code/data/webhook"
)
const (
tableName = "codewallet__core_webhook"
)
type model struct {
Id sql.NullInt64 `db:"id"`
WebhookId string `db:"webhook_id"`
Url string `db:"url"`
Type uint8 `db:"webhook_type"`
Attempts uint8 `db:"attempts"`
State uint8 `db:"state"`
CreatedAt time.Time `db:"created_at"`
NextAttemptAt sql.NullTime `db:"next_attempt_at"`
}
func toModel(obj *webhook.Record) (*model, error) {
if err := obj.Validate(); err != nil {
return nil, err
}
return &model{
WebhookId: obj.WebhookId,
Url: obj.Url,
Type: uint8(obj.Type),
Attempts: obj.Attempts,
State: uint8(obj.State),
CreatedAt: obj.CreatedAt,
NextAttemptAt: sql.NullTime{
Valid: obj.NextAttemptAt != nil,
Time: *pointer.TimeOrDefault(obj.NextAttemptAt, time.Time{}),
},
}, nil
}
func fromModel(obj *model) *webhook.Record {
return &webhook.Record{
Id: uint64(obj.Id.Int64),
WebhookId: obj.WebhookId,
Url: obj.Url,
Type: webhook.Type(obj.Type),
Attempts: obj.Attempts,
State: webhook.State(obj.State),
CreatedAt: obj.CreatedAt,
NextAttemptAt: pointer.TimeIfValid(obj.NextAttemptAt.Valid, obj.NextAttemptAt.Time),
}
}
func (m *model) dbPut(ctx context.Context, db *sqlx.DB) error {
err := pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
query := `INSERT INTO ` + tableName + `
(webhook_id, url, webhook_type, attempts, state, created_at, next_attempt_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, webhook_id, url, webhook_type, attempts, state, created_at, next_attempt_at
`
if m.CreatedAt.IsZero() {
m.CreatedAt = time.Now()
}
return tx.QueryRowxContext(
ctx,
query,
m.WebhookId,
m.Url,
m.Type,
m.Attempts,
m.State,
m.CreatedAt,
m.NextAttemptAt,
).StructScan(m)
})
return pgutil.CheckUniqueViolation(err, webhook.ErrAlreadyExists)
}
func (m *model) dbUpdate(ctx context.Context, db *sqlx.DB) error {
err := pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
query := `UPDATE ` + tableName + `
SET attempts = $2, state = $3, next_attempt_at = $4
WHERE webhook_id = $1
RETURNING id, webhook_id, url, webhook_type, attempts, state, created_at, next_attempt_at
`
return tx.QueryRowxContext(
ctx,
query,
m.WebhookId,
m.Attempts,
m.State,
m.NextAttemptAt,
).StructScan(m)
})
return pgutil.CheckNoRows(err, webhook.ErrNotFound)
}
func dbGetByWebhookId(ctx context.Context, db *sqlx.DB, webhookId string) (*model, error) {
var res model
query := `SELECT id, webhook_id, url, webhook_type, attempts, state, created_at, next_attempt_at FROM ` + tableName + `
WHERE webhook_id = $1
`
err := db.GetContext(ctx, &res, query, webhookId)
if err != nil {
return nil, pgutil.CheckNoRows(err, webhook.ErrNotFound)
}
return &res, nil
}
func dbCountByState(ctx context.Context, db *sqlx.DB, state webhook.State) (uint64, error) {
var res uint64
query := `SELECT COUNT(*) FROM ` + tableName + `
WHERE state = $1
`
err := db.GetContext(ctx, &res, query, state)
if err != nil {
return 0, err
}
return res, nil
}
func dbGetAllPendingReadyToSend(ctx context.Context, db *sqlx.DB, limit uint64) ([]*model, error) {
res := []*model{}
query := `SELECT id, webhook_id, url, webhook_type, attempts, state, created_at, next_attempt_at FROM ` + tableName + `
WHERE state = $1 AND next_attempt_at <= $2
LIMIT $3
`
err := db.SelectContext(
ctx,
&res,
query,
webhook.StatePending,
time.Now(),
limit,
)
if err != nil {
return nil, pgutil.CheckNoRows(err, webhook.ErrNotFound)
} else if len(res) == 0 {
return nil, webhook.ErrNotFound
}
return res, nil
}