-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsubscriptionskus.go
77 lines (62 loc) · 1.55 KB
/
subscriptionskus.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
package database
import (
"context"
_ "embed"
"errors"
"github.com/TicketsBot/common/model"
"github.com/google/uuid"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type SubscriptionSkus struct {
*pgxpool.Pool
}
var (
//go:embed sql/subscription_skus/schema.sql
subscriptionSkusSchema string
//go:embed sql/subscription_skus/get.sql
subscriptionSkusGet string
//go:embed sql/subscription_skus/search.sql
subscriptionSkusSearch string
)
func newSubscriptionSkusTable(db *pgxpool.Pool) *SubscriptionSkus {
return &SubscriptionSkus{
db,
}
}
func (SubscriptionSkus) Schema() string {
return subscriptionSkusSchema
}
func (e *SubscriptionSkus) GetSku(ctx context.Context, tx pgx.Tx, skuId uuid.UUID) (*model.SubscriptionSku, error) {
var sku model.SubscriptionSku
if err := tx.QueryRow(ctx, subscriptionSkusGet, skuId).Scan(
&sku.Id,
&sku.Label,
&sku.SkuType,
&sku.Tier,
&sku.Priority,
&sku.IsGlobal,
); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
return nil, err
}
return &sku, nil
}
func (e *SubscriptionSkus) Search(ctx context.Context, label string, limit int) ([]model.SubscriptionSku, error) {
rows, err := e.Query(ctx, subscriptionSkusSearch, label, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var skus []model.SubscriptionSku
for rows.Next() {
var sku model.SubscriptionSku
if err := rows.Scan(&sku.Id, &sku.Label, &sku.SkuType, &sku.Tier, &sku.Priority, &sku.IsGlobal); err != nil {
return nil, err
}
skus = append(skus, sku)
}
return skus, nil
}