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 0390af8 commit f4c1491
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
12 changes: 12 additions & 0 deletions backend/controller/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
30 changes: 30 additions & 0 deletions backend/controller/postapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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{})
}
7 changes: 7 additions & 0 deletions backend/database/po.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
47 changes: 46 additions & 1 deletion backend/service/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

0 comments on commit f4c1491

Please sign in to comment.