Files
koin_ping_0.2.0/backend-go/internal/models/notification_config.go
2026-02-27 17:33:40 -05:00

95 lines
2.9 KiB
Go

package models
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/kjannette/koin-ping/backend-go/internal/domain"
)
type NotificationConfigModel struct {
pool *pgxpool.Pool
}
func NewNotificationConfigModel(pool *pgxpool.Pool) *NotificationConfigModel {
return &NotificationConfigModel{pool: pool}
}
func (m *NotificationConfigModel) GetConfig(ctx context.Context, userID string) (*domain.NotificationConfig, error) {
var c domain.NotificationConfig
err := m.pool.QueryRow(ctx,
`SELECT user_id, discord_webhook_url, slack_webhook_url,
email, notification_enabled, created_at, updated_at
FROM user_notification_configs
WHERE user_id = $1`,
userID,
).Scan(&c.UserID, &c.DiscordWebhookURL, &c.SlackWebhookURL,
&c.Email, &c.NotificationEnabled, &c.CreatedAt, &c.UpdatedAt)
if err != nil {
if err.Error() == "no rows in result set" {
return nil, nil
}
return nil, err
}
return &c, nil
}
func (m *NotificationConfigModel) UpsertConfig(ctx context.Context, userID string, cfg domain.NotificationConfig) (*domain.NotificationConfig, error) {
var c domain.NotificationConfig
err := m.pool.QueryRow(ctx,
`INSERT INTO user_notification_configs
(user_id, discord_webhook_url, slack_webhook_url, email, notification_enabled, updated_at)
VALUES ($1, $2, $3, $4, $5, NOW())
ON CONFLICT (user_id)
DO UPDATE SET
discord_webhook_url = COALESCE($2, user_notification_configs.discord_webhook_url),
slack_webhook_url = COALESCE($3, user_notification_configs.slack_webhook_url),
email = COALESCE($4, user_notification_configs.email),
notification_enabled = $5,
updated_at = NOW()
RETURNING user_id, discord_webhook_url, slack_webhook_url,
email, notification_enabled, created_at, updated_at`,
userID, cfg.DiscordWebhookURL, cfg.SlackWebhookURL,
cfg.Email, cfg.NotificationEnabled,
).Scan(&c.UserID, &c.DiscordWebhookURL, &c.SlackWebhookURL,
&c.Email, &c.NotificationEnabled, &c.CreatedAt, &c.UpdatedAt)
if err != nil {
return nil, err
}
return &c, nil
}
func (m *NotificationConfigModel) Remove(ctx context.Context, userID string) (bool, error) {
tag, err := m.pool.Exec(ctx,
`DELETE FROM user_notification_configs WHERE user_id = $1`,
userID,
)
if err != nil {
return false, err
}
return tag.RowsAffected() > 0, nil
}
func (m *NotificationConfigModel) ListEnabled(ctx context.Context) ([]domain.NotificationConfig, error) {
rows, err := m.pool.Query(ctx,
`SELECT user_id, discord_webhook_url, slack_webhook_url, email
FROM user_notification_configs
WHERE notification_enabled = TRUE`,
)
if err != nil {
return nil, err
}
defer rows.Close()
var configs []domain.NotificationConfig
for rows.Next() {
var c domain.NotificationConfig
if err := rows.Scan(&c.UserID, &c.DiscordWebhookURL, &c.SlackWebhookURL, &c.Email); err != nil {
return nil, err
}
c.NotificationEnabled = true
configs = append(configs, c)
}
return configs, rows.Err()
}