-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpremiumguilds.go
More file actions
58 lines (48 loc) · 1.29 KB
/
premiumguilds.go
File metadata and controls
58 lines (48 loc) · 1.29 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
package database
import (
"context"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"time"
)
type PremiumGuilds struct {
*pgxpool.Pool
}
func newPremiumGuilds(db *pgxpool.Pool) *PremiumGuilds {
return &PremiumGuilds{
db,
}
}
func (p PremiumGuilds) Schema() string {
return `
CREATE TABLE IF NOT EXISTS premium_guilds(
"guild_id" int8 NOT NULL UNIQUE,
"expiry" timestamp NOT NULL,
PRIMARY KEY("guild_id")
);`
}
func (p *PremiumGuilds) IsPremium(ctx context.Context, guildId uint64) (bool, error) {
expiry, err := p.GetExpiry(ctx, guildId)
if err != nil {
return false, err
}
return expiry.After(time.Now()), nil
}
func (p *PremiumGuilds) GetExpiry(ctx context.Context, guildId uint64) (expiry time.Time, e error) {
if err := p.QueryRow(ctx, `SELECT "expiry" from premium_guilds WHERE "guild_id" = $1;`, guildId).Scan(&expiry); err != nil && err != pgx.ErrNoRows {
e = err
}
return
}
func (p *PremiumGuilds) Add(ctx context.Context, guildId uint64, interval time.Duration) (err error) {
query := `
INSERT INTO premium_guilds("guild_id", "expiry")
VALUES($1, NOW() + $2)
ON CONFLICT("guild_id")
DO UPDATE SET "expiry" = CASE WHEN premium_guilds.expiry < NOW()
THEN NOW() + $2
ELSE premium_guilds.expiry + $2
END;`
_, err = p.Exec(ctx, query, guildId, interval)
return
}