Skip to content

Commit

Permalink
feat: get单个稿件的接口
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Apr 12, 2024
1 parent 1973af1 commit 930a2f4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
31 changes: 31 additions & 0 deletions backend/controller/postapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"bytes"
"strconv"

"github.com/RockChinQ/Campux/backend/service"
"github.com/gin-gonic/gin"
Expand All @@ -26,6 +27,7 @@ func NewPostRouter(rg *gin.RouterGroup, ps service.PostService) *PostRouter {
group.GET("/download-image/:key", pr.DownloadImage)
group.POST("/get-self-posts", pr.GetSelfPosts)
group.POST("/get-posts", pr.GetPosts)
group.GET("/get-post-info/:id", pr.GetPostInfo)

return pr
}
Expand Down Expand Up @@ -183,3 +185,32 @@ func (pr *PostRouter) GetPosts(c *gin.Context) {
"list": posts,
})
}

func (pr *PostRouter) GetPostInfo(c *gin.Context) {
_, err := pr.GetUin(c)

if err != nil {
pr.StatusCode(c, 401, err.Error())
return
}

id := c.Param("id")

idInt, err := strconv.Atoi(id)

if err != nil {
pr.Fail(c, 1, err.Error())
return
}

post, err := pr.PostService.GetPost(idInt)

if err != nil {
pr.Fail(c, 1, err.Error())
return
}

pr.Success(c, gin.H{
"post": post,
})
}
16 changes: 16 additions & 0 deletions backend/database/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,19 @@ func (m *MongoDBManager) GetPosts(

return posts, nil
}

func (m *MongoDBManager) GetPost(id int) (*PostPO, error) {
var post PostPO
err := m.Client.Database(viper.GetString("database.mongo.db")).Collection(POST_COLLECTION).FindOne(
context.TODO(),
bson.M{"id": id},
).Decode(&post)
if err != nil {
if err == mongo.ErrNoDocuments {
return nil, nil
} else {
return nil, err
}
}
return &post, nil
}
7 changes: 6 additions & 1 deletion backend/service/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ func (ps *PostService) PostNew(uuid string, uin int64, text string, images []str
// 获取用户的帖子
func (ps *PostService) GetPosts(uin int64, status database.PostStatus, timeOrder int, page, pageSize int) ([]database.PostPO, error) {
return ps.DB.GetPosts(uin, status, timeOrder, page, pageSize)
}
}

// 获取单个稿件信息
func (ps *PostService) GetPost(id int) (*database.PostPO, error) {
return ps.DB.GetPost(id)
}

0 comments on commit 930a2f4

Please sign in to comment.