oups
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
type BlacklistedUser struct {
|
||||
ID int64
|
||||
DiscordID string
|
||||
Reason sql.NullString
|
||||
BlacklistedBy string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type UserBlacklistRepo struct{ db *sql.DB }
|
||||
|
||||
func NewUserBlacklistRepo(db *sql.DB) *UserBlacklistRepo { return &UserBlacklistRepo{db: db} }
|
||||
|
||||
func (r *UserBlacklistRepo) Add(ctx context.Context, discordID, reason, blacklistedBy string) error {
|
||||
_, err := r.db.ExecContext(ctx,
|
||||
`INSERT OR REPLACE INTO user_blacklist(discord_id,reason,blacklisted_by,created_at) VALUES(?,?,?,?)`,
|
||||
discordID, NullStr(reason), blacklistedBy, time.Now().UTC())
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *UserBlacklistRepo) Remove(ctx context.Context, discordID string) error {
|
||||
_, err := r.db.ExecContext(ctx, `DELETE FROM user_blacklist WHERE discord_id=?`, discordID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *UserBlacklistRepo) IsBlacklisted(ctx context.Context, discordID string) (bool, error) {
|
||||
var count int
|
||||
err := r.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM user_blacklist WHERE discord_id=?`, discordID).Scan(&count)
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
func (r *UserBlacklistRepo) List(ctx context.Context) ([]*BlacklistedUser, error) {
|
||||
rows, err := r.db.QueryContext(ctx,
|
||||
`SELECT id,discord_id,reason,blacklisted_by,created_at FROM user_blacklist ORDER BY created_at DESC`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []*BlacklistedUser
|
||||
for rows.Next() {
|
||||
var u BlacklistedUser
|
||||
if err := rows.Scan(&u.ID, &u.DiscordID, &u.Reason, &u.BlacklistedBy, &u.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, &u)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user