From c8a08fa3f4aabca1dc269ba4f1013ff5b467aa1e Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Fri, 12 Apr 2024 03:28:33 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8C=89=E7=85=A7=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=96=87=E6=A1=A3=20post-new=20=E5=BA=94=E8=AF=A5=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=96=B0=E7=A8=BF=E4=BB=B6id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/controller/postapi.go | 6 ++++-- backend/database/mongo.go | 10 ++++++++-- backend/database/po.go | 37 +++++++++++++++++++++++++++++------ backend/service/post.go | 32 ++++++++++++++++++++++++++---- 4 files changed, 71 insertions(+), 14 deletions(-) diff --git a/backend/controller/postapi.go b/backend/controller/postapi.go index dc41dd7..6096246 100644 --- a/backend/controller/postapi.go +++ b/backend/controller/postapi.go @@ -67,12 +67,14 @@ func (pr *PostRouter) PostNew(c *gin.Context) { } // 发布新稿件 - err = pr.PostService.PostNew(body.UUID, uin, body.Text, body.Images, *body.Anon) + id, err := pr.PostService.PostNew(body.UUID, uin, body.Text, body.Images, *body.Anon) if err != nil { pr.Fail(c, 1, err.Error()) return } - pr.Success(c, gin.H{}) + pr.Success(c, gin.H{ + "id": id, + }) } diff --git a/backend/database/mongo.go b/backend/database/mongo.go index affbf18..316c005 100644 --- a/backend/database/mongo.go +++ b/backend/database/mongo.go @@ -10,8 +10,9 @@ import ( ) const ( - ACCOUNT_COLLECTION = "account" - POST_COLLECTION = "post" + ACCOUNT_COLLECTION = "account" + POST_COLLECTION = "post" + POST_LOG_COLLECTION = "post_log" ) type MongoDBManager struct { @@ -100,3 +101,8 @@ func (m *MongoDBManager) AddPost(post *PostPO) error { _, err := m.Client.Database(viper.GetString("database.mongo.db")).Collection(POST_COLLECTION).InsertOne(context.TODO(), post) return err } + +func (m *MongoDBManager) AddPostLog(log *PostLogPO) error { + _, err := m.Client.Database(viper.GetString("database.mongo.db")).Collection(POST_LOG_COLLECTION).InsertOne(context.TODO(), log) + return err +} diff --git a/backend/database/po.go b/backend/database/po.go index 22d611e..c52efe5 100644 --- a/backend/database/po.go +++ b/backend/database/po.go @@ -20,11 +20,36 @@ type AccountPO struct { Salt string `json:"salt" bson:"salt"` // 加盐 } +type PostStatus string + +const ( + POST_STATUS_ANY PostStatus = "any" // 任何 + POST_STATUS_PENDING_APPROVAL PostStatus = "pending_approval" // 待审核 + POST_STATUS_APPROVED PostStatus = "approved" // 通过 + POST_STATUS_REJECTED PostStatus = "rejected" // 拒绝 + POST_STATUS_CANCELLED PostStatus = "cancelled" // 取消 + POST_STATUS_IN_QUEUE PostStatus = "in_queue" // 排队 + POST_STATUS_PUBLISHED PostStatus = "published" // 已发表 + POST_STATUS_FAILED PostStatus = "failed" // 失败 + POST_STATUS_PENDING_RECALL PostStatus = "pending_recall" // 待撤回 + POST_STATUS_RECALLED PostStatus = "recalled" // 已撤回 +) + type PostPO struct { - ID int `json:"id" bson:"id"` // 稿件ID - UUID string `json:"uuid" bson:"uuid"` // UUID - Uin int64 `json:"uin" bson:"uin"` // 作者QQ号 - Text string `json:"text" bson:"text"` // 正文 - Images []string `json:"images" bson:"images"` // 图片 - Anon bool `json:"anon" bson:"anon"` // 是否匿名 + ID int `json:"id" bson:"id"` // 稿件ID + UUID string `json:"uuid" bson:"uuid"` // UUID + Uin int64 `json:"uin" bson:"uin"` // 作者QQ号 + Text string `json:"text" bson:"text"` // 正文 + Images []string `json:"images" bson:"images"` // 图片 + Anon bool `json:"anon" bson:"anon"` // 是否匿名 + Status PostStatus `json:"status" bson:"status"` // 状态 +} + +type PostLogPO struct { + PostID int `json:"post_id" bson:"post_id"` // 稿件ID + Op int64 `json:"op" bson:"op"` // 操作者ID -1表示系统 + OldStat PostStatus `json:"old_stat" bson:"old_stat"` // 旧状态 + NewStat PostStatus `json:"new_stat" bson:"new_stat"` // 新状态 + Comment string `json:"comment" bson:"comment"` // 备注 + CreatedAt time.Time `json:"created_at" bson:"created_at"` // CST时间 } diff --git a/backend/service/post.go b/backend/service/post.go index 48aaf36..834b631 100644 --- a/backend/service/post.go +++ b/backend/service/post.go @@ -30,23 +30,47 @@ func (ps *PostService) UploadImage(ioReader io.Reader, suffix string) (string, e return ps.OSS.UploadFromIO(ioReader, suffix) } -func (ps *PostService) PostNew(uuid string, uin int64, text string, images []string, anon bool) error { +func (ps *PostService) PostNew(uuid string, uin int64, text string, images []string, anon bool) (int, error) { id, err := ps.DB.CountPost() if err != nil { - return err + return -1, err } + id += 1 + // TODO 检查这个用户是否有未过审的帖子 // TODO 检查图片是否存在 - return ps.DB.AddPost(&database.PostPO{ - ID: id + 1, + err = ps.DB.AddPost(&database.PostPO{ + ID: id, UUID: uuid, Uin: uin, Text: text, Images: images, Anon: anon, + Status: database.POST_STATUS_PENDING_APPROVAL, }) + + if err != nil { + return -1, err + } + + err = ps.DB.AddPostLog( + &database.PostLogPO{ + PostID: id, + Op: uin, + OldStat: database.POST_STATUS_ANY, + NewStat: database.POST_STATUS_PENDING_APPROVAL, + Comment: "新稿件", + CreatedAt: util.GetCSTTime(), + }, + ) + + if err != nil { + return -1, err + } + + return id, nil }