-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.go
71 lines (64 loc) · 1.72 KB
/
wallet.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
package models
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"time"
)
type Wallet struct {
GuildID int64
UserID int64
Tukens int64
TimeLastMined time.Time
}
func WalletByGuildUser(ctx context.Context, db *pgxpool.Conn, gid int64, uid int64) (w Wallet, err error) {
var rows pgx.Rows
rows, err = db.Query(
ctx,
"SELECT * FROM wallet WHERE guild_id = $1 AND user_id = $2",
gid, uid)
if err == nil {
w, err = pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Wallet])
}
return
}
func WalletsByGuild(ctx context.Context, db *pgxpool.Conn, gid int64) (ws []Wallet, err error) {
var rows pgx.Rows
rows, _ = db.Query(
ctx,
"SELECT * FROM wallet WHERE guild_id = $1",
gid)
ws, err = pgx.CollectRows(rows, pgx.RowToStructByName[Wallet])
return
}
func (w *Wallet) Insert(ctx context.Context, db *pgxpool.Conn) (err error) {
_, err = db.Exec(
ctx,
"INSERT INTO wallet(guild_id, user_id, tukens, time_last_mined) "+
"VALUES($1, $2, $3, $4)",
w.GuildID, w.UserID, w.Tukens, w.TimeLastMined)
return
}
func (w *Wallet) UpdateTukens(ctx context.Context, db *pgxpool.Conn, tukens int64) (err error) {
_, err = db.Exec(
ctx,
"UPDATE wallet SET tukens = $3 "+
"WHERE guild_id = $1 AND user_id = $2",
w.GuildID, w.UserID, tukens)
if err == nil {
w.Tukens = tukens
}
return
}
func (w *Wallet) UpdateTukensMine(ctx context.Context, db *pgxpool.Conn, tukens int64, timeLastMined time.Time) (err error) {
_, err = db.Exec(
ctx,
"UPDATE wallet SET tukens = $3, time_last_mined = $4 "+
"WHERE guild_id = $1 AND user_id = $2",
w.GuildID, w.UserID, tukens, timeLastMined)
if err == nil {
w.Tukens = tukens
w.TimeLastMined = timeLastMined
}
return
}