Files
aiaa-notification-server/internal/store/template.go
T
ryan c74bab3033 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 使用,包含环境变量配置
2026-07-31 22:09:39 +08:00

64 lines
1.9 KiB
Go

package store
import (
"context"
"fmt"
"aiaa-notification-service/internal/model"
)
func (s *Store) CreateTemplate(ctx context.Context, t *model.Template) error {
query := `INSERT INTO notification_template (name, content) VALUES (?, ?)`
result, err := s.DB.ExecContext(ctx, query, t.Name, t.Content)
if err != nil {
return fmt.Errorf("create template: %w", err)
}
id, _ := result.LastInsertId()
t.ID = int(id)
return nil
}
func (s *Store) GetTemplate(ctx context.Context, id int) (*model.Template, error) {
var t model.Template
err := s.DB.GetContext(ctx, &t, `SELECT * FROM notification_template WHERE id = ?`, id)
if err != nil {
return nil, fmt.Errorf("get template %d: %w", id, err)
}
return &t, nil
}
func (s *Store) GetTemplateByName(ctx context.Context, name string) (*model.Template, error) {
var t model.Template
err := s.DB.GetContext(ctx, &t, `SELECT * FROM notification_template WHERE name = ?`, name)
if err != nil {
return nil, fmt.Errorf("get template by name %s: %w", name, err)
}
return &t, nil
}
func (s *Store) ListTemplates(ctx context.Context) ([]model.Template, error) {
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)
}
return templates, nil
}
func (s *Store) UpdateTemplate(ctx context.Context, id int, t *model.Template) error {
query := `UPDATE notification_template SET name=?, content=? WHERE id=?`
_, err := s.DB.ExecContext(ctx, query, t.Name, t.Content, id)
if err != nil {
return fmt.Errorf("update template %d: %w", id, err)
}
return nil
}
func (s *Store) DeleteTemplate(ctx context.Context, id int) error {
_, err := s.DB.ExecContext(ctx, `DELETE FROM notification_template WHERE id = ?`, id)
if err != nil {
return fmt.Errorf("delete template %d: %w", id, err)
}
return nil
}