-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforminputapiconfig.go
More file actions
277 lines (236 loc) · 7.8 KB
/
Copy pathforminputapiconfig.go
File metadata and controls
277 lines (236 loc) · 7.8 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package database
import (
"context"
"errors"
"time"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type FormInputApiConfig struct {
Id int `json:"id"`
FormInputId int `json:"form_input_id"`
EndpointUrl string `json:"endpoint_url"`
Method string `json:"method"`
CacheDurationSeconds *int `json:"cache_duration_seconds,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type FormInputApiConfigTable struct {
*pgxpool.Pool
}
func newFormInputApiConfigTable(db *pgxpool.Pool) *FormInputApiConfigTable {
return &FormInputApiConfigTable{
db,
}
}
func (f FormInputApiConfigTable) Schema() string {
return `
CREATE TABLE IF NOT EXISTS form_input_api_config(
"id" SERIAL NOT NULL UNIQUE,
"form_input_id" INT NOT NULL UNIQUE,
"endpoint_url" VARCHAR(500) NOT NULL,
"method" VARCHAR(10) NOT NULL DEFAULT 'GET',
"cache_duration_seconds" INT DEFAULT 300,
"created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY("form_input_id") REFERENCES form_input("id") ON DELETE CASCADE,
CHECK(method IN ('GET', 'POST', 'PUT', 'PATCH', 'DELETE')),
CHECK(cache_duration_seconds >= 0),
PRIMARY KEY("id")
);
CREATE INDEX IF NOT EXISTS form_input_api_config_form_input_id ON form_input_api_config("form_input_id");
`
}
func (f *FormInputApiConfigTable) Get(ctx context.Context, formInputId int) (config FormInputApiConfig, ok bool, e error) {
query := `
SELECT "id", "form_input_id", "endpoint_url", "method", "cache_duration_seconds", "created_at", "updated_at"
FROM form_input_api_config
WHERE "form_input_id" = $1;`
err := f.QueryRow(ctx, query, formInputId).Scan(
&config.Id,
&config.FormInputId,
&config.EndpointUrl,
&config.Method,
&config.CacheDurationSeconds,
&config.CreatedAt,
&config.UpdatedAt,
)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return FormInputApiConfig{}, false, nil
} else {
return FormInputApiConfig{}, false, err
}
}
return config, true, nil
}
func (f *FormInputApiConfigTable) GetById(ctx context.Context, id int) (config FormInputApiConfig, ok bool, e error) {
query := `
SELECT "id", "form_input_id", "endpoint_url", "method", "cache_duration_seconds", "created_at", "updated_at"
FROM form_input_api_config
WHERE "id" = $1;`
err := f.QueryRow(ctx, query, id).Scan(
&config.Id,
&config.FormInputId,
&config.EndpointUrl,
&config.Method,
&config.CacheDurationSeconds,
&config.CreatedAt,
&config.UpdatedAt,
)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return FormInputApiConfig{}, false, nil
} else {
return FormInputApiConfig{}, false, err
}
}
return config, true, nil
}
func (f *FormInputApiConfigTable) GetByFormId(ctx context.Context, formId int) ([]FormInputApiConfig, error) {
query := `
SELECT c."id", c."form_input_id", c."endpoint_url", c."method", c."cache_duration_seconds", c."created_at", c."updated_at"
FROM form_input_api_config c
INNER JOIN form_input i ON c."form_input_id" = i."id"
WHERE i."form_id" = $1
ORDER BY i."position" ASC;`
rows, err := f.Query(ctx, query, formId)
if err != nil {
return nil, err
}
defer rows.Close()
var configs []FormInputApiConfig
for rows.Next() {
var config FormInputApiConfig
if err := rows.Scan(
&config.Id,
&config.FormInputId,
&config.EndpointUrl,
&config.Method,
&config.CacheDurationSeconds,
&config.CreatedAt,
&config.UpdatedAt,
); err != nil {
return nil, err
}
configs = append(configs, config)
}
return configs, rows.Err()
}
func (f *FormInputApiConfigTable) GetByFormInputId(ctx context.Context, formInputId int) (config FormInputApiConfig, ok bool, e error) {
query := `
SELECT "id", "form_input_id", "endpoint_url", "method", "cache_duration_seconds", "created_at", "updated_at"
FROM form_input_api_config
WHERE "form_input_id" = $1;`
err := f.QueryRow(ctx, query, formInputId).Scan(
&config.Id,
&config.FormInputId,
&config.EndpointUrl,
&config.Method,
&config.CacheDurationSeconds,
&config.CreatedAt,
&config.UpdatedAt,
)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return FormInputApiConfig{}, false, nil
} else {
return FormInputApiConfig{}, false, err
}
}
return config, true, nil
}
func (f *FormInputApiConfigTable) GetAllByGuild(ctx context.Context, guildId uint64) (map[int]FormInputApiConfig, error) {
query := `
SELECT c."id", c."form_input_id", c."endpoint_url", c."method", c."cache_duration_seconds", c."created_at", c."updated_at"
FROM form_input_api_config c
INNER JOIN form_input i ON c."form_input_id" = i."id"
INNER JOIN forms f ON i."form_id" = f."form_id"
WHERE f."guild_id" = $1;`
rows, err := f.Query(ctx, query, guildId)
if err != nil {
return nil, err
}
defer rows.Close()
configs := make(map[int]FormInputApiConfig)
for rows.Next() {
var config FormInputApiConfig
if err := rows.Scan(
&config.Id,
&config.FormInputId,
&config.EndpointUrl,
&config.Method,
&config.CacheDurationSeconds,
&config.CreatedAt,
&config.UpdatedAt,
); err != nil {
return nil, err
}
configs[config.FormInputId] = config
}
return configs, rows.Err()
}
func (f *FormInputApiConfigTable) Create(ctx context.Context, formInputId int, endpointUrl string, method string, cacheDurationSeconds *int) (int, error) {
query := `
INSERT INTO form_input_api_config("form_input_id", "endpoint_url", "method", "cache_duration_seconds")
VALUES($1, $2, $3, $4)
RETURNING "id";`
var id int
if err := f.QueryRow(ctx, query, formInputId, endpointUrl, method, cacheDurationSeconds).Scan(&id); err != nil {
return 0, err
}
return id, nil
}
func (f *FormInputApiConfigTable) CreateTx(ctx context.Context, tx pgx.Tx, formInputId int, endpointUrl string, method string, cacheDurationSeconds *int) (int, error) {
query := `
INSERT INTO form_input_api_config("form_input_id", "endpoint_url", "method", "cache_duration_seconds")
VALUES($1, $2, $3, $4)
RETURNING "id";`
var id int
if err := tx.QueryRow(ctx, query, formInputId, endpointUrl, method, cacheDurationSeconds).Scan(&id); err != nil {
return 0, err
}
return id, nil
}
func (f *FormInputApiConfigTable) Update(ctx context.Context, id int, endpointUrl string, method string, cacheDurationSeconds *int) error {
query := `
UPDATE form_input_api_config
SET "endpoint_url" = $2,
"method" = $3,
"cache_duration_seconds" = $4,
"updated_at" = CURRENT_TIMESTAMP
WHERE "id" = $1;`
_, err := f.Exec(ctx, query, id, endpointUrl, method, cacheDurationSeconds)
return err
}
func (f *FormInputApiConfigTable) UpdateTx(ctx context.Context, tx pgx.Tx, id int, endpointUrl string, method string, cacheDurationSeconds *int) error {
query := `
UPDATE form_input_api_config
SET "endpoint_url" = $2,
"method" = $3,
"cache_duration_seconds" = $4,
"updated_at" = CURRENT_TIMESTAMP
WHERE "id" = $1;`
_, err := tx.Exec(ctx, query, id, endpointUrl, method, cacheDurationSeconds)
return err
}
func (f *FormInputApiConfigTable) Delete(ctx context.Context, id int) error {
query := `DELETE FROM form_input_api_config WHERE "id" = $1;`
_, err := f.Exec(ctx, query, id)
return err
}
func (f *FormInputApiConfigTable) DeleteTx(ctx context.Context, tx pgx.Tx, id int) error {
query := `DELETE FROM form_input_api_config WHERE "id" = $1;`
_, err := tx.Exec(ctx, query, id)
return err
}
func (f *FormInputApiConfigTable) DeleteByFormInput(ctx context.Context, formInputId int) error {
query := `DELETE FROM form_input_api_config WHERE "form_input_id" = $1;`
_, err := f.Exec(ctx, query, formInputId)
return err
}
func (f *FormInputApiConfigTable) DeleteByFormInputTx(ctx context.Context, tx pgx.Tx, formInputId int) error {
query := `DELETE FROM form_input_api_config WHERE "form_input_id" = $1;`
_, err := tx.Exec(ctx, query, formInputId)
return err
}