62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package getArticleNum
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"toutoukan/init/databaseInit"
|
|
)
|
|
|
|
type Article struct {
|
|
ArticleId int64 `gorm:"column:articleId;primaryKey;autoIncrement"`
|
|
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"`
|
|
}
|
|
|
|
// 为模型指定表名
|
|
func (Article) TableName() string {
|
|
return "article_list"
|
|
}
|
|
|
|
type UserReq struct {
|
|
Uid string `json:"uid" binding:"required"`
|
|
}
|
|
|
|
func GetArticlenum(c *gin.Context) {
|
|
var req UserReq
|
|
|
|
// 1. 解析并验证请求参数
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "参数解析失败",
|
|
"detail": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// 2. 查询数据库,统计用户发布的文章数量
|
|
var count int64
|
|
result := databaseInit.UserDB.Model(&Article{}).
|
|
Where("publish_user_id = ?", req.Uid).
|
|
Count(&count)
|
|
|
|
if result.Error != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "数据库查询失败",
|
|
"detail": result.Error.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// 3. 返回结果
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"uid": req.Uid,
|
|
"article_num": count,
|
|
})
|
|
|
|
}
|