fix(数据存储): 确保列表查询返回空数组而非 null,并补充 HTTPie/Postman API 文档
Motivation: 当数据库查询无结果时,Go 的 `var slice []Type` 声明会使 JSON 序列化输出 `null`,而非 `[]`。这在 API 响应中造成不一致,也增加了客户端的空值处理负担。改为 `make([]Type, 0)` 后空结果统一输出为 `[]`,提升 API 规范性。 Changes: * 统一 store 层所有 List 方法使用 `make([]Type, 0)` 初始化切片,覆盖 Sources、Channels、Templates、Rules、MessageLogs 及 RuleChannels 查询 * 新增完整的 cURL 示例文档,覆盖全部 API 端点(通知、Source、Template、Channel、Rule、消息记录) * 新增 Postman Collection JSON 文件,可直接导入 HTTPie 使用,包含环境变量配置
This commit is contained in:
@@ -127,7 +127,7 @@ func (s *Store) SetRuleEnabled(ctx context.Context, id int, enabled bool) error
|
||||
}
|
||||
|
||||
func (s *Store) GetRuleChannels(ctx context.Context, ruleID int) ([]model.RuleChannel, error) {
|
||||
var rcs []model.RuleChannel
|
||||
rcs := make([]model.RuleChannel, 0)
|
||||
err := s.DB.SelectContext(ctx, &rcs, `SELECT id, rule_id, channel_id, enabled FROM notification_rule_channel WHERE rule_id = ? AND enabled = 1`, ruleID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get rule channels: %w", err)
|
||||
@@ -156,7 +156,7 @@ func marshalJSON(v *json.RawMessage) (interface{}, error) {
|
||||
}
|
||||
|
||||
func scanRules(rows *sql.Rows) ([]model.Rule, error) {
|
||||
var rules []model.Rule
|
||||
rules := make([]model.Rule, 0)
|
||||
for rows.Next() {
|
||||
var r model.Rule
|
||||
var condsBytes []byte
|
||||
|
||||
Reference in New Issue
Block a user