-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathforminputoptions.go
More file actions
194 lines (162 loc) · 5.34 KB
/
forminputoptions.go
File metadata and controls
194 lines (162 loc) · 5.34 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package database
import (
"context"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type FormInputOption struct {
Id int `json:"id"`
FormInputId int `json:"form_input_id"`
Position int `json:"position"`
Label string `json:"label"`
Description *string `json:"description,omitempty"`
Value string `json:"value"`
}
type FormInputOptionTable struct {
*pgxpool.Pool
}
func newFormInputOptionTable(db *pgxpool.Pool) *FormInputOptionTable {
return &FormInputOptionTable{
db,
}
}
func (f FormInputOptionTable) Schema() string {
return `
CREATE TABLE IF NOT EXISTS form_input_option(
"id" SERIAL NOT NULL UNIQUE,
"form_input_id" int NOT NULL,
"position" int NOT NULL,
"label" VARCHAR(100) NOT NULL,
"description" VARCHAR(255) NULL,
"value" VARCHAR(100) NOT NULL,
FOREIGN KEY("form_input_id") REFERENCES form_input("id") ON DELETE CASCADE,
UNIQUE("form_input_id", "position") DEFERRABLE INITIALLY DEFERRED,
CHECK(position >= 1),
CHECK(position <= 25),
PRIMARY KEY("id")
);
CREATE INDEX IF NOT EXISTS form_input_option_form_input_id ON form_input_options("form_input_id");
`
}
func (f *FormInputOptionTable) GetOptions(ctx context.Context, formInputId int) (options []FormInputOption, e error) {
rows, err := f.Query(ctx, `SELECT id, form_input_id, position, label, description, value FROM form_input_option WHERE form_input_id=$1 ORDER BY position ASC`, formInputId)
if err != nil {
return options, err
}
defer rows.Close()
for rows.Next() {
var option FormInputOption
err := rows.Scan(&option.Id, &option.FormInputId, &option.Position, &option.Label, &option.Description, &option.Value)
if err != nil {
return options, err
}
options = append(options, option)
}
if rows.Err() != nil {
return options, rows.Err()
}
return options, nil
}
func (f *FormInputOptionTable) GetOptionsByForm(ctx context.Context, formId int) (options map[int][]FormInputOption, e error) {
query := `SELECT o.id, o.form_input_id, o.position, o.label, o.description, o.value
FROM form_input_option o
JOIN form_input i ON o.form_input_id = i.id
WHERE i.form_id = $1
ORDER BY o.form_input_id, o.position ASC;`
rows, err := f.Query(ctx, query, formId)
if err != nil {
return options, err
}
defer rows.Close()
options = make(map[int][]FormInputOption)
for rows.Next() {
var option FormInputOption
err := rows.Scan(&option.Id, &option.FormInputId, &option.Position, &option.Label, &option.Description, &option.Value)
if err != nil {
return options, err
}
options[option.FormInputId] = append(options[option.FormInputId], option)
}
if rows.Err() != nil {
return options, rows.Err()
}
return options, nil
}
func (f *FormInputOptionTable) GetAllOptionsByGuild(ctx context.Context, guildId uint64) (map[int][]FormInputOption, error) {
query := `SELECT o.id, o.form_input_id, o.position, o.label, o.description, o.value
FROM form_input_option o
JOIN form_input i ON o.form_input_id = i.id
JOIN forms f ON i.form_id = f.form_id
WHERE f.guild_id = $1
ORDER BY o.form_input_id, o.position ASC;`
rows, err := f.Query(ctx, query, guildId)
if err != nil {
return nil, err
}
defer rows.Close()
optionsMap := make(map[int][]FormInputOption)
for rows.Next() {
var option FormInputOption
err := rows.Scan(&option.Id, &option.FormInputId, &option.Position, &option.Label, &option.Description, &option.Value)
if err != nil {
return nil, err
}
optionsMap[option.FormInputId] = append(optionsMap[option.FormInputId], option)
}
if rows.Err() != nil {
return nil, rows.Err()
}
return optionsMap, nil
}
func (f *FormInputOptionTable) Create(ctx context.Context, formInputOption FormInputOption) (id int, e error) {
err := f.QueryRow(ctx, `INSERT INTO form_input_option (form_input_id, position, label, description, value) VALUES ($1, $2, $3, $4, $5) RETURNING id`,
formInputOption.FormInputId,
formInputOption.Position,
formInputOption.Label,
formInputOption.Description,
formInputOption.Value,
).Scan(&id)
if err != nil {
return 0, err
}
return id, nil
}
func (f *FormInputOptionTable) CreateTx(ctx context.Context, tx pgx.Tx, formInputOption FormInputOption) (id int, e error) {
err := tx.QueryRow(ctx, `INSERT INTO form_input_option (form_input_id, position, label, description, value) VALUES ($1, $2, $3, $4, $5) RETURNING id`,
formInputOption.FormInputId,
formInputOption.Position,
formInputOption.Label,
formInputOption.Description,
formInputOption.Value,
).Scan(&id)
if err != nil {
return 0, err
}
return id, nil
}
func (f *FormInputOptionTable) Delete(ctx context.Context, id int) (e error) {
q := `WITH deleted_positions AS (
DELETE FROM form_input_option
WHERE id = $1
RETURNING form_input_id, position
)
UPDATE form_input_option
SET position = position - 1
WHERE form_input_id = (SELECT form_input_id FROM deleted_positions)
AND position > (SELECT position FROM deleted_positions);`
_, err := f.Exec(ctx, q, id)
return err
}
func (f *FormInputOptionTable) DeleteTx(ctx context.Context, tx pgx.Tx, id int) (e error) {
q := `WITH deleted_positions AS (
DELETE FROM form_input_option
WHERE id = $1
RETURNING form_input_id, position
)
UPDATE form_input_option
SET position = position - 1
WHERE form_input_id = (SELECT form_input_id FROM deleted_positions)
AND position > (SELECT position FROM deleted_positions);`
_, err := tx.Exec(ctx, q, id)
return err
}