添加查询用户发布文评数量接口

This commit is contained in:
JACKYMYPERSON
2025-09-27 18:26:29 +08:00
parent ee2bcf1988
commit 9081472c10
2 changed files with 63 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
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,
})
}

View File

@@ -3,6 +3,7 @@ package router
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"toutoukan/controllers/article" "toutoukan/controllers/article"
"toutoukan/controllers/article/getArticleNum"
"toutoukan/controllers/comments/getcomments" "toutoukan/controllers/comments/getcomments"
"toutoukan/controllers/comments/publishComments" "toutoukan/controllers/comments/publishComments"
"toutoukan/controllers/goods" "toutoukan/controllers/goods"
@@ -42,6 +43,7 @@ func SetupRouter() *gin.Engine {
articleGroup.POST("/vote", article.VoteArticle) articleGroup.POST("/vote", article.VoteArticle)
articleGroup.POST("/create", article.CreateArticle) articleGroup.POST("/create", article.CreateArticle)
articleGroup.POST("/delete", article.DeleteArticle) articleGroup.POST("/delete", article.DeleteArticle)
articleGroup.POST("/getnum", getArticleNum.GetArticlenum)
} }
goodsGroup := r.Group("/goods") goodsGroup := r.Group("/goods")