-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathglobalblacklist.go
More file actions
66 lines (53 loc) · 1.35 KB
/
Copy pathglobalblacklist.go
File metadata and controls
66 lines (53 loc) · 1.35 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
package database
import (
"context"
"github.com/jackc/pgx/v4/pgxpool"
)
type GlobalBlacklist struct {
*pgxpool.Pool
}
func newGlobalBlacklist(db *pgxpool.Pool) *GlobalBlacklist {
return &GlobalBlacklist{
db,
}
}
func (b GlobalBlacklist) Schema() string {
return `
CREATE TABLE IF NOT EXISTS global_blacklist(
"user_id" int8 NOT NULL UNIQUE,
PRIMARY KEY("user_id")
);
`
}
func (b *GlobalBlacklist) IsBlacklisted(ctx context.Context, userId uint64) (blacklisted bool, err error) {
query := `
SELECT EXISTS(
SELECT 1 FROM global_blacklist WHERE "user_id" = $1
);
`
err = b.QueryRow(ctx, query, userId).Scan(&blacklisted)
return
}
func (b *GlobalBlacklist) ListAll(ctx context.Context) (users []uint64, err error) {
query := `SELECT "user_id" FROM global_blacklist;`
rows, err := b.Query(ctx, query)
if err != nil {
return
}
for rows.Next() {
var userId uint64
if err = rows.Scan(&userId); err != nil {
return
}
users = append(users, userId)
}
return
}
func (b *GlobalBlacklist) Add(ctx context.Context, userId uint64) (err error) {
_, err = b.Exec(ctx, `INSERT INTO global_blacklist("user_id") VALUES($1) ON CONFLICT("user_id") DO NOTHING;`, userId)
return
}
func (b *GlobalBlacklist) Delete(ctx context.Context, userId uint64) (err error) {
_, err = b.Exec(ctx, `DELETE FROM global_blacklist WHERE "user_id" = $1;`, userId)
return
}