fix: 添加分类名称唯一约束,防止重复名称

This commit is contained in:
2026-05-25 17:38:11 +08:00
parent 4b03e593bc
commit 2cb86d1b38
6 changed files with 249 additions and 47 deletions
-13
View File
@@ -5,7 +5,6 @@ import (
"net/http"
"strconv"
"sale/internal/crawler"
"sale/internal/models"
"sale/internal/schemas"
"sale/internal/utils"
@@ -164,15 +163,3 @@ func (h *ArticleHandler) TogglePin(c *gin.Context) {
utils.DB.First(&article, id)
c.JSON(http.StatusOK, gin.H{"data": article})
}
// CrawlRibenyan 采集 ribenyan.com 文章
func (h *ArticleHandler) CrawlRibenyan(c *gin.Context) {
crawler := crawler.NewRibenyanCrawler(utils.DB)
if err := crawler.CrawlArticles(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Crawl completed successfully"})
}
+14
View File
@@ -34,6 +34,12 @@ func (h *CategoryHandler) Create(c *gin.Context) {
return
}
var existing models.Category
if err := utils.DB.Where("name = ?", req.Name).First(&existing).Error; err == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "分类名称已存在"})
return
}
category := models.Category{
Name: req.Name,
Description: req.Description,
@@ -69,6 +75,14 @@ func (h *CategoryHandler) Update(c *gin.Context) {
return
}
if req.Name != nil && *req.Name != category.Name {
var existing models.Category
if err := utils.DB.Where("name = ? AND id != ?", *req.Name, id).First(&existing).Error; err == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "分类名称已存在"})
return
}
}
updates := make(map[string]interface{})
if req.Name != nil {
updates["name"] = *req.Name