package handler import ( "net/http" "strconv" "soul-api/internal/database" "soul-api/internal/model" "github.com/gin-gonic/gin" "gorm.io/gorm" ) // DBUserRulesList GET /api/db/user-rules func DBUserRulesList(c *gin.Context) { db := database.DB() var rules []model.UserRule if err := db.Order("sort ASC, id ASC").Find(&rules).Error; err != nil { c.JSON(http.StatusOK, gin.H{"success": false, "error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"success": true, "rules": rules}) } // DBUserRulesAction POST/PUT/DELETE /api/db/user-rules func DBUserRulesAction(c *gin.Context) { db := database.DB() switch c.Request.Method { case http.MethodPost: var body struct { Title string `json:"title" binding:"required"` Description string `json:"description"` Trigger string `json:"trigger"` Sort int `json:"sort"` Enabled *bool `json:"enabled"` } if err := c.ShouldBindJSON(&body); err != nil { c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": "参数错误"}) return } enabled := true if body.Enabled != nil { enabled = *body.Enabled } rule := model.UserRule{ Title: trimSpace(body.Title), Description: body.Description, Trigger: trimSpace(body.Trigger), Sort: body.Sort, Enabled: enabled, } if err := db.Create(&rule).Error; err != nil { c.JSON(http.StatusOK, gin.H{"success": false, "error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"success": true, "rule": rule}) case http.MethodPut: var body struct { ID uint `json:"id" binding:"required"` Title string `json:"title"` Description string `json:"description"` Trigger string `json:"trigger"` Sort *int `json:"sort"` Enabled *bool `json:"enabled"` } if err := c.ShouldBindJSON(&body); err != nil { c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": "参数错误"}) return } var rule model.UserRule if err := db.First(&rule, body.ID).Error; err != nil { if err == gorm.ErrRecordNotFound { c.JSON(http.StatusNotFound, gin.H{"success": false, "error": "规则不存在"}) return } c.JSON(http.StatusOK, gin.H{"success": false, "error": err.Error()}) return } updates := make(map[string]interface{}) if body.Title != "" { updates["title"] = trimSpace(body.Title) } updates["description"] = body.Description updates["trigger"] = trimSpace(body.Trigger) if body.Sort != nil { updates["sort"] = *body.Sort } if body.Enabled != nil { updates["enabled"] = *body.Enabled } if err := db.Model(&rule).Updates(updates).Error; err != nil { c.JSON(http.StatusOK, gin.H{"success": false, "error": err.Error()}) return } var updated model.UserRule _ = db.First(&updated, body.ID) c.JSON(http.StatusOK, gin.H{"success": true, "rule": updated}) case http.MethodDelete: idStr := c.Query("id") if idStr == "" { c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": "缺少 id"}) return } id, err := strconv.ParseUint(idStr, 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": "id 无效"}) return } if err := db.Delete(&model.UserRule{}, uint(id)).Error; err != nil { c.JSON(http.StatusOK, gin.H{"success": false, "error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"success": true}) default: c.JSON(http.StatusMethodNotAllowed, gin.H{"success": false, "error": "方法不支持"}) } }