-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathemailverificationcodes.go
More file actions
79 lines (64 loc) · 2.14 KB
/
Copy pathemailverificationcodes.go
File metadata and controls
79 lines (64 loc) · 2.14 KB
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
package database
import (
"context"
_ "embed"
"errors"
"time"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type EmailVerificationCodes struct {
*pgxpool.Pool
}
type EmailVerificationCode struct {
UserId uint64 `json:"user_id"`
Code string `json:"code"`
ExpiresAt time.Time `json:"expires_at"`
CreatedAt time.Time `json:"created_at"`
FailedAttempts int `json:"failed_attempts"`
}
var (
//go:embed sql/email_verification_codes/schema.sql
emailVerificationCodesSchema string
//go:embed sql/email_verification_codes/upsert.sql
emailVerificationCodesUpsert string
//go:embed sql/email_verification_codes/get_by_user_id.sql
emailVerificationCodesGetByUserId string
//go:embed sql/email_verification_codes/delete.sql
emailVerificationCodesDelete string
//go:embed sql/email_verification_codes/increment_attempts.sql
emailVerificationCodesIncrementAttempts string
)
func newEmailVerificationCodes(db *pgxpool.Pool) *EmailVerificationCodes {
return &EmailVerificationCodes{db}
}
func (EmailVerificationCodes) Schema() string {
return emailVerificationCodesSchema
}
func (t *EmailVerificationCodes) Upsert(ctx context.Context, userId uint64, code string, expiresAt time.Time) error {
_, err := t.Exec(ctx, emailVerificationCodesUpsert, userId, code, expiresAt)
return err
}
func (t *EmailVerificationCodes) GetByUserId(ctx context.Context, userId uint64) (*EmailVerificationCode, error) {
var vc EmailVerificationCode
if err := t.QueryRow(ctx, emailVerificationCodesGetByUserId, userId).Scan(
&vc.UserId, &vc.Code, &vc.ExpiresAt, &vc.CreatedAt, &vc.FailedAttempts,
); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
return nil, err
}
return &vc, nil
}
func (t *EmailVerificationCodes) Delete(ctx context.Context, userId uint64) error {
_, err := t.Exec(ctx, emailVerificationCodesDelete, userId)
return err
}
func (t *EmailVerificationCodes) IncrementAttempts(ctx context.Context, userId uint64) (int, error) {
var count int
if err := t.QueryRow(ctx, emailVerificationCodesIncrementAttempts, userId).Scan(&count); err != nil {
return 0, err
}
return count, nil
}