修改getarticle
This commit is contained in:
@@ -11,7 +11,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ArticleList 数据库文章记录结构体
|
// ArticleList 数据库文章记录结构体
|
||||||
// 添加了与 ArticleOption 的关联
|
|
||||||
type ArticleList struct {
|
type ArticleList struct {
|
||||||
ArticleID int64 `gorm:"column:articleId;primaryKey;autoIncrement"`
|
ArticleID int64 `gorm:"column:articleId;primaryKey;autoIncrement"`
|
||||||
PublishUserID string `gorm:"column:publish_user_id"`
|
PublishUserID string `gorm:"column:publish_user_id"`
|
||||||
@@ -21,21 +20,18 @@ type ArticleList struct {
|
|||||||
IsEnded bool `gorm:"column:is_ended"`
|
IsEnded bool `gorm:"column:is_ended"`
|
||||||
TotalVotersNum int `gorm:"column:total_voters_num"`
|
TotalVotersNum int `gorm:"column:total_voters_num"`
|
||||||
CreateTime time.Time `gorm:"column:create_time"`
|
CreateTime time.Time `gorm:"column:create_time"`
|
||||||
// 这是关键:定义 Has Many 关联,表示一篇文章有多个选项
|
|
||||||
Options []ArticleOption `gorm:"foreignKey:VoteArticleID;references:ArticleID"`
|
Options []ArticleOption `gorm:"foreignKey:VoteArticleID;references:ArticleID"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ArticleOption 文评选项结构
|
// ArticleOption 文评选项结构
|
||||||
type ArticleOption struct {
|
type ArticleOption struct {
|
||||||
ID int64 `gorm:"column:id"`
|
ID int64 `gorm:"column:id"`
|
||||||
VoteArticleID int64 `gorm:"column:vote_article_id"` // 外键,关联 ArticleList
|
VoteArticleID int64 `gorm:"column:vote_article_id"`
|
||||||
OptionContent string `gorm:"column:option_content"`
|
OptionContent string `gorm:"column:option_content"`
|
||||||
OptionVotesNum int `gorm:"column:option_votes_num"`
|
OptionVotesNum int `gorm:"column:option_votes_num"`
|
||||||
SortOrder int `gorm:"column:sort_order"`
|
SortOrder int `gorm:"column:sort_order"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserVote 用户投票记录表结构
|
|
||||||
|
|
||||||
// ArticleOptionResp 格式化后的选项响应结构
|
// ArticleOptionResp 格式化后的选项响应结构
|
||||||
type ArticleOptionResp struct {
|
type ArticleOptionResp struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
@@ -59,15 +55,16 @@ type ArticleResponse struct {
|
|||||||
VotedOptionIDs []int64 `json:"user_voted_option_ids"`
|
VotedOptionIDs []int64 `json:"user_voted_option_ids"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UserReq 请求参数结构体,允许uid为空
|
||||||
type UserReq struct {
|
type UserReq struct {
|
||||||
Uid string `json:"uid" binding:"required"`
|
Uid string `json:"uid" binding:"omitempty"` // 允许uid为空,空字符串也合法
|
||||||
}
|
}
|
||||||
|
|
||||||
// ArticleListget 获取所有文评及选项信息(包含用户投票状态)
|
// ArticleListget 获取所有文评及选项信息(支持空uid场景)
|
||||||
func ArticleListget(c *gin.Context) {
|
func ArticleListget(c *gin.Context) {
|
||||||
var req UserReq
|
var req UserReq
|
||||||
|
|
||||||
// 1. 解析并验证请求参数
|
// 1. 解析请求参数,uid为空也能通过校验
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
"error": "参数解析失败",
|
"error": "参数解析失败",
|
||||||
@@ -76,7 +73,7 @@ func ArticleListget(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 使用 Preload 一次性查询所有文章和它们的选项
|
// 2. 查询所有文章及关联的选项
|
||||||
var articles []ArticleList
|
var articles []ArticleList
|
||||||
if err := databaseInit.UserDB.Model(&ArticleList{}).
|
if err := databaseInit.UserDB.Model(&ArticleList{}).
|
||||||
Preload("Options", func(db *gorm.DB) *gorm.DB {
|
Preload("Options", func(db *gorm.DB) *gorm.DB {
|
||||||
@@ -89,35 +86,57 @@ func ArticleListget(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 一次性查询当前用户的所有投票记录,并构建一个 map 方便查找
|
// 3. 根据uid是否存在(包括空字符串)决定是否查询投票记录
|
||||||
|
userVotesMap := make(map[int64][]int64)
|
||||||
|
// 判断uid是否有效(非空字符串)
|
||||||
|
uidValid := req.Uid != ""
|
||||||
|
|
||||||
|
if uidValid {
|
||||||
|
// 3.1 仅当uid有效时查询用户投票记录
|
||||||
var userAllVotes []UserVote
|
var userAllVotes []UserVote
|
||||||
if err := databaseInit.UserDB.Table("user_votes").Where("user_id = ?", req.Uid).Find(&userAllVotes).Error; err != nil && err != gorm.ErrRecordNotFound {
|
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{
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
"error": "查询用户投票记录失败: " + err.Error(),
|
"error": "查询用户投票记录失败: " + err.Error(),
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 投票记录 map:key: articleId, value: []optionId
|
// 构建投票记录映射
|
||||||
userVotesMap := make(map[int64][]int64)
|
|
||||||
for _, vote := range userAllVotes {
|
for _, vote := range userAllVotes {
|
||||||
userVotesMap[vote.VoteArticleID] = append(userVotesMap[vote.VoteArticleID], vote.OptionID)
|
userVotesMap[vote.VoteArticleID] = append(userVotesMap[vote.VoteArticleID], vote.OptionID)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
// 3.2 当uid为空时,不执行任何投票查询,保持映射为空
|
||||||
|
|
||||||
// 4. 组装最终响应数据
|
// 4. 组装响应数据
|
||||||
var responseList []ArticleResponse
|
var responseList []ArticleResponse
|
||||||
for _, article := range articles {
|
for _, article := range articles {
|
||||||
votedOptionIDs, userHasVoted := userVotesMap[article.ArticleID]
|
// 处理投票状态
|
||||||
|
var votedOptionIDs []int64
|
||||||
|
var userHasVoted bool
|
||||||
|
if uidValid {
|
||||||
|
votedOptionIDs, userHasVoted = userVotesMap[article.ArticleID]
|
||||||
|
} else {
|
||||||
|
// uid为空时,默认未投票
|
||||||
|
votedOptionIDs = []int64{}
|
||||||
|
userHasVoted = false
|
||||||
|
}
|
||||||
|
|
||||||
// 格式化选项数据
|
// 处理选项列表
|
||||||
var optionList []ArticleOptionResp
|
var optionList []ArticleOptionResp
|
||||||
for _, opt := range article.Options {
|
for _, opt := range article.Options {
|
||||||
isVoted := false
|
isVoted := false
|
||||||
|
if uidValid {
|
||||||
|
// 仅在uid有效时检查是否投票
|
||||||
for _, votedID := range votedOptionIDs {
|
for _, votedID := range votedOptionIDs {
|
||||||
if opt.ID == votedID {
|
if opt.ID == votedID {
|
||||||
isVoted = true
|
isVoted = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
// uid为空时isVoted保持false
|
||||||
optionList = append(optionList, ArticleOptionResp{
|
optionList = append(optionList, ArticleOptionResp{
|
||||||
ID: opt.ID,
|
ID: opt.ID,
|
||||||
Name: opt.OptionContent,
|
Name: opt.OptionContent,
|
||||||
@@ -126,6 +145,7 @@ func ArticleListget(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 组装文章响应
|
||||||
articleData := ArticleResponse{
|
articleData := ArticleResponse{
|
||||||
ID: article.ArticleID,
|
ID: article.ArticleID,
|
||||||
Title: article.Title,
|
Title: article.Title,
|
||||||
@@ -142,6 +162,7 @@ func ArticleListget(c *gin.Context) {
|
|||||||
responseList = append(responseList, articleData)
|
responseList = append(responseList, articleData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 5. 返回响应
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": true,
|
"success": true,
|
||||||
"message": fmt.Sprintf("查询成功,共 %d 篇文章", len(responseList)),
|
"message": fmt.Sprintf("查询成功,共 %d 篇文章", len(responseList)),
|
||||||
|
|||||||
Reference in New Issue
Block a user