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 28, 2024
2 parents 631cc60 + 9ee58de commit daac7f6
Showing 1 changed file with 87 additions and 2 deletions.
89 changes: 87 additions & 2 deletions backend/database/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,50 @@ const (
METADATA_COLLECTION = "metadata"
)

type Metadata struct {
Key string `bson:"key"`

Value string `bson:"value"`
}

var PresetMetadata = []Metadata{
{
Key: "banner",
Value: "投稿前请阅读投稿规则!",
},
{
Key: "popup_announcement",
Value: "欢迎使用 Campux!",
},
{
Key: "post_rules",
Value: `[
"投稿规则是数组",
"每个元素是一个字符串"
]`,
},
{
Key: "services",
Value: `[
{
"name": "服务名称",
"description": "服务也是数组形式,会显示在服务tab",
"link": "https://url.to.service",
"toast": "点击时的提示",
"emoji": "🗺️"
}
]`,
},
{
Key: "brand",
Value: "Campux 这个是你的墙的名称",
},
{
Key: "beianhao",
Value: "桂ICP备1145141919号-1",
},
}

type MongoDBManager struct {
Client *mongo.Client
}
Expand All @@ -29,6 +73,24 @@ func NewMongoDBManager() *MongoDBManager {
panic(err)
}

m := &MongoDBManager{
Client: client,
}

// 检查连接
err = client.Ping(context.TODO(), nil)
if err != nil {
panic(err)
}

// 元数据

err = m.CheckMetadata()

if err != nil {
panic(err)
}

// 创建索引
// post的uuid
_, err = client.Database(viper.GetString("database.mongo.db")).Collection(POST_COLLECTION).Indexes().CreateOne(
Expand All @@ -44,9 +106,32 @@ func NewMongoDBManager() *MongoDBManager {
panic(err)
}

return &MongoDBManager{
Client: client,
return m
}

// 检查所有元数据的key是否存在,不存在则插入预设的
func (m *MongoDBManager) CheckMetadata() error {
// 创建collection
err := m.Client.Database(viper.GetString("database.mongo.db")).CreateCollection(context.TODO(), METADATA_COLLECTION)
if err != nil {
return err
}
for _, meta := range PresetMetadata {
exist, err := m.Client.Database(viper.GetString("database.mongo.db")).Collection(METADATA_COLLECTION).CountDocuments(
context.TODO(),
bson.M{"key": meta.Key},
)
if err != nil {
return err
}
if exist == 0 {
_, err := m.Client.Database(viper.GetString("database.mongo.db")).Collection(METADATA_COLLECTION).InsertOne(context.TODO(), meta)
if err != nil {
return err
}
}
}
return nil
}

func (m *MongoDBManager) AddAccount(acc *AccountPO) error {
Expand Down

0 comments on commit daac7f6

Please sign in to comment.