-
Notifications
You must be signed in to change notification settings - Fork 4
/
legacypremiumentitlements.go
133 lines (108 loc) · 3.4 KB
/
legacypremiumentitlements.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
package database
import (
"context"
_ "embed"
"errors"
"github.com/google/uuid"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"time"
)
type LegacyPremiumEntitlement struct {
UserId uint64 `json:"user_id"`
TierId int32 `json:"tier_id"`
SkuLabel string `json:"sku_label"`
SkuId uuid.UUID `json:"sku_id"`
IsLegacy bool `json:"is_legacy"`
ExpiresAt time.Time `json:"expires_at"`
}
type LegacyPremiumEntitlements struct {
*pgxpool.Pool
}
func newLegacyPremiumEntitlement(db *pgxpool.Pool) *LegacyPremiumEntitlements {
return &LegacyPremiumEntitlements{
db,
}
}
var (
//go:embed sql/legacy_premium_entitlements/schema.sql
legacyPremiumEntitlementsSchema string
//go:embed sql/legacy_premium_entitlements/list_all.sql
legacyPremiumEntitlementsListAll string
//go:embed sql/legacy_premium_entitlements/get_guild_tier.sql
legacyPremiumEntitlementsGetGuildTier string
//go:embed sql/legacy_premium_entitlements/get_user_entitlement.sql
legacyPremiumEntitlementGetUserEntitlement string
//go:embed sql/legacy_premium_entitlements/set_entitlement.sql
legacyPremiumEntitlementsSet string
//go:embed sql/legacy_premium_entitlements/delete_entitlement.sql
legacyPremiumEntitlementsDelete string
)
func (e LegacyPremiumEntitlements) Schema() string {
return legacyPremiumEntitlementsSchema
}
func (e *LegacyPremiumEntitlements) ListAll(ctx context.Context, tx pgx.Tx) ([]LegacyPremiumEntitlement, error) {
rows, err := tx.Query(ctx, legacyPremiumEntitlementsListAll)
if err != nil {
return nil, err
}
defer rows.Close()
var entitlements []LegacyPremiumEntitlement
for rows.Next() {
var entitlement LegacyPremiumEntitlement
if err := rows.Scan(
&entitlement.UserId,
&entitlement.TierId,
&entitlement.SkuLabel,
&entitlement.SkuId,
&entitlement.IsLegacy,
&entitlement.ExpiresAt,
); err != nil {
return nil, err
}
entitlements = append(entitlements, entitlement)
}
return entitlements, nil
}
func (e *LegacyPremiumEntitlements) GetGuildTier(ctx context.Context, guildId, ownerId uint64, gracePeriod time.Duration) (int32, bool, error) {
var tier int32
if err := e.QueryRow(ctx, legacyPremiumEntitlementsGetGuildTier, guildId, ownerId, gracePeriod).Scan(&tier); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return -1, false, nil
}
return -1, false, err
}
return tier, true, nil
}
func (e *LegacyPremiumEntitlements) GetUserTier(ctx context.Context, userId uint64, gracePeriod time.Duration) (*LegacyPremiumEntitlement, error) {
var entitlement LegacyPremiumEntitlement
if err := e.QueryRow(ctx, legacyPremiumEntitlementGetUserEntitlement, userId, gracePeriod).Scan(
&entitlement.UserId,
&entitlement.TierId,
&entitlement.SkuLabel,
&entitlement.SkuId,
&entitlement.IsLegacy,
&entitlement.ExpiresAt,
); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
return nil, err
}
return &entitlement, nil
}
func (e *LegacyPremiumEntitlements) SetEntitlement(ctx context.Context, tx pgx.Tx, entitlement LegacyPremiumEntitlement) error {
_, err := tx.Exec(ctx, legacyPremiumEntitlementsSet,
entitlement.UserId,
entitlement.TierId,
entitlement.SkuLabel,
entitlement.SkuId,
entitlement.IsLegacy,
entitlement.ExpiresAt,
)
return err
}
func (e *LegacyPremiumEntitlements) Delete(ctx context.Context, tx pgx.Tx, userId uint64) error {
_, err := tx.Exec(ctx, legacyPremiumEntitlementsDelete, userId)
return err
}