mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-04 02:32:57 +00:00
dev day #79
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"fotbal-club/internal/models"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -187,13 +188,38 @@ func (pc *PageElementConfigController) BatchUpdatePageElementConfigs(c *gin.Cont
|
||||
updated := 0
|
||||
created := 0
|
||||
|
||||
// Validate styles before saving
|
||||
validator := &StyleValidator{}
|
||||
for i := range configs {
|
||||
if len(configs[i].Settings) > 0 {
|
||||
// ElementSettings is already a map[string]interface{} type
|
||||
settingsMap := map[string]interface{}(configs[i].Settings)
|
||||
|
||||
// Validate and normalize
|
||||
if err := validator.ValidateAndNormalizeStyles(settingsMap); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Style validation failed for %s: %v", configs[i].ElementName, err)})
|
||||
return
|
||||
}
|
||||
|
||||
// Update back
|
||||
configs[i].Settings = models.ElementSettings(settingsMap)
|
||||
}
|
||||
}
|
||||
|
||||
err := pc.DB.Transaction(func(tx *gorm.DB) error {
|
||||
for _, cfg := range configs {
|
||||
var existing models.PageElementConfig
|
||||
result := tx.Where("page_type = ? AND element_name = ?", cfg.PageType, cfg.ElementName).First(&existing)
|
||||
|
||||
if result.Error == nil {
|
||||
// Update
|
||||
// Update - merge styles to preserve other settings
|
||||
if len(cfg.Settings) > 0 && len(existing.Settings) > 0 {
|
||||
existingMap := map[string]interface{}(existing.Settings)
|
||||
newMap := map[string]interface{}(cfg.Settings)
|
||||
mergedMap := validator.MergeStyles(existingMap, newMap)
|
||||
cfg.Settings = models.ElementSettings(mergedMap)
|
||||
}
|
||||
|
||||
existing.Variant = cfg.Variant
|
||||
existing.Visible = cfg.Visible
|
||||
existing.DisplayOrder = cfg.DisplayOrder
|
||||
|
||||
Reference in New Issue
Block a user