修改文评功能
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
package article
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
"time"
|
||||
"toutoukan/init/databaseInit"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// OptionItem 选项子结构
|
||||
@@ -23,25 +24,33 @@ type CreateArticleRequest struct {
|
||||
Options []OptionItem `json:"options" binding:"required,min=1,max=3"`
|
||||
}
|
||||
|
||||
// 数据库文章记录结构体(与表结构完全对应)
|
||||
type ArticleList struct {
|
||||
ArticleID int64 `gorm:"column:articleId;primaryKey;autoIncrement"` // 明确主键和自增
|
||||
PublishUserID string `gorm:"column:publish_user_id"`
|
||||
Title string `gorm:"column:title"`
|
||||
VoteType string `gorm:"column:vote_type"`
|
||||
EndTime time.Time `gorm:"column:end_time"`
|
||||
IsEnded bool `gorm:"column:is_ended"`
|
||||
TotalVotersNum int `gorm:"column:total_voters_num"`
|
||||
CreateTime time.Time `gorm:"column:create_time"`
|
||||
// ArticleList 数据库文章记录结构体
|
||||
|
||||
// UserPoint 用户积分记录表结构
|
||||
|
||||
// UserInfo 用户信息表结构
|
||||
type UserInfo struct {
|
||||
UID string `gorm:"column:uid;primaryKey"`
|
||||
TotalPoints int `gorm:"column:total_points"`
|
||||
}
|
||||
|
||||
// 自定义表名(如果结构体名与表名不一致)
|
||||
// 自定义表名,GORM会根据这些结构体进行操作
|
||||
func (ArticleList) TableName() string {
|
||||
return "article_list"
|
||||
}
|
||||
func (UserPoint) TableName() string {
|
||||
return "user_points"
|
||||
}
|
||||
func (UserInfo) TableName() string {
|
||||
return "user_info"
|
||||
}
|
||||
|
||||
// CreateArticle 创建文章(包含选项)
|
||||
func CreateArticle(c *gin.Context) {
|
||||
const (
|
||||
POST_ARTICLE_POINTS = 2 // 发表文章获得的积分
|
||||
)
|
||||
|
||||
var req CreateArticleRequest
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
@@ -52,6 +61,7 @@ func CreateArticle(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 开启数据库事务
|
||||
tx := databaseInit.UserDB.Begin()
|
||||
if tx.Error != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "开启事务失败: " + tx.Error.Error()})
|
||||
@@ -63,7 +73,21 @@ func CreateArticle(c *gin.Context) {
|
||||
}
|
||||
}()
|
||||
|
||||
// 1. 创建文章主记录
|
||||
// 1. 检查文章发布者是否存在
|
||||
var userCheck UserInfo
|
||||
if err := tx.Table("user_info").
|
||||
Where("uid = ?", req.PublishUserID).
|
||||
First(&userCheck).Error; err != nil {
|
||||
tx.Rollback()
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "文章发布者UID不存在"})
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "查询用户失败: " + err.Error()})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 创建文章主记录
|
||||
newArticle := ArticleList{
|
||||
PublishUserID: req.PublishUserID,
|
||||
Title: req.Title,
|
||||
@@ -73,53 +97,58 @@ func CreateArticle(c *gin.Context) {
|
||||
TotalVotersNum: 0,
|
||||
CreateTime: time.Now(),
|
||||
}
|
||||
|
||||
// 插入文章并获取ID(使用GORM的Create方法,自动填充自增ID)
|
||||
if err := tx.Create(&newArticle).Error; err != nil {
|
||||
tx.Rollback()
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "创建文章失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 验证文章ID是否有效(必须大于0)
|
||||
if newArticle.ArticleID <= 0 {
|
||||
tx.Rollback()
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "无法获取文章ID,创建失败"})
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 批量创建选项(确保vote_article_id正确关联)
|
||||
// 3. 批量创建选项
|
||||
var options []map[string]interface{}
|
||||
for _, opt := range req.Options {
|
||||
options = append(options, map[string]interface{}{
|
||||
"vote_article_id": newArticle.ArticleID, // 使用刚创建的文章ID
|
||||
"vote_article_id": newArticle.ArticleID,
|
||||
"option_content": opt.Content,
|
||||
"option_votes_num": 0,
|
||||
"sort_order": opt.SortOrder,
|
||||
})
|
||||
}
|
||||
|
||||
// 插入选项前先验证文章ID是否存在(额外保险)
|
||||
var count int64
|
||||
if err := tx.Model(&ArticleList{}).Where("articleId = ?", newArticle.ArticleID).Count(&count).Error; err != nil {
|
||||
tx.Rollback()
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "验证文章ID失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
if count == 0 {
|
||||
tx.Rollback()
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "文章ID不存在,外键约束失败"})
|
||||
return
|
||||
}
|
||||
|
||||
// 批量插入选项
|
||||
if err := tx.Table("article_options").CreateInBatches(options, len(options)).Error; err != nil {
|
||||
tx.Rollback()
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "创建选项失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 提交事务
|
||||
// 4. 记录文章发布者积分变动
|
||||
userPoint := UserPoint{
|
||||
UserID: req.PublishUserID,
|
||||
PointsChange: POST_ARTICLE_POINTS,
|
||||
Source: "publish_article",
|
||||
CreateTime: time.Now(),
|
||||
}
|
||||
if err := tx.Table("user_points").Create(&userPoint).Error; err != nil {
|
||||
tx.Rollback()
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "记录积分变动失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 5. 更新 user_info 表中的 total_points 字段
|
||||
// 使用 UpdateColumn 来确保原子性,并避免 GORM 的其他回调
|
||||
if err := tx.Table("user_info").
|
||||
Where("uid = ?", req.PublishUserID).
|
||||
UpdateColumn("total_points", gorm.Expr("total_points + ?", POST_ARTICLE_POINTS)).Error; err != nil {
|
||||
tx.Rollback()
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "更新用户总积分失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 6. 提交事务
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
tx.Rollback()
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "提交数据失败: " + err.Error()})
|
||||
@@ -128,7 +157,7 @@ func CreateArticle(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "文章及选项创建成功",
|
||||
"message": "文章及选项创建成功,您获得了" + fmt.Sprintf("%d", POST_ARTICLE_POINTS) + "积分",
|
||||
"data": gin.H{
|
||||
"article_id": newArticle.ArticleID,
|
||||
"option_count": len(req.Options),
|
||||
|
||||
Reference in New Issue
Block a user