修改文评功能
This commit is contained in:
@@ -3,37 +3,64 @@ package article
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
"toutoukan/init/databaseInit"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ArticleList 数据库文章记录结构体
|
||||
// 添加了与 ArticleOption 的关联
|
||||
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"`
|
||||
// 这是关键:定义 Has Many 关联,表示一篇文章有多个选项
|
||||
Options []ArticleOption `gorm:"foreignKey:VoteArticleID;references:ArticleID"`
|
||||
}
|
||||
|
||||
// ArticleOption 文评选项结构
|
||||
type ArticleOption struct {
|
||||
ID int64 `json:"id"` // 选项ID
|
||||
Name string `json:"name"` // 选项名称
|
||||
Votes int `json:"votes"` // 该选项的投票数
|
||||
IsVotedByUser bool `json:"is_voted"` // 当前用户是否投了该选项
|
||||
ID int64 `gorm:"column:id"`
|
||||
VoteArticleID int64 `gorm:"column:vote_article_id"` // 外键,关联 ArticleList
|
||||
OptionContent string `gorm:"column:option_content"`
|
||||
OptionVotesNum int `gorm:"column:option_votes_num"`
|
||||
SortOrder int `gorm:"column:sort_order"`
|
||||
}
|
||||
|
||||
// UserVote 用户投票记录表结构
|
||||
|
||||
// ArticleOptionResp 格式化后的选项响应结构
|
||||
type ArticleOptionResp struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Votes int `json:"votes"`
|
||||
IsVotedByUser bool `json:"is_voted"`
|
||||
}
|
||||
|
||||
// ArticleResponse 单个文评的响应结构
|
||||
type ArticleResponse struct {
|
||||
ID int64 `json:"文评ID"` // 文评唯一ID
|
||||
Title string `json:"文评标题"` // 文评标题
|
||||
VoteType string `json:"投票类型"` // 投票类型
|
||||
TotalVoters int `json:"总投票人数"` // 总投票人数
|
||||
EndTime string `json:"结束时间"` // 结束时间
|
||||
IsEnded bool `json:"是否结束"` // 是否结束
|
||||
PublisherID string `json:"发布者ID"` // 发布者ID
|
||||
CreateTime string `json:"创建时间"` // 创建时间
|
||||
Options []ArticleOption `json:"选项"` // 按顺序排列的选项列表
|
||||
UserHasVoted bool `json:"用户是否已投票"` // 当前用户是否投过票
|
||||
VotedOptionIDs []int64 `json:"用户投票的选项ID"` // 若已投票,记录所有选项ID(未投票为空数组)
|
||||
ID int64 `json:"文评ID"`
|
||||
Title string `json:"文评标题"`
|
||||
VoteType string `json:"投票类型"`
|
||||
TotalVoters int `json:"总投票人数"`
|
||||
EndTime string `json:"结束时间"`
|
||||
IsEnded bool `json:"是否结束"`
|
||||
PublisherID string `json:"发布者ID"`
|
||||
CreateTime string `json:"创建时间"`
|
||||
Options []ArticleOptionResp `json:"选项"`
|
||||
UserHasVoted bool `json:"用户是否已投票"`
|
||||
VotedOptionIDs []int64 `json:"用户投票的选项ID"`
|
||||
}
|
||||
|
||||
type UserReq struct {
|
||||
Uid string `json:"uid"`
|
||||
Uid string `json:"uid" binding:"required"`
|
||||
}
|
||||
|
||||
// ArticleListget 获取所有文评及选项信息(包含用户投票状态)
|
||||
@@ -49,20 +76,12 @@ func ArticleListget(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 查询所有文评
|
||||
var articles []struct {
|
||||
ArticleID int64 `gorm:"column:articleId"`
|
||||
Title string `gorm:"column:title"`
|
||||
VoteType string `gorm:"column:vote_type"`
|
||||
TotalVotersNum int `gorm:"column:total_voters_num"`
|
||||
EndTime string `gorm:"column:end_time"`
|
||||
IsEnded bool `gorm:"column:is_ended"`
|
||||
PublishUserID string `gorm:"column:publish_user_id"`
|
||||
CreateTime string `gorm:"column:create_time"`
|
||||
}
|
||||
|
||||
if err := databaseInit.UserDB.Table("article_list").
|
||||
Select("articleId, title, vote_type, total_voters_num, end_time, is_ended, publish_user_id, create_time").
|
||||
// 2. 使用 Preload 一次性查询所有文章和它们的选项
|
||||
var articles []ArticleList
|
||||
if err := databaseInit.UserDB.Model(&ArticleList{}).
|
||||
Preload("Options", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("sort_order ASC")
|
||||
}).
|
||||
Find(&articles).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "查询文评失败: " + err.Error(),
|
||||
@@ -70,62 +89,28 @@ func ArticleListget(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("所有文评列表:", articles)
|
||||
// 3. 一次性查询当前用户的所有投票记录,并构建一个 map 方便查找
|
||||
var userAllVotes []UserVote
|
||||
if err := databaseInit.UserDB.Table("user_votes").Where("user_id = ?", req.Uid).Find(&userAllVotes).Error; err != nil && err != gorm.ErrRecordNotFound {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "查询用户投票记录失败: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
// 投票记录 map:key: articleId, value: []optionId
|
||||
userVotesMap := make(map[int64][]int64)
|
||||
for _, vote := range userAllVotes {
|
||||
userVotesMap[vote.VoteArticleID] = append(userVotesMap[vote.VoteArticleID], vote.OptionID)
|
||||
}
|
||||
|
||||
// 3. 构建结果映射
|
||||
result := make(map[string]interface{})
|
||||
|
||||
// 4. 逐个处理文评
|
||||
// 4. 组装最终响应数据
|
||||
var responseList []ArticleResponse
|
||||
for _, article := range articles {
|
||||
// 4.1 查询当前文评的选项
|
||||
var options []struct {
|
||||
ID int64 `gorm:"column:id"`
|
||||
OptionName string `gorm:"column:option_content"`
|
||||
VoteCount int `gorm:"column:option_votes_num"`
|
||||
SortOrder int `gorm:"column:sort_order"`
|
||||
}
|
||||
votedOptionIDs, userHasVoted := userVotesMap[article.ArticleID]
|
||||
|
||||
if err := databaseInit.UserDB.Table("article_options").
|
||||
Where("vote_article_id = ?", article.ArticleID).
|
||||
Select("id, option_content, option_votes_num, sort_order").
|
||||
Order("sort_order ASC").
|
||||
Find(&options).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "查询文评选项失败: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 4.2 查询当前用户对该文评的所有投票记录
|
||||
var userVotes []struct {
|
||||
OptionID int64 `gorm:"column:option_id"`
|
||||
}
|
||||
voteErr := databaseInit.UserDB.Table("user_votes").
|
||||
Where("user_id = ? AND vote_article_id = ?", req.Uid, article.ArticleID).
|
||||
Find(&userVotes).Error
|
||||
|
||||
// 标记用户是否投票及所有投票的选项ID
|
||||
userHasVoted := false
|
||||
var votedOptionIDs []int64 = []int64{}
|
||||
|
||||
if voteErr == nil && len(userVotes) > 0 {
|
||||
userHasVoted = true
|
||||
// 收集所有投票的选项ID
|
||||
for _, vote := range userVotes {
|
||||
votedOptionIDs = append(votedOptionIDs, vote.OptionID)
|
||||
}
|
||||
} else if voteErr != nil && voteErr != gorm.ErrRecordNotFound {
|
||||
// 处理查询错误(非"未找到"的错误)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "查询用户投票记录失败: " + voteErr.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 4.3 格式化选项数据(标记用户是否投了该选项)
|
||||
var optionList []ArticleOption
|
||||
for _, opt := range options {
|
||||
// 检查当前选项是否是用户投票的选项之一
|
||||
// 格式化选项数据
|
||||
var optionList []ArticleOptionResp
|
||||
for _, opt := range article.Options {
|
||||
isVoted := false
|
||||
for _, votedID := range votedOptionIDs {
|
||||
if opt.ID == votedID {
|
||||
@@ -133,33 +118,33 @@ func ArticleListget(c *gin.Context) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
optionList = append(optionList, ArticleOption{
|
||||
optionList = append(optionList, ArticleOptionResp{
|
||||
ID: opt.ID,
|
||||
Name: opt.OptionName,
|
||||
Votes: opt.VoteCount,
|
||||
IsVotedByUser: isVoted, // 标记当前用户是否投了这个选项
|
||||
Name: opt.OptionContent,
|
||||
Votes: opt.OptionVotesNum,
|
||||
IsVotedByUser: isVoted,
|
||||
})
|
||||
}
|
||||
|
||||
// 4.4 组装单个文评的响应数据
|
||||
articleData := ArticleResponse{
|
||||
ID: article.ArticleID,
|
||||
Title: article.Title,
|
||||
VoteType: article.VoteType,
|
||||
TotalVoters: article.TotalVotersNum,
|
||||
EndTime: article.EndTime,
|
||||
EndTime: article.EndTime.Format("2006-01-02 15:04:05"),
|
||||
IsEnded: article.IsEnded,
|
||||
PublisherID: article.PublishUserID,
|
||||
CreateTime: article.CreateTime,
|
||||
CreateTime: article.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
Options: optionList,
|
||||
UserHasVoted: userHasVoted, // 整体标记用户是否投过票
|
||||
VotedOptionIDs: votedOptionIDs, // 记录用户投票的所有选项ID(未投票则为空数组)
|
||||
UserHasVoted: userHasVoted,
|
||||
VotedOptionIDs: votedOptionIDs,
|
||||
}
|
||||
|
||||
// 4.5 加入结果集
|
||||
result[article.Title] = articleData
|
||||
responseList = append(responseList, articleData)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": fmt.Sprintf("查询成功,共 %d 篇文章", len(responseList)),
|
||||
"data": responseList,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user