feat(规则): 支持名称代号,便于区分和管理
创建与更新规则时必填全局唯一 name,已有规则迁移回填为 rule-{id}。
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,7 @@ type Channel struct {
|
||||
|
||||
type Rule struct {
|
||||
ID int `db:"id" json:"id"`
|
||||
Name string `db:"name" json:"name"`
|
||||
SourceID int `db:"source_id" json:"source_id"`
|
||||
Event string `db:"event" json:"event"`
|
||||
TemplateID int `db:"template_id" json:"template_id"`
|
||||
|
||||
+11
-11
@@ -16,12 +16,12 @@ func (s *Store) CreateRule(ctx context.Context, r *model.Rule, channelIDs []int)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
query := `INSERT INTO notification_rule (source_id, event, template_id, conditions, enabled) VALUES (?, ?, ?, ?, ?)`
|
||||
query := `INSERT INTO notification_rule (name, source_id, event, template_id, conditions, enabled) VALUES (?, ?, ?, ?, ?, ?)`
|
||||
condsJSON, err := marshalJSON(r.Conditions)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal conditions: %w", err)
|
||||
}
|
||||
result, err := tx.ExecContext(ctx, query, r.SourceID, r.Event, r.TemplateID, condsJSON, r.Enabled)
|
||||
result, err := tx.ExecContext(ctx, query, r.Name, r.SourceID, r.Event, r.TemplateID, condsJSON, r.Enabled)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create rule: %w", err)
|
||||
}
|
||||
@@ -40,8 +40,8 @@ func (s *Store) CreateRule(ctx context.Context, r *model.Rule, channelIDs []int)
|
||||
func (s *Store) GetRule(ctx context.Context, id int) (*model.Rule, error) {
|
||||
var r model.Rule
|
||||
var condsBytes []byte
|
||||
row := s.DB.QueryRowContext(ctx, `SELECT id, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule WHERE id = ?`, id)
|
||||
if err := row.Scan(&r.ID, &r.SourceID, &r.Event, &r.TemplateID, &condsBytes, &r.Enabled, &r.CreatedAt, &r.UpdatedAt); err != nil {
|
||||
row := s.DB.QueryRowContext(ctx, `SELECT id, name, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule WHERE id = ?`, id)
|
||||
if err := row.Scan(&r.ID, &r.Name, &r.SourceID, &r.Event, &r.TemplateID, &condsBytes, &r.Enabled, &r.CreatedAt, &r.UpdatedAt); err != nil {
|
||||
return nil, fmt.Errorf("get rule %d: %w", id, err)
|
||||
}
|
||||
if len(condsBytes) > 0 && string(condsBytes) != "null" {
|
||||
@@ -54,9 +54,9 @@ func (s *Store) GetRule(ctx context.Context, id int) (*model.Rule, error) {
|
||||
func (s *Store) GetRuleBySourceEvent(ctx context.Context, sourceID int, event string) (*model.Rule, error) {
|
||||
var r model.Rule
|
||||
var condsBytes []byte
|
||||
query := `SELECT id, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule WHERE source_id = ? AND event = ? AND enabled = 1`
|
||||
query := `SELECT id, name, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule WHERE source_id = ? AND event = ? AND enabled = 1`
|
||||
row := s.DB.QueryRowContext(ctx, query, sourceID, event)
|
||||
if err := row.Scan(&r.ID, &r.SourceID, &r.Event, &r.TemplateID, &condsBytes, &r.Enabled, &r.CreatedAt, &r.UpdatedAt); err != nil {
|
||||
if err := row.Scan(&r.ID, &r.Name, &r.SourceID, &r.Event, &r.TemplateID, &condsBytes, &r.Enabled, &r.CreatedAt, &r.UpdatedAt); err != nil {
|
||||
return nil, fmt.Errorf("get rule by source+event: %w", err)
|
||||
}
|
||||
if len(condsBytes) > 0 && string(condsBytes) != "null" {
|
||||
@@ -67,7 +67,7 @@ func (s *Store) GetRuleBySourceEvent(ctx context.Context, sourceID int, event st
|
||||
}
|
||||
|
||||
func (s *Store) ListEnabledRulesBySource(ctx context.Context, sourceID int) ([]model.Rule, error) {
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT id, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule WHERE source_id = ? AND enabled = 1`, sourceID)
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT id, name, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule WHERE source_id = ? AND enabled = 1`, sourceID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list enabled rules by source: %w", err)
|
||||
}
|
||||
@@ -86,7 +86,7 @@ func (s *Store) ListRules(ctx context.Context, page PageFilter) ([]model.Rule, i
|
||||
}
|
||||
|
||||
page.Normalize()
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT id, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule ORDER BY id LIMIT ? OFFSET ?`, page.PageSize, page.Offset())
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT id, name, source_id, event, template_id, conditions, enabled, created_at, updated_at FROM notification_rule ORDER BY id LIMIT ? OFFSET ?`, page.PageSize, page.Offset())
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("list rules: %w", err)
|
||||
}
|
||||
@@ -109,8 +109,8 @@ func (s *Store) UpdateRule(ctx context.Context, id int, r *model.Rule, channelID
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal conditions: %w", err)
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `UPDATE notification_rule SET source_id=?, event=?, template_id=?, conditions=?, enabled=? WHERE id=?`,
|
||||
r.SourceID, r.Event, r.TemplateID, condsJSON, r.Enabled, id)
|
||||
_, err = tx.ExecContext(ctx, `UPDATE notification_rule SET name=?, source_id=?, event=?, template_id=?, conditions=?, enabled=? WHERE id=?`,
|
||||
r.Name, r.SourceID, r.Event, r.TemplateID, condsJSON, r.Enabled, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update rule: %w", err)
|
||||
}
|
||||
@@ -183,7 +183,7 @@ func scanRules(rows *sql.Rows) ([]model.Rule, error) {
|
||||
for rows.Next() {
|
||||
var r model.Rule
|
||||
var condsBytes []byte
|
||||
if err := rows.Scan(&r.ID, &r.SourceID, &r.Event, &r.TemplateID, &condsBytes, &r.Enabled, &r.CreatedAt, &r.UpdatedAt); err != nil {
|
||||
if err := rows.Scan(&r.ID, &r.Name, &r.SourceID, &r.Event, &r.TemplateID, &condsBytes, &r.Enabled, &r.CreatedAt, &r.UpdatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(condsBytes) > 0 && string(condsBytes) != "null" {
|
||||
|
||||
Reference in New Issue
Block a user