-
Notifications
You must be signed in to change notification settings - Fork 4
/
webhooks.go
55 lines (46 loc) · 1.58 KB
/
webhooks.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
package database
import (
"context"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type Webhook struct {
Id uint64
Token string
}
type WebhookTable struct {
*pgxpool.Pool
}
func newWebhookTable(db *pgxpool.Pool) *WebhookTable {
return &WebhookTable{
db,
}
}
func (w WebhookTable) Schema() string {
return `
CREATE TABLE IF NOT EXISTS webhooks(
"guild_id" int8 NOT NULL,
"ticket_id" int4 NOT NULL,
"webhook_id" int8 NOT NULL UNIQUE,
"webhook_token" varchar(100) NOT NULL,
FOREIGN KEY("guild_id", "ticket_id") REFERENCES tickets("guild_id", "id"),
PRIMARY KEY("guild_id", "ticket_id")
);`
}
func (w *WebhookTable) Get(ctx context.Context, guildId uint64, ticketId int) (webhook Webhook, e error) {
query := `SELECT "webhook_id", "webhook_token" from webhooks WHERE "guild_id"=$1 AND "ticket_id"=$2;`
if err := w.QueryRow(ctx, query, guildId, ticketId).Scan(&webhook.Id, &webhook.Token); err != nil && err != pgx.ErrNoRows {
e = err
}
return
}
func (w *WebhookTable) Create(ctx context.Context, guildId uint64, ticketId int, webhook Webhook) (err error) {
query := `INSERT INTO webhooks("guild_id", "ticket_id", "webhook_id", "webhook_token") VALUES($1, $2, $3, $4) ON CONFLICT("guild_id", "ticket_id") DO UPDATE SET "webhook_id" = $3, "webhook_token" = $4;`
_, err = w.Exec(ctx, query, guildId, ticketId, webhook.Id, webhook.Token)
return
}
func (w *WebhookTable) Delete(ctx context.Context, guildId uint64, ticketId int) (err error) {
query := `DELETE FROM webhooks WHERE "guild_id"=$1 AND "ticket_id"=$2;`
_, err = w.Exec(ctx, query, guildId, ticketId)
return
}