Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Soulter committed Apr 12, 2024
2 parents bd807ac + c8a08fa commit fb0b3b4
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 28 deletions.
5 changes: 5 additions & 0 deletions backend/controller/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"github.com/RockChinQ/Campux/backend/service"
"github.com/RockChinQ/Campux/backend/util"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)

Expand All @@ -16,6 +17,10 @@ func NewApiController(
) *APIController {
r := gin.Default()

if gin.Mode() == gin.DebugMode {
r.Use(cors.Default())
}

rg := r.Group("/v1")

// bind routes
Expand Down
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
}
30 changes: 16 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,37 @@ go 1.22.1
require github.com/spf13/viper v1.18.2

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/bytedance/sonic v1.11.3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/cors v1.7.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-playground/validator/v10 v10.19.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.6 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/minio-go/v7 v7.0.69 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.0 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand All @@ -43,22 +45,22 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.mongodb.org/mongo-driver v1.14.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit fb0b3b4

Please sign in to comment.