From f4c149187288aa79ec3c13ee3e4ff4e61052cfa3 Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Mon, 15 Apr 2024 14:37:04 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=A1=E6=A0=B8=E7=A8=BF=E4=BB=B6?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/controller/dto.go | 12 +++++++++ backend/controller/postapi.go | 30 ++++++++++++++++++++++ backend/database/po.go | 7 ++++++ backend/service/post.go | 47 ++++++++++++++++++++++++++++++++++- 4 files changed, 95 insertions(+), 1 deletion(-) diff --git a/backend/controller/dto.go b/backend/controller/dto.go index 3958d54..89ef8a6 100644 --- a/backend/controller/dto.go +++ b/backend/controller/dto.go @@ -59,3 +59,15 @@ type GetPostsBody struct { type UserCancelPostBody struct { PostID *int `json:"post_id" binding:"required"` } + +// 稿件审核 +type PostReviewBody struct { + // 稿件id + PostID int `json:"post_id" binding:"required"` + + // 审核选项 + Option database.ReviewOption `json:"option" binding:"required"` + + // 审核意见 + Comment *string `json:"comment" binding:"required"` +} diff --git a/backend/controller/postapi.go b/backend/controller/postapi.go index 652eb3f..6c14fb9 100644 --- a/backend/controller/postapi.go +++ b/backend/controller/postapi.go @@ -29,6 +29,7 @@ func NewPostRouter(rg *gin.RouterGroup, ps service.PostService) *PostRouter { group.POST("/get-posts", pr.GetPosts) group.GET("/get-post-info/:id", pr.GetPostInfo) group.POST("/user-cancel", pr.UserCancelPost) + group.POST("/review-post", pr.ReviewPost) return pr } @@ -244,3 +245,32 @@ func (pr *PostRouter) UserCancelPost(c *gin.Context) { pr.Success(c, gin.H{}) } + +// 提交稿件审核 +func (pr *PostRouter) ReviewPost(c *gin.Context) { + // 从jwt取uin + uin, err := pr.GetUin(c) + + if err != nil { + pr.StatusCode(c, 401, err.Error()) + return + } + + // 取body的json里的id, status, comment + var body PostReviewBody + + if err := c.ShouldBindJSON(&body); err != nil { + pr.Fail(c, 1, err.Error()) + return + } + + // 提交稿件审核 + err = pr.PostService.PostReview(uin, body.PostID, body.Option, *body.Comment) + + if err != nil { + pr.Fail(c, 1, err.Error()) + return + } + + pr.Success(c, gin.H{}) +} diff --git a/backend/database/po.go b/backend/database/po.go index 61946a9..a095959 100644 --- a/backend/database/po.go +++ b/backend/database/po.go @@ -55,3 +55,10 @@ type PostLogPO struct { Comment string `json:"comment" bson:"comment"` // 备注 CreatedAt time.Time `json:"created_at" bson:"created_at"` // CST时间 } + +type ReviewOption string + +const ( + REVIEW_OPTION_APPROVE ReviewOption = "approve" + REVIEW_OPTION_REJECT ReviewOption = "reject" +) diff --git a/backend/service/post.go b/backend/service/post.go index 35905d2..1d01102 100644 --- a/backend/service/post.go +++ b/backend/service/post.go @@ -101,7 +101,7 @@ func (ps *PostService) UserCancelPost(uin int64, id int) error { } if post.Status != database.POST_STATUS_PENDING_APPROVAL { - return errors.New("稿件的状态不是 未审核") + return errors.New("稿件的状态不是 待审核") } if post.Uin != uin { @@ -126,3 +126,48 @@ func (ps *PostService) UserCancelPost(uin int64, id int) error { return ps.DB.UpdatePostStatus(id, database.POST_STATUS_CANCELLED) } + +// 审核 +func (ps *PostService) PostReview(uin int64, id int, option database.ReviewOption, comment string) error { + // 检查状态是不是 待审核 + post, err := ps.DB.GetPost(id) + + if err != nil { + return nil + } + + if post.Status != database.POST_STATUS_PENDING_APPROVAL { + return errors.New("稿件的状态不是 待审核") + } + + newStat := database.POST_STATUS_APPROVED + + if option == database.REVIEW_OPTION_REJECT { + + if comment == "" { + return errors.New("拒绝时必须填写理由") + } + + newStat = database.POST_STATUS_REJECTED + } else if option != database.REVIEW_OPTION_APPROVE { + return errors.New("审核选项不合法") + } + + // 记录日志 + err = ps.DB.AddPostLog( + &database.PostLogPO{ + PostID: id, + Op: uin, + OldStat: database.POST_STATUS_PENDING_APPROVAL, + NewStat: newStat, + Comment: comment, + CreatedAt: util.GetCSTTime(), + }, + ) + + if err != nil { + return err + } + + return ps.DB.UpdatePostStatus(id, database.POST_STATUS_APPROVED) +}