package repository import ( "context" "github.com/jackc/pgx/v5/pgxpool" "financeiro-carvalho/internal/middleware" "financeiro-carvalho/internal/model" ) type SettingsRepository interface { Get(ctx context.Context) (*model.UserSettings, error) Upsert(ctx context.Context, s model.UserSettings) (*model.UserSettings, error) } type settingsRepo struct{ pool *pgxpool.Pool } func NewSettingsRepository(pool *pgxpool.Pool) SettingsRepository { return &settingsRepo{pool: pool} } func (r *settingsRepo) Get(ctx context.Context) (*model.UserSettings, error) { pid := middleware.ProfileIDFromCtx(ctx) var s model.UserSettings err := r.pool.QueryRow(ctx, ` SELECT savings_goal_pct FROM user_settings WHERE profile_id = $1 `, pid).Scan(&s.SavingsGoalPct) if err != nil { // Return defaults if not yet configured return &model.UserSettings{SavingsGoalPct: 40}, nil } return &s, nil } func (r *settingsRepo) Upsert(ctx context.Context, s model.UserSettings) (*model.UserSettings, error) { pid := middleware.ProfileIDFromCtx(ctx) var out model.UserSettings err := r.pool.QueryRow(ctx, ` INSERT INTO user_settings (profile_id, savings_goal_pct) VALUES ($1, $2) ON CONFLICT (profile_id) DO UPDATE SET savings_goal_pct = EXCLUDED.savings_goal_pct, updated_at = NOW() RETURNING savings_goal_pct `, pid, s.SavingsGoalPct).Scan(&out.SavingsGoalPct) if err != nil { return nil, err } return &out, nil }