From 3824fbbf5fd2750f0f43b3d37663928e2db49e62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A1=E8=8B=A5?= Date: Sun, 8 Mar 2026 08:25:06 +0800 Subject: [PATCH] =?UTF-8?q?sync:=20soul-api=20=E6=8E=A5=E5=8F=A3=E9=80=BB?= =?UTF-8?q?=E8=BE=91=20|=20=E5=8E=9F=E5=9B=A0:=20=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E9=80=BB=E8=BE=91=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- soul-api/internal/handler/admin_user_rules.go | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 soul-api/internal/handler/admin_user_rules.go diff --git a/soul-api/internal/handler/admin_user_rules.go b/soul-api/internal/handler/admin_user_rules.go new file mode 100644 index 00000000..a99604a4 --- /dev/null +++ b/soul-api/internal/handler/admin_user_rules.go @@ -0,0 +1,121 @@ +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": "方法不支持"}) + } +}