-
Notifications
You must be signed in to change notification settings - Fork 4
/
multipaneltargets.go
167 lines (145 loc) · 3.48 KB
/
multipaneltargets.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package database
import (
"context"
"github.com/jackc/pgx/v4/pgxpool"
)
type MultiPanelTargets struct {
*pgxpool.Pool
}
func newMultiPanelTargets(db *pgxpool.Pool) *MultiPanelTargets {
return &MultiPanelTargets{
db,
}
}
func (p MultiPanelTargets) Schema() string {
return `
CREATE TABLE IF NOT EXISTS multi_panel_targets(
"multi_panel_id" int4 NOT NULL,
"panel_id" int NOT NULL,
FOREIGN KEY("multi_panel_id") REFERENCES multi_panels("id") ON DELETE CASCADE,
FOREIGN KEY ("panel_id") REFERENCES panels("panel_id") ON DELETE CASCADE,
PRIMARY KEY("multi_panel_id", "panel_id")
);
CREATE INDEX IF NOT EXISTS multi_panel_targets_multi_panel_id ON multi_panel_targets("multi_panel_id");
`
}
func (p *MultiPanelTargets) GetPanels(ctx context.Context, multiPanelId int) (panels []Panel, e error) {
query := `
SELECT
panels.panel_id,
panels.message_id,
panels.channel_id,
panels.guild_id,
panels.title,
panels.content,
panels.colour,
panels.target_category,
panels.emoji_name,
panels.emoji_id,
panels.welcome_message,
panels.default_team,
panels.custom_id,
panels.image_url,
panels.thumbnail_url,
panels.button_style,
panels.button_label,
panels.form_id,
panels.naming_scheme,
panels.force_disabled,
panels.disabled,
panels.exit_survey_form_id
FROM multi_panel_targets
INNER JOIN panels
ON panels.panel_id = multi_panel_targets.panel_id
WHERE "multi_panel_id" = $1;`
rows, err := p.Query(ctx, query, multiPanelId)
defer rows.Close()
if err != nil {
e = err
return
}
for rows.Next() {
var panel Panel
if err := rows.Scan(panel.fieldPtrs()...); err != nil {
return nil, err
}
panels = append(panels, panel)
}
return
}
func (p *MultiPanelTargets) GetMultiPanels(ctx context.Context, panelId int) ([]MultiPanel, error) {
query := `
SELECT
multi_panels.id,
multi_panels.message_id,
multi_panels.channel_id,
multi_panels.guild_id,
multi_panels.select_menu,
multi_panels.select_menu_placeholder,
multi_panels.embed
FROM multi_panel_targets
INNER JOIN multi_panels
ON multi_panels.id = multi_panel_targets.multi_panel_id
WHERE multi_panel_targets.panel_id = $1;
`
rows, err := p.Query(ctx, query, panelId)
defer rows.Close()
if err != nil {
return nil, err
}
var multiPanels []MultiPanel
for rows.Next() {
var multiPanel MultiPanel
var embedRaw *string
err := rows.Scan(
&multiPanel.Id,
&multiPanel.MessageId,
&multiPanel.ChannelId,
&multiPanel.GuildId,
&multiPanel.SelectMenu,
&multiPanel.SelectMenuPlaceholder,
&embedRaw,
)
if err != nil {
return nil, err
}
if embedRaw != nil {
if err := json.Unmarshal([]byte(*embedRaw), &multiPanel.Embed); err != nil {
return nil, err
}
}
multiPanels = append(multiPanels, multiPanel)
}
return multiPanels, nil
}
func (p *MultiPanelTargets) Insert(ctx context.Context, multiPanelId, panelId int) (err error) {
query := `
INSERT INTO multi_panel_targets("multi_panel_id", "panel_id")
VALUES ($1, $2)
ON CONFLICT("multi_panel_id", "panel_id") DO NOTHING;
`
_, err = p.Exec(ctx, query, multiPanelId, panelId)
return
}
func (p *MultiPanelTargets) DeleteAll(ctx context.Context, multiPanelId int) (err error) {
query := `
DELETE FROM
multi_panel_targets
WHERE
"multi_panel_id"=$1
;`
_, err = p.Exec(ctx, query, multiPanelId)
return
}
func (p *MultiPanelTargets) Delete(ctx context.Context, multiPanelId, panelId int) (err error) {
query := `
DELETE FROM
multi_panel_targets
WHERE
"multi_panel_id"=$1
AND
"panel_id" = $2
;`
_, err = p.Exec(ctx, query, multiPanelId, panelId)
return
}