-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnotificationpreferences.go
More file actions
80 lines (66 loc) · 2.21 KB
/
Copy pathnotificationpreferences.go
File metadata and controls
80 lines (66 loc) · 2.21 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"
_ "embed"
"errors"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type NotificationPreferencesTable struct {
*pgxpool.Pool
}
type NotificationPreference struct {
UserId uint64 `json:"user_id"`
Category string `json:"category"`
DiscordDm bool `json:"discord_dm"`
Email bool `json:"email"`
InApp bool `json:"in_app"`
}
var (
//go:embed sql/notification_preferences/schema.sql
notificationPreferencesSchema string
//go:embed sql/notification_preferences/get_by_user_id.sql
notificationPreferencesGetByUserId string
//go:embed sql/notification_preferences/get_by_user_id_and_category.sql
notificationPreferencesGetByUserIdAndCategory string
//go:embed sql/notification_preferences/upsert.sql
notificationPreferencesUpsert string
)
func newNotificationPreferencesTable(pool *pgxpool.Pool) *NotificationPreferencesTable {
return &NotificationPreferencesTable{pool}
}
func (NotificationPreferencesTable) Schema() string {
return notificationPreferencesSchema
}
func (t *NotificationPreferencesTable) GetByUserId(ctx context.Context, userId uint64) ([]NotificationPreference, error) {
rows, err := t.Query(ctx, notificationPreferencesGetByUserId, userId)
if err != nil {
return nil, err
}
defer rows.Close()
var prefs []NotificationPreference
for rows.Next() {
var p NotificationPreference
if err := rows.Scan(&p.UserId, &p.Category, &p.DiscordDm, &p.Email, &p.InApp); err != nil {
return nil, err
}
prefs = append(prefs, p)
}
return prefs, nil
}
func (t *NotificationPreferencesTable) GetByUserIdAndCategory(ctx context.Context, userId uint64, category string) (*NotificationPreference, error) {
var p NotificationPreference
if err := t.QueryRow(ctx, notificationPreferencesGetByUserIdAndCategory, userId, category).Scan(
&p.UserId, &p.Category, &p.DiscordDm, &p.Email, &p.InApp,
); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
return nil, err
}
return &p, nil
}
func (t *NotificationPreferencesTable) Upsert(ctx context.Context, userId uint64, category string, discordDm, email, inApp bool) error {
_, err := t.Exec(ctx, notificationPreferencesUpsert, userId, category, discordDm, email, inApp)
return err
}