增加批量导入违禁词

This commit is contained in:
2026-07-05 14:59:05 +08:00
parent cfb7d2e0ea
commit f27c6ba60c
4 changed files with 96 additions and 1 deletions
@@ -2,6 +2,7 @@ package handler
import (
"net/http"
"strings"
"backend/internal/repo"
"github.com/gin-gonic/gin"
@@ -51,6 +52,31 @@ func (h *BannedWordsHandler) Create(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"data": gin.H{"id": item.ID, "word": item.Word, "hits": item.Hits, "created_at": item.CreatedAt}})
}
// Import bulk-adds words from a free-form text blob — split on newlines,
// commas (英文/中文), 顿号 and semicolons — skipping blanks and existing entries.
func (h *BannedWordsHandler) Import(c *gin.Context) {
var body struct {
Text string `json:"text"`
}
if err := c.ShouldBindJSON(&body); err != nil || strings.TrimSpace(body.Text) == "" {
c.JSON(http.StatusBadRequest, gin.H{"detail": "请提供要导入的违禁词"})
return
}
words := strings.FieldsFunc(body.Text, func(r rune) bool {
switch r {
case '\n', '\r', ',', '', '、', ';', '':
return true
}
return false
})
added, skipped, err := h.words.BulkCreate(c.Request.Context(), words)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"detail": "导入失败"})
return
}
c.JSON(http.StatusOK, gin.H{"added": added, "skipped": skipped})
}
func (h *BannedWordsHandler) Delete(c *gin.Context) {
n, err := h.words.Delete(c.Request.Context(), c.Param("id"))
if err != nil {
+1
View File
@@ -142,6 +142,7 @@ func New(cfg *config.Config, auth *service.AuthService, handlers Handlers) *gin.
authed.DELETE("/images", handlers.AdminRead.DeleteImage)
authed.GET("/banned-words", handlers.BannedWords.List)
authed.POST("/banned-words", handlers.BannedWords.Create)
authed.POST("/banned-words/import", handlers.BannedWords.Import)
authed.DELETE("/banned-words/:id", handlers.BannedWords.Delete)
authed.GET("/refresh/profiles", handlers.ProviderAdmin.RefreshProfiles)
authed.POST("/refresh/profiles/:profile_id/refresh-now", handlers.ProviderAdmin.RefreshNow)