chore: remove blogger config

This commit is contained in:
deepzz0
2021-04-27 14:02:52 +08:00
parent 990e6abbd8
commit c3fdbfcb78
9 changed files with 280 additions and 70 deletions

View File

@@ -50,27 +50,50 @@ func (db *mongodb) Init(source string) (Store, error) {
return nil, err
}
db.Client = client
// create index
indexModel := mongo.IndexModel{
Keys: bson.D{{"username", 1}},
Options: options.Index().SetUnique(true).SetSparse(true),
}
db.Database(mongoDBName).Collection(collectionAccount).
Indexes().
CreateOne(context.Background(), indexModel)
indexModel = mongo.IndexModel{
Keys: bson.D{{"slug", 1}},
Options: options.Index().SetUnique(true).SetSparse(true),
}
db.Database(mongoDBName).Collection(collectionArticle).
Indexes().
CreateOne(context.Background(), indexModel)
indexModel = mongo.IndexModel{
Keys: bson.D{{"slug", 1}},
Options: options.Index().SetUnique(true).SetSparse(true),
}
db.Database(mongoDBName).Collection(collectionSeries).
Indexes().
CreateOne(context.Background(), indexModel)
return db, nil
}
// LoadInsertBlogger 读取或创建博客
func (db *mongodb) LoadInsertBlogger(ctx context.Context,
blogger *model.Blogger) (*model.Blogger, error) {
blogger *model.Blogger) (created bool, err error) {
collection := db.Database(mongoDBName).Collection(collectionBlogger)
filter := bson.M{}
result := collection.FindOne(ctx, filter)
err := result.Err()
err = result.Err()
if err != nil {
if err != mongo.ErrNoDocuments {
return nil, err
return
}
_, err = collection.InsertOne(ctx, blogger)
created = true
} else {
err = result.Decode(blogger)
}
return blogger, err
return
}
// UpdateBlogger 更新博客
@@ -91,22 +114,23 @@ func (db *mongodb) UpdateBlogger(ctx context.Context,
// LoadInsertAccount 读取或创建账户
func (db *mongodb) LoadInsertAccount(ctx context.Context,
acct *model.Account) (*model.Account, error) {
acct *model.Account) (created bool, err error) {
collection := db.Database(mongoDBName).Collection(collectionAccount)
filter := bson.M{"username": config.Conf.BlogApp.Account.Username}
result := collection.FindOne(ctx, filter)
err := result.Err()
err = result.Err()
if err != nil {
if err != mongo.ErrNoDocuments {
return nil, err
return
}
_, err = collection.InsertOne(ctx, acct)
created = true
} else {
err = result.Decode(acct)
}
return acct, err
return
}
// UpdateAccount 更新账户
@@ -185,14 +209,13 @@ func (db *mongodb) LoadAllSeries(ctx context.Context) (model.SortedSeries, error
// InsertArticle 创建文章
func (db *mongodb) InsertArticle(ctx context.Context, article *model.Article) error {
// 分配ID, 占位至起始id
for {
// 可手动分配ID或者分配ID, 占位至起始id
for article.ID == 0 {
id := db.nextValue(ctx, counterNameArticle)
if id < config.Conf.BlogApp.General.StartID {
continue
} else {
article.ID = id
break
}
}

View File

@@ -135,6 +135,7 @@ func TestLoadAllSeries(t *testing.T) {
}
func TestInsertArticle(t *testing.T) {
article.ID = 12
err := store.InsertArticle(context.Background(), article)
if err != nil {
t.Fatal(err)

View File

@@ -18,12 +18,12 @@ var (
// Store 存储后端
type Store interface {
// LoadInsertBlogger 读取或创建博客
LoadInsertBlogger(ctx context.Context, blogger *model.Blogger) (*model.Blogger, error)
LoadInsertBlogger(ctx context.Context, blogger *model.Blogger) (bool, error)
// UpdateBlogger 更新博客
UpdateBlogger(ctx context.Context, fields map[string]interface{}) error
// LoadInsertAccount 读取或创建账户
LoadInsertAccount(ctx context.Context, acct *model.Account) (*model.Account, error)
LoadInsertAccount(ctx context.Context, acct *model.Account) (bool, error)
// UpdateAccount 更新账户
UpdateAccount(ctx context.Context, name string, fields map[string]interface{}) error
@@ -58,6 +58,7 @@ type Store interface {
// Driver 存储驱动
type Driver interface {
// Init 数据库初始化, 建表, 加索引操作等
Init(source string) (Store, error)
}