修改文评功能
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package article
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
"toutoukan/init/databaseInit"
|
||||
@@ -9,7 +10,7 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 投票请求结构体,同时支持单选(optionId)和多选(optionIds)
|
||||
// VoteArticleRequest 投票请求结构体,同时支持单选(optionId)和多选(optionIds)
|
||||
type VoteArticleRequest struct {
|
||||
Uid string `json:"uid" binding:"required,min=1,max=50"` // 用户ID
|
||||
ArticleID int64 `json:"articleId" binding:"required,min=1"` // 文章ID
|
||||
@@ -17,7 +18,7 @@ type VoteArticleRequest struct {
|
||||
OptionIDs []int64 `json:"optionIds" binding:"omitempty,dive,min=1"` // 多个选项ID(多选时使用)
|
||||
}
|
||||
|
||||
// 用户投票记录表结构
|
||||
// UserVote 用户投票记录表结构
|
||||
type UserVote struct {
|
||||
UserID string `gorm:"column:user_id"`
|
||||
VoteArticleID int64 `gorm:"column:vote_article_id"`
|
||||
@@ -25,7 +26,22 @@ type UserVote struct {
|
||||
VoteTime time.Time `gorm:"column:vote_time"`
|
||||
}
|
||||
|
||||
// 用户积分记录表结构
|
||||
type UserPoint struct {
|
||||
UserID string `gorm:"column:user_id"`
|
||||
PointsChange int `gorm:"column:points_change"`
|
||||
Source string `gorm:"column:source"`
|
||||
CreateTime time.Time `gorm:"column:create_time"`
|
||||
}
|
||||
|
||||
// UserInfo 用户信息表结构,用于更新 total_points
|
||||
func VoteArticle(c *gin.Context) {
|
||||
const (
|
||||
// 定义积分常量
|
||||
VOTER_POINTS = 1 // 投票者获得的积分
|
||||
POSTER_POINTS = 3 // 文章发布者获得的积分
|
||||
)
|
||||
|
||||
var req VoteArticleRequest
|
||||
|
||||
// 1. 解析并验证请求参数
|
||||
@@ -55,7 +71,8 @@ func VoteArticle(c *gin.Context) {
|
||||
var articleStatus struct {
|
||||
IsEnded bool `gorm:"column:is_ended"`
|
||||
EndTime time.Time `gorm:"column:end_time"`
|
||||
VoteType string `gorm:"column:vote_type"` // 投票类型:single-单选,multiple-多选
|
||||
VoteType string `gorm:"column:vote_type"` // 投票类型:single-单选,multiple-多选
|
||||
AuthorID string `gorm:"column:publish_user_id"` // 文章发布者ID
|
||||
}
|
||||
if err := tx.Table("article_list").
|
||||
Where("articleId = ?", req.ArticleID).
|
||||
@@ -166,6 +183,53 @@ func VoteArticle(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 7.4 记录用户积分变动
|
||||
var userPoints []UserPoint
|
||||
// 投票者积分
|
||||
userPoints = append(userPoints, UserPoint{
|
||||
UserID: req.Uid,
|
||||
PointsChange: VOTER_POINTS,
|
||||
Source: "vote_article",
|
||||
CreateTime: voteTime,
|
||||
})
|
||||
|
||||
// 文章发布者积分,如果不是自己给自己投票
|
||||
if articleStatus.AuthorID != req.Uid {
|
||||
userPoints = append(userPoints, UserPoint{
|
||||
UserID: articleStatus.AuthorID,
|
||||
PointsChange: POSTER_POINTS,
|
||||
Source: "article_voted",
|
||||
CreateTime: voteTime,
|
||||
})
|
||||
}
|
||||
if err := tx.Table("user_points").Create(&userPoints).Error; err != nil {
|
||||
tx.Rollback()
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "记录积分变动失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 7.5 更新user_info表中的total_points字段
|
||||
// 这两个更新操作都是原子性的,可以保证数据一致性
|
||||
// 更新投票者总积分
|
||||
if err := tx.Table("user_info").
|
||||
Where("uid = ?", req.Uid).
|
||||
Update("total_points", gorm.Expr("total_points + ?", VOTER_POINTS)).Error; err != nil {
|
||||
tx.Rollback()
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "更新投票者总积分失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 更新文章发布者总积分(如果不是自己给自己投票)
|
||||
if articleStatus.AuthorID != req.Uid {
|
||||
if err := tx.Table("user_info").
|
||||
Where("uid = ?", articleStatus.AuthorID).
|
||||
Update("total_points", gorm.Expr("total_points + ?", POSTER_POINTS)).Error; err != nil {
|
||||
tx.Rollback()
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "更新文章发布者总积分失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 8. 提交事务
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
tx.Rollback()
|
||||
@@ -186,7 +250,7 @@ func VoteArticle(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "投票成功",
|
||||
"message": "投票成功,您获得了" + fmt.Sprintf("%d", VOTER_POINTS) + "积分",
|
||||
"data": responseData,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user