dev day #90 🥳

This commit is contained in:
Tomas Dvorak
2025-11-12 20:31:37 +01:00
parent 8762bde4bf
commit f3db65d350
103 changed files with 4053 additions and 2189 deletions
+93 -22
View File
@@ -162,35 +162,106 @@ func (nc *NavigationController) UpdateNavigationItem(c *gin.Context) {
return
}
var updates models.NavigationItem
if err := c.ShouldBindJSON(&updates); err != nil {
// Bind into a generic map to know which fields are present (partial update)
var raw map[string]interface{}
if err := c.ShouldBindJSON(&raw); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Update fields
item.Label = updates.Label
item.URL = updates.URL
item.Icon = updates.Icon
item.Type = updates.Type
item.PageType = updates.PageType
item.PageID = updates.PageID
item.Visible = updates.Visible
item.DisplayOrder = updates.DisplayOrder
item.ParentID = updates.ParentID
item.Target = updates.Target
item.CSSClass = updates.CSSClass
item.RequiresAuth = updates.RequiresAuth
item.RequiresAdmin = updates.RequiresAdmin
if err := nc.DB.Save(&item).Error; err != nil {
// Allow-list of updatable fields and basic type normalization
updates := map[string]interface{}{}
if v, ok := raw["label"]; ok {
if s, ok2 := v.(string); ok2 { updates["label"] = s }
}
if v, ok := raw["url"]; ok {
if s, ok2 := v.(string); ok2 { updates["url"] = s }
}
if v, ok := raw["icon"]; ok {
if s, ok2 := v.(string); ok2 { updates["icon"] = s }
}
if v, ok := raw["type"]; ok {
if s, ok2 := v.(string); ok2 { updates["type"] = s }
}
if v, ok := raw["page_type"]; ok {
if s, ok2 := v.(string); ok2 { updates["page_type"] = s }
}
if v, ok := raw["page_id"]; ok {
switch t := v.(type) {
case float64:
updates["page_id"] = int(t)
case int:
updates["page_id"] = t
case int32:
updates["page_id"] = int(t)
case int64:
updates["page_id"] = int(t)
case nil:
updates["page_id"] = nil
}
}
if v, ok := raw["visible"]; ok {
if b, ok2 := v.(bool); ok2 { updates["visible"] = b }
}
if v, ok := raw["display_order"]; ok {
switch t := v.(type) {
case float64:
updates["display_order"] = int(t)
case int:
updates["display_order"] = t
case int32:
updates["display_order"] = int(t)
case int64:
updates["display_order"] = int(t)
}
}
if v, ok := raw["parent_id"]; ok {
switch t := v.(type) {
case float64:
updates["parent_id"] = int(t)
case int:
updates["parent_id"] = t
case int32:
updates["parent_id"] = int(t)
case int64:
updates["parent_id"] = int(t)
case nil:
updates["parent_id"] = nil
}
}
if v, ok := raw["target"]; ok {
if s, ok2 := v.(string); ok2 { updates["target"] = s }
}
if v, ok := raw["css_class"]; ok {
if s, ok2 := v.(string); ok2 { updates["css_class"] = s }
}
if v, ok := raw["requires_auth"]; ok {
if b, ok2 := v.(bool); ok2 { updates["requires_auth"] = b }
}
if v, ok := raw["requires_admin"]; ok {
if b, ok2 := v.(bool); ok2 { updates["requires_admin"] = b }
}
if len(updates) == 0 {
// Nothing to update
c.JSON(http.StatusOK, item)
return
}
if err := nc.DB.Model(&item).Updates(updates).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to update navigation item",
"details": err.Error(),
})
return
}
// Reload to return consistent, fresh data
if err := nc.DB.First(&item, id).Error; err != nil {
c.JSON(http.StatusOK, gin.H{"message": "Updated", "id": id})
return
}
c.JSON(http.StatusOK, item)
}
@@ -524,8 +595,8 @@ func (nc *NavigationController) SeedDefaultNavigation(c *gin.Context) {
if err != nil { return err }
if err := createChild(obsah, "Články", "articles", 0); err != nil { return err }
if err := createChild(obsah, "Aktivity", "activities", 1); err != nil { return err }
if err := createChild(obsah, "Kategorie", "categories", 2); err != nil { return err }
if err := createChild(obsah, "Komentáře", "comments", 3); err != nil { return err }
// Kategorie admin page removed (categories derived from competition aliases)
if err := createChild(obsah, "Komentáře", "comments", 2); err != nil { return err }
media, err := createCategory("Média")
if err != nil { return err }