Files
toutoukan/controllers/article/deletearticle.go
2025-09-24 01:07:21 +08:00

93 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package article
import (
"net/http"
"toutoukan/init/databaseInit"
"github.com/gin-gonic/gin"
)
// DeleteArticleRequest 删除文章的请求参数
type DeleteArticleRequest struct {
ArticleID int64 `json:"article_id" binding:"required,min=1"` // 要删除的文章ID
}
// DeleteArticle 删除文章(处理外键关联)
func DeleteArticle(c *gin.Context) {
var req DeleteArticleRequest
// 解析请求参数
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "参数解析失败",
"detail": err.Error(),
})
return
}
// 开启事务(确保所有删除操作要么全部成功,要么全部失败)
tx := databaseInit.UserDB.Begin()
if tx.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "开启事务失败: " + tx.Error.Error(),
})
return
}
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
// 步骤1先删除用户投票记录因为依赖文章ID和选项ID
if err := tx.Table("user_votes").
Where("vote_article_id = ?", req.ArticleID).
Delete(nil).Error; err != nil {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{
"error": "删除用户投票记录失败: " + err.Error(),
})
return
}
// 步骤2再删除文章选项因为依赖文章ID
if err := tx.Table("article_options").
Where("vote_article_id = ?", req.ArticleID).
Delete(nil).Error; err != nil {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{
"error": "删除文章选项失败: " + err.Error(),
})
return
}
// 步骤3最后删除文章主记录
if err := tx.Table("article_list").
Where("articleId = ?", req.ArticleID).
Delete(nil).Error; err != nil {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{
"error": "删除文章失败: " + err.Error(),
})
return
}
// 提交事务
if err := tx.Commit().Error; err != nil {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{
"error": "提交删除操作失败: " + err.Error(),
})
return
}
// 返回成功响应
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "文章及关联数据已全部删除",
"data": gin.H{
"deleted_article_id": req.ArticleID,
},
})
}