-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharchivechannel.go
More file actions
75 lines (60 loc) · 1.83 KB
/
archivechannel.go
File metadata and controls
75 lines (60 loc) · 1.83 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
package database
import (
"context"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type ArchiveChannel struct {
*pgxpool.Pool
}
func newArchiveChannel(db *pgxpool.Pool) *ArchiveChannel {
return &ArchiveChannel{
db,
}
}
func (c ArchiveChannel) Schema() string {
return `
CREATE TABLE IF NOT EXISTS archive_channel(
"guild_id" int8 NOT NULL UNIQUE,
"channel_id" int8,
PRIMARY KEY("guild_id")
);`
}
func (c *ArchiveChannel) Get(ctx context.Context, guildId uint64) (archiveChannel *uint64, e error) {
query := `SELECT "channel_id" from archive_channel WHERE "guild_id" = $1;`
if err := c.QueryRow(ctx, query, guildId).Scan(&archiveChannel); err != nil && err != pgx.ErrNoRows {
e = err
}
return
}
func (c *ArchiveChannel) GetByPanel(ctx context.Context, guildId uint64, panelId int) (archiveChannel *uint64, e error) {
query := `
SELECT
COALESCE(p.transcript_channel_id, ac.channel_id)
FROM
panels p
JOIN archive_channel ac ON ac.guild_id = p.guild_id
WHERE p.panel_id = $1 AND p.guild_id = $2;
`
if err := c.QueryRow(ctx, query, panelId, guildId).Scan(&archiveChannel); err != nil && err != pgx.ErrNoRows {
e = err
}
return
}
func (c *ArchiveChannel) Set(ctx context.Context, guildId uint64, archiveChannel *uint64) (err error) {
query := `
INSERT INTO archive_channel("guild_id", "channel_id")
VALUES($1, $2)
ON CONFLICT("guild_id") DO UPDATE SET "channel_id" = $2;
`
_, err = c.Exec(ctx, query, guildId, archiveChannel)
return
}
func (c *ArchiveChannel) DeleteByGuild(ctx context.Context, guildId uint64) (err error) {
_, err = c.Exec(ctx, `DELETE FROM archive_channel WHERE "guild_id" = $1;`, guildId)
return
}
func (c *ArchiveChannel) DeleteByChannel(ctx context.Context, channelId uint64) (err error) {
_, err = c.Exec(ctx, `DELETE FROM archive_channel WHERE "channel_id" = $1;`, channelId)
return
}