diff --git a/controllers/article/getArticleNum/getarticlenum.go b/controllers/article/getArticleNum/getarticlenum.go new file mode 100644 index 0000000..e1d32f4 --- /dev/null +++ b/controllers/article/getArticleNum/getarticlenum.go @@ -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, + }) + +} diff --git a/router/setupRouter.go b/router/setupRouter.go index 77df8fb..0384560 100644 --- a/router/setupRouter.go +++ b/router/setupRouter.go @@ -3,6 +3,7 @@ package router import ( "github.com/gin-gonic/gin" "toutoukan/controllers/article" + "toutoukan/controllers/article/getArticleNum" "toutoukan/controllers/comments/getcomments" "toutoukan/controllers/comments/publishComments" "toutoukan/controllers/goods" @@ -42,6 +43,7 @@ func SetupRouter() *gin.Engine { articleGroup.POST("/vote", article.VoteArticle) articleGroup.POST("/create", article.CreateArticle) articleGroup.POST("/delete", article.DeleteArticle) + articleGroup.POST("/getnum", getArticleNum.GetArticlenum) } goodsGroup := r.Group("/goods")