- PATCH /me/settings accepts exactly one JSON object: text after it, null, a differently cased name or an unknown name are refused (400) and change nothing. - A user deleted between the read and the write of the settings gives 401, not a 500, on PostgreSQL too (foreign key violation mapped to user not found). - The spec says a 401 can also mean the account no longer exists, and that the body is strict. - README no longer says more than the setting does. - Tests: strict body cases, tokens of a deleted account on GET and PATCH, recognising a foreign key violation on both databases.
68 lines
2.2 KiB
Go
68 lines
2.2 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"codeberg.org/squidd/sonda/internal/usersettings"
|
|
)
|
|
|
|
// UserSettingsRepo stores per-user settings. It implements usersettings.Store.
|
|
type UserSettingsRepo struct{ db *bun.DB }
|
|
|
|
// NewUserSettingsRepo builds a UserSettingsRepo.
|
|
func NewUserSettingsRepo(db *bun.DB) *UserSettingsRepo { return &UserSettingsRepo{db: db} }
|
|
|
|
// Get returns the user's settings: the defaults when no row exists yet.
|
|
func (r *UserSettingsRepo) Get(ctx context.Context, userID int64) (usersettings.Settings, error) {
|
|
return getUserSettings(ctx, r.db, userID)
|
|
}
|
|
|
|
type queryer interface {
|
|
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
|
|
}
|
|
|
|
func getUserSettings(ctx context.Context, q queryer, userID int64) (usersettings.Settings, error) {
|
|
var s usersettings.Settings
|
|
err := q.QueryRowContext(ctx,
|
|
`SELECT COALESCE(s.discover_from_listening, FALSE)
|
|
FROM users u LEFT JOIN user_settings s ON s.user_id = u.id WHERE u.id = ?`, userID).
|
|
Scan(&s.DiscoverFromListening)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return usersettings.Settings{}, usersettings.ErrUserNotFound
|
|
}
|
|
if err != nil {
|
|
return usersettings.Settings{}, fmt.Errorf("read user settings: %w", err)
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
// Update applies the patch in one transaction and returns the result.
|
|
func (r *UserSettingsRepo) Update(ctx context.Context, userID int64, p usersettings.Patch) (usersettings.Settings, error) {
|
|
var out usersettings.Settings
|
|
err := r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
|
cur, err := getUserSettings(ctx, tx, userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if p.DiscoverFromListening != nil {
|
|
cur.DiscoverFromListening = *p.DiscoverFromListening
|
|
if _, err := tx.ExecContext(ctx,
|
|
`INSERT INTO user_settings (user_id, discover_from_listening) VALUES (?, ?)
|
|
ON CONFLICT (user_id) DO UPDATE SET discover_from_listening = excluded.discover_from_listening`,
|
|
userID, cur.DiscoverFromListening); err != nil {
|
|
if isForeignKeyViolation(err) {
|
|
return usersettings.ErrUserNotFound // deleted between the read and the write
|
|
}
|
|
return fmt.Errorf("write user settings: %w", err)
|
|
}
|
|
}
|
|
out = cur
|
|
return nil
|
|
})
|
|
return out, err
|
|
}
|