Skip to content

Commit

Permalink
feat: 稿件取消接口
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Apr 15, 2024
1 parent 94cc9e3 commit 0390af8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
4 changes: 4 additions & 0 deletions backend/controller/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ type GetPostsBody struct {

GetSelfPostsBody
}

type UserCancelPostBody struct {
PostID *int `json:"post_id" binding:"required"`
}
30 changes: 30 additions & 0 deletions backend/controller/postapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewPostRouter(rg *gin.RouterGroup, ps service.PostService) *PostRouter {
group.POST("/get-self-posts", pr.GetSelfPosts)
group.POST("/get-posts", pr.GetPosts)
group.GET("/get-post-info/:id", pr.GetPostInfo)
group.POST("/user-cancel", pr.UserCancelPost)

return pr
}
Expand Down Expand Up @@ -214,3 +215,32 @@ func (pr *PostRouter) GetPostInfo(c *gin.Context) {
"post": post,
})
}

// 取消投稿
func (pr *PostRouter) UserCancelPost(c *gin.Context) {
// 从jwt取uin
uin, err := pr.GetUin(c)

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

// 取body的json里的id
var body UserCancelPostBody

if err := c.ShouldBindJSON(&body); err != nil {
pr.Fail(c, 1, err.Error())
return
}

// 用户取消投稿
err = pr.PostService.UserCancelPost(uin, *body.PostID)

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

pr.Success(c, gin.H{})
}
9 changes: 9 additions & 0 deletions backend/database/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,12 @@ func (m *MongoDBManager) GetPost(id int) (*PostPO, error) {
}
return &post, nil
}

func (m *MongoDBManager) UpdatePostStatus(id int, status PostStatus) error {
_, err := m.Client.Database(viper.GetString("database.mongo.db")).Collection(POST_COLLECTION).UpdateOne(
context.TODO(),
bson.M{"id": id},
bson.M{"$set": bson.M{"status": status}},
)
return err
}
37 changes: 37 additions & 0 deletions backend/service/post.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"errors"
"io"

"github.com/RockChinQ/Campux/backend/database"
Expand Down Expand Up @@ -89,3 +90,39 @@ func (ps *PostService) GetPosts(uin int64, status database.PostStatus, timeOrder
func (ps *PostService) GetPost(id int) (*database.PostPO, error) {
return ps.DB.GetPost(id)
}

// 用户取消投稿
func (ps *PostService) UserCancelPost(uin int64, id int) error {
// 检查状态是不是 待审核
post, err := ps.DB.GetPost(id)

if err != nil {
return nil
}

if post.Status != database.POST_STATUS_PENDING_APPROVAL {
return errors.New("稿件的状态不是 未审核")
}

if post.Uin != uin {
return errors.New("无权操作他人稿件")
}

// 记录日志
err = ps.DB.AddPostLog(
&database.PostLogPO{
PostID: id,
Op: uin,
OldStat: database.POST_STATUS_PENDING_APPROVAL,
NewStat: database.POST_STATUS_CANCELLED,
Comment: "用户取消投稿",
CreatedAt: util.GetCSTTime(),
},
)

if err != nil {
return err
}

return ps.DB.UpdatePostStatus(id, database.POST_STATUS_CANCELLED)
}

0 comments on commit 0390af8

Please sign in to comment.