130 lines
3.0 KiB
Go
130 lines
3.0 KiB
Go
package controller
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"hepa-calc-api/api/dao"
|
|
"hepa-calc-api/api/dto"
|
|
"hepa-calc-api/api/requests"
|
|
"hepa-calc-api/api/responses"
|
|
"hepa-calc-api/api/service"
|
|
"hepa-calc-api/global"
|
|
"hepa-calc-api/utils"
|
|
"strconv"
|
|
)
|
|
|
|
type Question struct{}
|
|
|
|
// GetQuestionPage 获取问题列表-分页
|
|
func (b *Question) GetQuestionPage(c *gin.Context) {
|
|
questionRequest := requests.QuestionRequest{}
|
|
req := questionRequest.GetQuestionPage
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
if req.Page == 0 {
|
|
req.Page = 1
|
|
}
|
|
|
|
if req.PageSize == 0 {
|
|
req.PageSize = 20
|
|
}
|
|
|
|
// 获取数据
|
|
questionDao := dao.QuestionDao{}
|
|
question, total, err := questionDao.GetQuestionPageSearch(req, req.Page, req.PageSize)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
g := dto.GetQuestionPageListDto(question)
|
|
|
|
result := make(map[string]interface{})
|
|
result["page"] = req.Page
|
|
result["page_size"] = req.PageSize
|
|
result["total"] = total
|
|
result["data"] = g
|
|
responses.OkWithData(result, c)
|
|
}
|
|
|
|
// GetQuestionHot 获取问题列表-热榜
|
|
func (b *Question) GetQuestionHot(c *gin.Context) {
|
|
// 获取数据
|
|
questionService := service.QuestionService{}
|
|
|
|
// 获取算一算热榜-人气数最高的9个
|
|
hotQuestions, err := questionService.GetHotList()
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
g := dto.GetHotQuestionListDto(hotQuestions)
|
|
|
|
responses.OkWithData(g, c)
|
|
}
|
|
|
|
// GetQuestion 获取问题详情
|
|
func (r *Question) GetQuestion(c *gin.Context) {
|
|
userId := c.GetInt64("UserId")
|
|
|
|
id := c.Param("question_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
questionId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 获取数据
|
|
questionDao := dao.QuestionDao{}
|
|
question, err := questionDao.GetQuestionById(questionId)
|
|
if err != nil {
|
|
responses.FailWithMessage("题目错误", c)
|
|
return
|
|
}
|
|
|
|
// 检测问题是否被用户收藏
|
|
userCollectionService := service.UserCollectionService{}
|
|
IsCollection := userCollectionService.CheckUserCollectionQuestion(userId, questionId)
|
|
|
|
// 获取用户首次购买价格
|
|
questionService := service.QuestionService{}
|
|
firstTimePrice, err := questionService.GetUserFirstTimeBuyPrice(userId, questionId)
|
|
if err != nil {
|
|
responses.FailWithMessage("题目错误", c)
|
|
return
|
|
}
|
|
|
|
// 获取问题被购买数量
|
|
buyCount, err := questionService.GetQuestionBuyCount(userId, questionId)
|
|
|
|
g := dto.GetQuestionDto(question)
|
|
|
|
// 加载数据-是否收藏
|
|
g.LoadIsCollection(IsCollection)
|
|
|
|
// 加载数据-首次购买价格
|
|
g.LoadFirstTimePrice(firstTimePrice)
|
|
|
|
// 加载数据-问题被购买数量
|
|
g.LoadBuyCount(buyCount)
|
|
|
|
responses.OkWithData(g, c)
|
|
}
|