Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Soulter committed Apr 12, 2024
2 parents 4b96edc + 930a2f4 commit 2659735
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 11 deletions.
34 changes: 24 additions & 10 deletions backend/controller/accapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,30 @@ func (ar *AccountRouter) LoginAccount(c *gin.Context) {
domain := c.Request.Header.Get("Origin")

// set-cookie
http.SetCookie(c.Writer, &http.Cookie{
Name: "access-token",
Value: token,
Path: "/",
Domain: domain,
Secure: false,
SameSite: http.SameSiteNoneMode,
HttpOnly: true,
MaxAge: 3600,
})
if gin.Mode() == gin.DebugMode {
http.SetCookie(c.Writer, &http.Cookie{
Name: "access-token",
Value: token,
Path: "/",
Domain: domain,
Secure: true,
SameSite: http.SameSiteNoneMode,
HttpOnly: true,
MaxAge: 3600,
})
} else {
// 正式环境用strict模式
http.SetCookie(c.Writer, &http.Cookie{
Name: "access-token",
Value: token,
Path: "/",
Domain: domain,
Secure: true,
SameSite: http.SameSiteStrictMode,
HttpOnly: true,
MaxAge: 3600,
})
}

ar.Success(c, gin.H{
"token": token,
Expand Down
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 2659735

Please sign in to comment.