feat(规则): 支持名称代号,便于区分和管理

创建与更新规则时必填全局唯一 name,已有规则迁移回填为 rule-{id}。
This commit is contained in:
2026-08-17 00:01:34 +08:00
parent b3c42af53b
commit 0f9f154969
10 changed files with 76 additions and 13 deletions
+3
View File
@@ -22,6 +22,7 @@ func NewRuleHandler(s *store.Store, c *cache.Cache) *RuleHandler {
}
type createRuleReq struct {
Name string `json:"name" binding:"required"`
SourceName string `json:"source_name" binding:"required"`
Event string `json:"event" binding:"required"`
TemplateName string `json:"template_name" binding:"required"`
@@ -65,6 +66,7 @@ func (h *RuleHandler) Create(c *gin.Context) {
}
rule := &model.Rule{
Name: req.Name,
SourceID: src.ID,
Event: req.Event,
TemplateID: tmpl.ID,
@@ -138,6 +140,7 @@ func (h *RuleHandler) Update(c *gin.Context) {
}
rule := &model.Rule{
Name: req.Name,
SourceID: src.ID,
Event: req.Event,
TemplateID: tmpl.ID,
+43
View File
@@ -0,0 +1,43 @@
package handler
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func TestCreateRuleReqBindsName(t *testing.T) {
var req createRuleReq
err := json.Unmarshal([]byte(`{
"name":"高低分短线",
"source_name":"crypto-strategy",
"event":"HLSS.*",
"template_name":"高低分短线"
}`), &req)
if err != nil {
t.Fatal(err)
}
if req.Name != "高低分短线" {
t.Fatalf("name=%q", req.Name)
}
}
func TestCreateRuleRequiresName(t *testing.T) {
gin.SetMode(gin.TestMode)
h := NewRuleHandler(nil, nil)
r := gin.New()
r.POST("/api/v1/rules", h.Create)
req := httptest.NewRequest(http.MethodPost, "/api/v1/rules", bytes.NewReader([]byte(
`{"source_name":"s","event":"trade.open","template_name":"t"}`,
)))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("code=%d body=%s", w.Code, w.Body.String())
}
}