package handler import ( "encoding/json" "net/http" "sql-platform/internal/handler/resp" "sql-platform/internal/middleware" "sql-platform/internal/model" "github.com/gin-gonic/gin" ) // GET /api/v1/user/preferences func GetUserPreferences(c *gin.Context) { userID, _, _ := middleware.GetCurrentUser(c) var user model.User if err := model.DB.First(&user, userID).Error; err != nil { resp.NotFound(c, "User not found") return } prefs := map[string]string{} if user.Preferences != nil && *user.Preferences != "" { json.Unmarshal([]byte(*user.Preferences), &prefs) } c.JSON(http.StatusOK, prefs) } // PUT /api/v1/user/preferences func UpdateUserPreferences(c *gin.Context) { userID, _, _ := middleware.GetCurrentUser(c) var prefs map[string]string if err := c.ShouldBindJSON(&prefs); err != nil { resp.BadRequest(c, err.Error()) return } data, _ := json.Marshal(prefs) str := string(data) result := model.DB.Model(&model.User{}).Where("id = ?", userID).Update("preferences", str) if result.Error != nil { resp.InternalErr(c, result.Error) return } // Sync login_style and theme to SystemSetting so public endpoint picks them up syncToSystemSetting := func(key, value string) { var s model.SystemSetting if model.DB.Where("`key` = ?", key).First(&s).Error == nil { model.DB.Model(&s).Update("value", value) } else { model.DB.Create(&model.SystemSetting{ Key: key, Value: value, ValueType: "string", Group: "appearance", Label: key, SortOrder: 0, }) } } if v, ok := prefs["login_style"]; ok { syncToSystemSetting("login_style", v) } if v, ok := prefs["theme"]; ok { syncToSystemSetting("default_theme", v) } c.JSON(http.StatusOK, gin.H{"message": "Preferences updated", "preferences": prefs}) } // GET /api/v1/public/login-style (no auth required) func GetPublicLoginStyle(c *gin.Context) { style := "split" theme := "system" var s model.SystemSetting if model.DB.Where("`key` = ?", "login_style").Take(&s).Error == nil && s.Value != "" { style = s.Value } s = model.SystemSetting{} // reset to avoid GORM reusing primary key if model.DB.Where("`key` = ?", "default_theme").Take(&s).Error == nil && s.Value != "" { theme = s.Value } c.JSON(http.StatusOK, gin.H{"login_style": style, "theme": theme}) }