-
Notifications
You must be signed in to change notification settings - Fork 4
/
autocloseexclude.go
71 lines (59 loc) · 1.46 KB
/
autocloseexclude.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
package database
import (
"context"
"github.com/jackc/pgx/v4/pgxpool"
)
type AutoCloseExclude struct {
*pgxpool.Pool
}
func newAutoCloseExclude(db *pgxpool.Pool) *AutoCloseExclude {
return &AutoCloseExclude{
db,
}
}
func (a AutoCloseExclude) Schema() string {
return `
CREATE TABLE IF NOT EXISTS auto_close_exclude(
"guild_id" int8 NOT NULL,
"ticket_id" int4 NOT NULL,
FOREIGN KEY("guild_id", "ticket_id") REFERENCES tickets("guild_id", "id"),
PRIMARY KEY("guild_id", "ticket_id")
);
`
}
func (a *AutoCloseExclude) IsExcluded(ctx context.Context, guildId uint64, ticketId int) (excluded bool, e error) {
query := `
SELECT COUNT(*)
FROM auto_close_exclude
WHERE "guild_id" = $1 AND "ticket_id" = $2
;
`
var count int
if err := a.QueryRow(ctx, query, guildId, ticketId).Scan(&count); err != nil {
e = err
}
excluded = count > 0
return
}
func (a *AutoCloseExclude) Exclude(ctx context.Context, guildId uint64, ticketId int) (err error) {
query := `
INSERT INTO auto_close_exclude("guild_id", "ticket_id")
VALUES ($1, $2)
ON CONFLICT("guild_id", "ticket_id") DO NOTHING
;
`
_, err = a.Exec(ctx, query, guildId, ticketId)
return
}
func (a *AutoCloseExclude) ExcludeAll(ctx context.Context, guildId uint64) (err error) {
query := `
INSERT INTO auto_close_exclude("guild_id", "ticket_id")
SELECT "guild_id", "id"
FROM tickets
WHERE "guild_id" = $1 AND "open" = 't'
ON CONFLICT("guild_id", "ticket_id") DO NOTHING
;
`
_, err = a.Exec(ctx, query, guildId)
return
}