mirror of
https://github.com/eiblog/eiblog.git
synced 2026-02-10 00:22:27 +08:00
chore: remove blogger config
This commit is contained in:
45
pkg/cache/store/mongodb.go
vendored
45
pkg/cache/store/mongodb.go
vendored
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
1
pkg/cache/store/mongodb_test.go
vendored
1
pkg/cache/store/mongodb_test.go
vendored
@@ -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)
|
||||
|
||||
5
pkg/cache/store/store.go
vendored
5
pkg/cache/store/store.go
vendored
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user