-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkbsettings.go
More file actions
102 lines (90 loc) · 2.57 KB
/
Copy pathkbsettings.go
File metadata and controls
102 lines (90 loc) · 2.57 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
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
package database
import (
"context"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type KBSettings struct {
GuildId uint64 `json:"guild_id,string"`
PrimaryBg *int `json:"primary_bg"`
CardBg *int `json:"card_bg"`
TextColour *int `json:"text_colour"`
AccentColour *int `json:"accent_colour"`
LogoUrl *string `json:"logo_url"`
HideBranding bool `json:"hide_branding"`
}
type KBSettingsTable struct {
*pgxpool.Pool
}
func newKBSettings(db *pgxpool.Pool) *KBSettingsTable {
return &KBSettingsTable{
Pool: db,
}
}
// Schema no longer creates the custom domain columns. Databases that already ran
// an earlier version still have custom_domain, domain_verified and the
// domain_status family, plus their indexes; nothing reads or writes them.
//
// They are deliberately not dropped here. DROP COLUMN is irreversible and this
// file runs on every service boot, so removing them is a decision to take
// explicitly rather than a side effect of deploying.
func (t KBSettingsTable) Schema() string {
return `
CREATE TABLE IF NOT EXISTS kb_settings(
"guild_id" int8 NOT NULL PRIMARY KEY,
"primary_bg" int4 DEFAULT NULL,
"card_bg" int4 DEFAULT NULL,
"text_colour" int4 DEFAULT NULL,
"accent_colour" int4 DEFAULT NULL,
"logo_url" text DEFAULT NULL,
"hide_branding" bool NOT NULL DEFAULT false
);
`
}
func (t *KBSettingsTable) Get(ctx context.Context, guildId uint64) (KBSettings, bool, error) {
query := `
SELECT "guild_id", "primary_bg", "card_bg", "text_colour", "accent_colour", "logo_url", "hide_branding"
FROM kb_settings
WHERE "guild_id" = $1;
`
var settings KBSettings
err := t.QueryRow(ctx, query, guildId).Scan(
&settings.GuildId,
&settings.PrimaryBg,
&settings.CardBg,
&settings.TextColour,
&settings.AccentColour,
&settings.LogoUrl,
&settings.HideBranding,
)
if err != nil {
if err == pgx.ErrNoRows {
return KBSettings{}, false, nil
}
return KBSettings{}, false, err
}
return settings, true, nil
}
func (t *KBSettingsTable) Set(ctx context.Context, settings KBSettings) error {
query := `
INSERT INTO kb_settings("guild_id", "primary_bg", "card_bg", "text_colour", "accent_colour", "logo_url", "hide_branding")
VALUES($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT("guild_id") DO UPDATE SET
"primary_bg" = $2,
"card_bg" = $3,
"text_colour" = $4,
"accent_colour" = $5,
"logo_url" = $6,
"hide_branding" = $7;
`
_, err := t.Exec(ctx, query,
settings.GuildId,
settings.PrimaryBg,
settings.CardBg,
settings.TextColour,
settings.AccentColour,
settings.LogoUrl,
settings.HideBranding,
)
return err
}