修改投票和get逻辑
This commit is contained in:
@@ -19,17 +19,17 @@ type ArticleOption struct {
|
||||
|
||||
// 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:"用户是否已投票"` // 当前用户是否投过票
|
||||
VotedOptionID *int64 `json:"用户投票的选项ID"` // 若已投票,记录选项ID(未投票为null)
|
||||
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(未投票为空数组)
|
||||
}
|
||||
|
||||
type UserReq struct {
|
||||
@@ -96,21 +96,25 @@ func ArticleListget(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 4.2 查询当前用户对该文评的投票记录
|
||||
var userVote struct {
|
||||
// 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).
|
||||
First(&userVote).Error
|
||||
Find(&userVotes).Error
|
||||
|
||||
// 标记用户是否投票及投票的选项ID
|
||||
// 标记用户是否投票及所有投票的选项ID
|
||||
userHasVoted := false
|
||||
var votedOptionID *int64 = nil
|
||||
if voteErr == nil {
|
||||
var votedOptionIDs []int64 = []int64{}
|
||||
|
||||
if voteErr == nil && len(userVotes) > 0 {
|
||||
userHasVoted = true
|
||||
votedOptionID = &userVote.OptionID
|
||||
} else if voteErr != gorm.ErrRecordNotFound {
|
||||
// 收集所有投票的选项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(),
|
||||
@@ -121,8 +125,14 @@ func ArticleListget(c *gin.Context) {
|
||||
// 4.3 格式化选项数据(标记用户是否投了该选项)
|
||||
var optionList []ArticleOption
|
||||
for _, opt := range options {
|
||||
// 检查当前选项是否是用户投票的选项
|
||||
isVoted := userHasVoted && (opt.ID == *votedOptionID)
|
||||
// 检查当前选项是否是用户投票的选项之一
|
||||
isVoted := false
|
||||
for _, votedID := range votedOptionIDs {
|
||||
if opt.ID == votedID {
|
||||
isVoted = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
optionList = append(optionList, ArticleOption{
|
||||
ID: opt.ID,
|
||||
@@ -134,17 +144,17 @@ func ArticleListget(c *gin.Context) {
|
||||
|
||||
// 4.4 组装单个文评的响应数据
|
||||
articleData := ArticleResponse{
|
||||
ID: article.ArticleID,
|
||||
Title: article.Title,
|
||||
VoteType: article.VoteType,
|
||||
TotalVoters: article.TotalVotersNum,
|
||||
EndTime: article.EndTime,
|
||||
IsEnded: article.IsEnded,
|
||||
PublisherID: article.PublishUserID,
|
||||
CreateTime: article.CreateTime,
|
||||
Options: optionList,
|
||||
UserHasVoted: userHasVoted, // 整体标记用户是否投过票
|
||||
VotedOptionID: votedOptionID, // 记录用户投票的选项ID(未投票则为null)
|
||||
ID: article.ArticleID,
|
||||
Title: article.Title,
|
||||
VoteType: article.VoteType,
|
||||
TotalVoters: article.TotalVotersNum,
|
||||
EndTime: article.EndTime,
|
||||
IsEnded: article.IsEnded,
|
||||
PublisherID: article.PublishUserID,
|
||||
CreateTime: article.CreateTime,
|
||||
Options: optionList,
|
||||
UserHasVoted: userHasVoted, // 整体标记用户是否投过票
|
||||
VotedOptionIDs: votedOptionIDs, // 记录用户投票的所有选项ID(未投票则为空数组)
|
||||
}
|
||||
|
||||
// 4.5 加入结果集
|
||||
|
||||
Reference in New Issue
Block a user