feat: management handlers — source, template, channel CRUD
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
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) {
|
||||
templates, err := h.store.ListTemplates(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, templates)
|
||||
}
|
||||
|
||||
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})
|
||||
}
|
||||
Reference in New Issue
Block a user