Skip to content

Commit

Permalink
fix: 按照接口文档 post-new 应该返回新稿件id
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Apr 12, 2024
1 parent e443a3c commit c8a08fa
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 14 deletions.
6 changes: 4 additions & 2 deletions backend/controller/postapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
10 changes: 8 additions & 2 deletions backend/database/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
37 changes: 31 additions & 6 deletions backend/database/po.go
Original file line number Diff line number Diff line change
Expand Up @@ -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时间
}
32 changes: 28 additions & 4 deletions backend/service/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c8a08fa

Please sign in to comment.