π The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.
Vue dockergolangkanbanknowledge-baseknowledge-managementmarkdownnote-takingnotesnotes-appnotionnuxtnuxtjsoffline-firstoidcpwaselfhostedssovuevuejswiki
package repositories
import (
"alexandrie/models"
"alexandrie/types"
"database/sql"
"fmt"
"github.com/jmoiron/sqlx"
)
type UserSettingsRepositoryImpl struct {
db *sqlx.DB
}
type UserSettingsRepository interface {
GetByUserID(userID types.Snowflake) (*models.UserSettings, error)
Upsert(settings *models.UserSettings) error
}
func NewUserSettingsRepository(db *sqlx.DB) UserSettingsRepository {
return &UserSettingsRepositoryImpl{db: db}
}
func (r *UserSettingsRepositoryImpl) GetByUserID(userID types.Snowflake) (*models.UserSettings, error) {
var settings models.UserSettings
err := r.db.Get(&settings, `SELECT user_id, general, editor, advanced FROM user_settings WHERE user_id = ?`, userID)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("failed to get user settings: %w", err)
}
return &settings, nil
}
func (r *UserSettingsRepositoryImpl) Upsert(settings *models.UserSettings) error {
_, err := r.db.NamedExec(`
INSERT INTO user_settings (user_id, general, editor, advanced)
VALUES (:user_id, :general, :editor, :advanced)
ON DUPLICATE KEY UPDATE
general = VALUES(general),
editor = VALUES(editor),
advanced = VALUES(advanced)
`, settings)
if err != nil {
return fmt.Errorf("failed to upsert user settings: %w", err)
}
return nil
}