-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvotecredits.go
60 lines (47 loc) · 1.17 KB
/
votecredits.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
package database
import (
"context"
_ "embed"
"errors"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type VoteCredits struct {
*pgxpool.Pool
}
var (
//go:embed sql/vote_credits/schema.sql
voteCreditsSchema string
//go:embed sql/vote_credits/get.sql
voteCreditsGet string
//go:embed sql/vote_credits/increment.sql
voteCreditsIncrement string
//go:embed sql/vote_credits/delete.sql
voteCreditsDelete string
)
func newVoteCreditsTable(db *pgxpool.Pool) *VoteCredits {
return &VoteCredits{
db,
}
}
func (VoteCredits) Schema() string {
return voteCreditsSchema
}
func (v *VoteCredits) Get(ctx context.Context, tx pgx.Tx, userId uint64) (int, error) {
var credits int
if err := tx.QueryRow(ctx, voteCreditsGet, userId).Scan(&credits); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return 0, nil
}
return 0, err
}
return credits, nil
}
func (v *VoteCredits) Increment(ctx context.Context, tx pgx.Tx, userId uint64) error {
_, err := tx.Exec(ctx, voteCreditsIncrement, userId)
return err
}
func (v *VoteCredits) Delete(ctx context.Context, tx pgx.Tx, userId uint64) error {
_, err := tx.Exec(ctx, voteCreditsDelete, userId)
return err
}