45 lines
839 B
Go
45 lines
839 B
Go
package usersettingsrepo
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
type Repo struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewRepo(db *sql.DB) *Repo {
|
|
return &Repo{db: db}
|
|
}
|
|
|
|
func (r *Repo) SetTimezone(ctx context.Context, userID int64, zone string) error {
|
|
const q = `
|
|
INSERT INTO user_settings (user_id, timezone)
|
|
VALUES ($1, $2)
|
|
ON CONFLICT (user_id)
|
|
DO UPDATE SET timezone = EXCLUDED.timezone
|
|
`
|
|
_, err := r.db.ExecContext(ctx, q, userID, zone)
|
|
return err
|
|
}
|
|
|
|
func (r *Repo) GetTimezone(ctx context.Context, userID int64) (string, bool, error) {
|
|
const q = `
|
|
SELECT timezone
|
|
FROM user_settings
|
|
WHERE user_id = $1
|
|
`
|
|
row := r.db.QueryRowContext(ctx, q, userID)
|
|
var zone string
|
|
switch err := row.Scan(&zone); err {
|
|
case nil:
|
|
return zone, true, nil
|
|
case sql.ErrNoRows:
|
|
return "", false, nil
|
|
default:
|
|
return "", false, err
|
|
}
|
|
}
|
|
|