-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbotstaff.go
More file actions
80 lines (64 loc) · 1.34 KB
/
Copy pathbotstaff.go
File metadata and controls
80 lines (64 loc) · 1.34 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
76
77
78
79
80
package database
import (
"context"
"github.com/jackc/pgx/v4/pgxpool"
)
type BotStaff struct {
*pgxpool.Pool
}
func newBotStaff(db *pgxpool.Pool) *BotStaff {
return &BotStaff{
db,
}
}
func (s BotStaff) Schema() string {
return `
CREATE TABLE IF NOT EXISTS bot_staff(
"user_id" int8 NOT NULL UNIQUE,
PRIMARY KEY("user_id")
);`
}
func (s *BotStaff) IsStaff(ctx context.Context, userId uint64) (isStaff bool, err error) {
query := `
SELECT EXISTS (
SELECT 1
FROM bot_staff
where "user_id" = $1
);
`
err = s.QueryRow(ctx, query, userId).Scan(&isStaff)
return
}
func (s *BotStaff) GetAll(ctx context.Context) ([]uint64, error) {
query := `SELECT "user_id" FROM bot_staff;`
rows, err := s.Query(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var userIds []uint64
for rows.Next() {
var userId uint64
if err = rows.Scan(&userId); err != nil {
return nil, err
}
userIds = append(userIds, userId)
}
return userIds, nil
}
func (s *BotStaff) Add(ctx context.Context, userId uint64) (err error) {
query := `
INSERT INTO bot_staff("user_id")
VALUES($1)
ON CONFLICT("user_id") DO NOTHING;
`
_, err = s.Exec(ctx, query, userId)
return
}
func (s *BotStaff) Delete(ctx context.Context, userId uint64) (err error) {
query := `
DELETE FROM bot_staff
WHERE "user_id" = $1;`
_, err = s.Exec(ctx, query, userId)
return
}