-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathticketmembers.go
65 lines (54 loc) · 1.71 KB
/
ticketmembers.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
package database
import (
"context"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type TicketMembers struct {
*pgxpool.Pool
}
func newTicketMembers(db *pgxpool.Pool) *TicketMembers {
return &TicketMembers{
db,
}
}
func (m TicketMembers) Schema() string {
return `
CREATE TABLE IF NOT EXISTS ticket_members(
"guild_id" int8 NOT NULL,
"ticket_id" int4 NOT NULL,
"user_id" int8 NOT NULL,
FOREIGN KEY("guild_id", "ticket_id") REFERENCES tickets("guild_id", "id"),
PRIMARY KEY("guild_id", "ticket_id", "user_id")
);
CREATE INDEX IF NOT EXISTS ticket_members_guild_ticket ON ticket_members("guild_id", "ticket_id");
`
}
func (m *TicketMembers) Get(ctx context.Context, guildId uint64, ticketId int) (members []uint64, e error) {
query := `SELECT "user_id" FROM ticket_members WHERE "guild_id" = $1 AND "ticket_id" = $2;`
rows, err := m.Query(ctx, query, guildId, ticketId)
defer rows.Close()
if err != nil && err != pgx.ErrNoRows {
e = err
return
}
for rows.Next() {
var userId uint64
if err := rows.Scan(&userId); err != nil {
e = err
continue
}
members = append(members, userId)
}
return
}
func (m *TicketMembers) Add(ctx context.Context, guildId uint64, ticketId int, userId uint64) (err error) {
query := `INSERT INTO ticket_members("guild_id", "ticket_id", "user_id") VALUES($1, $2, $3) ON CONFLICT("guild_id", "ticket_id", "user_id") DO NOTHING;`
_, err = m.Exec(ctx, query, guildId, ticketId, userId)
return
}
func (m *TicketMembers) Delete(ctx context.Context, guildId uint64, ticketId int, userId uint64) (err error) {
query := `DELETE FROM ticket_members WHERE "guild_id"=$1 AND "ticket_id"=$2 AND "user_id"=$3;`
_, err = m.Exec(ctx, query, guildId, ticketId, userId)
return
}