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:
2026-07-31 22:09:39 +08:00
parent 34fb0f62b9
commit c74bab3033
7 changed files with 968 additions and 6 deletions
+1 -1
View File
@@ -54,7 +54,7 @@ func (s *Store) ListChannels(ctx context.Context) ([]model.Channel, error) {
}
defer rows.Close()
var channels []model.Channel
channels := make([]model.Channel, 0)
for rows.Next() {
var ch model.Channel
var configBytes []byte
+1 -1
View File
@@ -62,7 +62,7 @@ func (s *Store) ListMessageLogs(ctx context.Context, filter MessageLogFilter) ([
}
offset := (filter.Page - 1) * filter.PageSize
var logs []model.MessageLog
logs := make([]model.MessageLog, 0)
query := "SELECT * FROM notification_message_log " + where + " ORDER BY id DESC LIMIT ? OFFSET ?"
args = append(args, filter.PageSize, offset)
if err := s.DB.SelectContext(ctx, &logs, query, args...); err != nil {
+2 -2
View File
@@ -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
+1 -1
View File
@@ -55,7 +55,7 @@ func (s *Store) GetSourceByName(ctx context.Context, name string) (*model.Source
}
func (s *Store) ListSources(ctx context.Context) ([]model.Source, error) {
var sources []model.Source
sources := make([]model.Source, 0)
err := s.DB.SelectContext(ctx, &sources, `SELECT * FROM notification_source ORDER BY id`)
if err != nil {
return nil, fmt.Errorf("list sources: %w", err)
+1 -1
View File
@@ -37,7 +37,7 @@ func (s *Store) GetTemplateByName(ctx context.Context, name string) (*model.Temp
}
func (s *Store) ListTemplates(ctx context.Context) ([]model.Template, error) {
var templates []model.Template
templates := make([]model.Template, 0)
err := s.DB.SelectContext(ctx, &templates, `SELECT * FROM notification_template ORDER BY id`)
if err != nil {
return nil, fmt.Errorf("list templates: %w", err)