39f3774940
统一 sources/templates/channels/rules 列表为分页响应,避免配置增多时全量返回;按钉钉 access_token 限制每分钟发送并在超限时等待下一分钟,降低触发官方封禁风险。 Co-authored-by: Cursor <cursoragent@cursor.com>
96 lines
2.6 KiB
Go
96 lines
2.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"aiaa-notification-service/internal/cache"
|
|
"aiaa-notification-service/internal/model"
|
|
"aiaa-notification-service/internal/store"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type TemplateHandler struct {
|
|
store *store.Store
|
|
cache *cache.Cache
|
|
}
|
|
|
|
func NewTemplateHandler(s *store.Store, c *cache.Cache) *TemplateHandler {
|
|
return &TemplateHandler{store: s, cache: c}
|
|
}
|
|
|
|
type createTemplateReq struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Content string `json:"content" binding:"required"`
|
|
}
|
|
|
|
func (h *TemplateHandler) Create(c *gin.Context) {
|
|
var req createTemplateReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
tmpl := &model.Template{Name: req.Name, Content: req.Content}
|
|
if err := h.store.CreateTemplate(c.Request.Context(), tmpl); err != nil {
|
|
c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, tmpl)
|
|
}
|
|
|
|
func (h *TemplateHandler) List(c *gin.Context) {
|
|
var page store.PageFilter
|
|
if err := c.ShouldBindQuery(&page); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
page.Normalize()
|
|
|
|
templates, total, err := h.store.ListTemplates(c.Request.Context(), page)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": templates, "total": total, "page": page.Page})
|
|
}
|
|
|
|
func (h *TemplateHandler) Get(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
tmpl, err := h.store.GetTemplate(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "template not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, tmpl)
|
|
}
|
|
|
|
func (h *TemplateHandler) Update(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
var req createTemplateReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
tmpl := &model.Template{Name: req.Name, Content: req.Content}
|
|
if err := h.store.UpdateTemplate(c.Request.Context(), id, tmpl); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if h.cache != nil {
|
|
_ = h.cache.InvalidateByTemplate(c.Request.Context(), id)
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
}
|
|
|
|
func (h *TemplateHandler) Delete(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
if err := h.store.DeleteTemplate(c.Request.Context(), id); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
}
|