109 lines
3.6 KiB
Go
109 lines
3.6 KiB
Go
package article
|
||
|
||
import (
|
||
"fmt"
|
||
"net/http"
|
||
"toutoukan/init/databaseInit"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// ArticleOption 文评选项结构(增加了ID字段)
|
||
type ArticleOption struct {
|
||
ID int64 `json:"id"` // 选项ID
|
||
Name string `json:"name"` // 选项名称
|
||
Votes int `json:"votes"` // 该选项的投票数
|
||
}
|
||
|
||
// 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:"选项"` // 按顺序排列的选项列表
|
||
}
|
||
|
||
// ArticleList 获取所有文评及选项信息(带排序)
|
||
func ArticleListget(c *gin.Context) {
|
||
// 1. 查询所有文评,获取表中所有字段
|
||
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").
|
||
Find(&articles).Error; err != nil {
|
||
c.JSON(http.StatusInternalServerError, gin.H{
|
||
"error": "查询文评失败: " + err.Error(),
|
||
})
|
||
return
|
||
}
|
||
|
||
fmt.Println("所有文评列表:", articles)
|
||
|
||
// 2. 构建结果映射
|
||
result := make(map[string]interface{})
|
||
|
||
// 3. 逐个查询每个文评的选项
|
||
for _, article := range articles {
|
||
var options []struct {
|
||
ID int64 `gorm:"column:id"` // 新增:获取选项ID
|
||
OptionName string `gorm:"column:option_content"`
|
||
VoteCount int `gorm:"column:option_votes_num"`
|
||
SortOrder int `gorm:"column:sort_order"` // 用于排序的字段
|
||
}
|
||
|
||
// 查询当前文评的所有选项,并按sort_order排序
|
||
if err := databaseInit.UserDB.Table("article_options").
|
||
Where("vote_article_id = ?", article.ArticleID).
|
||
Select("id, option_content, option_votes_num, sort_order"). // 新增:选择id字段
|
||
Order("sort_order ASC").
|
||
Find(&options).Error; err != nil {
|
||
c.JSON(http.StatusInternalServerError, gin.H{
|
||
"error": "查询文评选项失败: " + err.Error(),
|
||
})
|
||
return
|
||
}
|
||
|
||
// 4. 格式化选项数据为有序切片(包含ID)
|
||
var optionList []ArticleOption
|
||
for _, opt := range options {
|
||
optionList = append(optionList, ArticleOption{
|
||
ID: opt.ID, // 新增:赋值选项ID
|
||
Name: opt.OptionName,
|
||
Votes: opt.VoteCount,
|
||
})
|
||
}
|
||
|
||
// 5. 组装单个文评的响应数据
|
||
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,
|
||
}
|
||
|
||
// 6. 加入结果集
|
||
result[article.Title] = articleData
|
||
}
|
||
|
||
c.JSON(http.StatusOK, result)
|
||
}
|