feat: management handlers — source, template, channel CRUD

This commit is contained in:
2026-06-27 13:23:10 +08:00
parent 2cf6601be0
commit 682ca3df81
4 changed files with 500 additions and 0 deletions
+109
View File
@@ -0,0 +1,109 @@
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 SourceHandler struct {
store *store.Store
cache *cache.Cache
}
func NewSourceHandler(s *store.Store, c *cache.Cache) *SourceHandler {
return &SourceHandler{store: s, cache: c}
}
type createSourceReq struct {
Name string `json:"name" binding:"required"`
ParseMode string `json:"parse_mode"`
ParsePattern string `json:"parse_pattern"`
Status int `json:"status"`
}
func (h *SourceHandler) Create(c *gin.Context) {
var req createSourceReq
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if req.ParseMode == "" {
req.ParseMode = "json"
}
if req.Status == 0 {
req.Status = 1
}
src := &model.Source{
Name: req.Name,
ParseMode: req.ParseMode,
ParsePattern: req.ParsePattern,
Status: req.Status,
}
if err := h.store.CreateSource(c.Request.Context(), src); err != nil {
c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, src)
}
func (h *SourceHandler) List(c *gin.Context) {
sources, err := h.store.ListSources(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, sources)
}
func (h *SourceHandler) Get(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
src, err := h.store.GetSource(c.Request.Context(), id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "source not found"})
return
}
c.JSON(http.StatusOK, src)
}
func (h *SourceHandler) Update(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
var req createSourceReq
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
src := &model.Source{
Name: req.Name,
ParseMode: req.ParseMode,
ParsePattern: req.ParsePattern,
Status: req.Status,
}
if err := h.store.UpdateSource(c.Request.Context(), id, src); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Invalidate cache
if h.cache != nil {
_ = h.cache.InvalidateBySource(c.Request.Context(), id)
}
c.JSON(http.StatusOK, gin.H{"ok": true})
}
func (h *SourceHandler) Delete(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
if err := h.store.DeleteSource(c.Request.Context(), id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"ok": true})
}