-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathembedfields.go
133 lines (114 loc) · 2.61 KB
/
embedfields.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
package database
import (
"context"
"github.com/jackc/pgx/v4/pgxpool"
)
type EmbedField struct {
FieldId int `json:"-"`
EmbedId int `json:"-"`
Name string `json:"name"`
Value string `json:"value"`
Inline bool `json:"inline"`
}
type EmbedFieldsTable struct {
*pgxpool.Pool
}
func newEmbedFieldsTable(db *pgxpool.Pool) *EmbedFieldsTable {
return &EmbedFieldsTable{
db,
}
}
func (s EmbedFieldsTable) Schema() string {
return `
CREATE TABLE IF NOT EXISTS embed_fields(
"id" SERIAL NOT NULL UNIQUE,
"embed_id" int NOT NULL,
"name" VARCHAR(255) NOT NULL,
"value" TEXT NOT NULL CONSTRAINT value_length CHECK (length(value) <= 1024),
"inline" BOOL NOT NULL,
FOREIGN KEY("embed_id") REFERENCES embeds("id") ON DELETE CASCADE,
PRIMARY KEY("id")
);
`
}
func (s *EmbedFieldsTable) GetField(ctx context.Context, id int) (field EmbedField, err error) {
query := `
SELECT
"id",
"embed_id",
"name",
"value",
"inline"
FROM embed_fields
WHERE "id" = $1;
`
err = s.QueryRow(ctx, query, id).Scan(
&field.FieldId,
&field.EmbedId,
&field.Name,
&field.Value,
&field.Inline,
)
return
}
func (s *EmbedFieldsTable) GetFieldsForEmbed(ctx context.Context, embedId int) ([]EmbedField, error) {
query := `
SELECT
"id",
"embed_id",
"name",
"value",
"inline"
FROM embed_fields
WHERE "embed_id" = $1;
`
rows, err := s.Query(ctx, query, embedId)
if err != nil {
return nil, err
}
var fields []EmbedField
for rows.Next() {
var field EmbedField
if err := rows.Scan(&field.FieldId, &field.EmbedId, &field.Name, &field.Value, &field.Inline); err != nil {
return nil, err
}
fields = append(fields, field)
}
return fields, nil
}
// GetAllFieldsForPanels Returns a map of [embed_id][]EmbedField
func (s *EmbedFieldsTable) GetAllFieldsForPanels(ctx context.Context, guildId uint64) (map[int][]EmbedField, error) {
query := `
SELECT
embed_fields.id,
embed_fields.embed_id,
embed_fields.name,
embed_fields.value,
embed_fields.inline
FROM embed_fields
INNER JOIN embeds
ON embeds.id = embed_fields.embed_id
INNER JOIN panels
ON panels.welcome_message = embeds.id
WHERE embeds.guild_id = $1
ORDER BY embed_fields.embed_id, embed_fields.id;
`
rows, err := s.Query(ctx, query, guildId)
if err != nil {
return nil, err
}
fields := make(map[int][]EmbedField)
for rows.Next() {
var field EmbedField
if err := rows.Scan(&field.FieldId, &field.EmbedId, &field.Name, &field.Value, &field.Inline); err != nil {
return nil, err
}
slice, ok := fields[field.EmbedId]
if !ok {
slice = make([]EmbedField, 0)
}
slice = append(slice, field)
fields[field.EmbedId] = slice
}
return fields, nil
}