mirror of
https://github.com/eiblog/eiblog.git
synced 2026-02-12 17:32:27 +08:00
chore: run blog
This commit is contained in:
76
pkg/cache/store/mongodb.go
vendored
76
pkg/cache/store/mongodb.go
vendored
@@ -16,6 +16,9 @@ import (
|
||||
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||
)
|
||||
|
||||
// driver: mongodb
|
||||
// source: mongodb://localhost:27017
|
||||
|
||||
const (
|
||||
mongoDBName = "eiblog"
|
||||
collectionAccount = "account"
|
||||
@@ -50,42 +53,6 @@ func (db *mongodb) Init(source string) (Store, error) {
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// LoadInsertAccount 读取或创建账户
|
||||
func (db *mongodb) LoadInsertAccount(ctx context.Context,
|
||||
acct *model.Account) (*model.Account, error) {
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionAccount)
|
||||
|
||||
filter := bson.M{"username": config.Conf.BlogApp.Account.Username}
|
||||
result := collection.FindOne(ctx, filter)
|
||||
err := result.Err()
|
||||
if err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
return nil, err
|
||||
}
|
||||
_, err = collection.InsertOne(ctx, acct)
|
||||
} else {
|
||||
err = result.Decode(acct)
|
||||
}
|
||||
return acct, err
|
||||
}
|
||||
|
||||
// UpdateAccount 更新账户
|
||||
func (db *mongodb) UpdateAccount(ctx context.Context, name string,
|
||||
fields map[string]interface{}) error {
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionAccount)
|
||||
|
||||
filter := bson.M{"username": name}
|
||||
params := bson.M{}
|
||||
for k, v := range fields {
|
||||
params[k] = v
|
||||
}
|
||||
update := bson.M{"$set": params}
|
||||
_, err := collection.UpdateOne(ctx, filter, update)
|
||||
return err
|
||||
}
|
||||
|
||||
// LoadInsertBlogger 读取或创建博客
|
||||
func (db *mongodb) LoadInsertBlogger(ctx context.Context,
|
||||
blogger *model.Blogger) (*model.Blogger, error) {
|
||||
@@ -122,6 +89,42 @@ func (db *mongodb) UpdateBlogger(ctx context.Context,
|
||||
return err
|
||||
}
|
||||
|
||||
// LoadInsertAccount 读取或创建账户
|
||||
func (db *mongodb) LoadInsertAccount(ctx context.Context,
|
||||
acct *model.Account) (*model.Account, error) {
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionAccount)
|
||||
|
||||
filter := bson.M{"username": config.Conf.BlogApp.Account.Username}
|
||||
result := collection.FindOne(ctx, filter)
|
||||
err := result.Err()
|
||||
if err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
return nil, err
|
||||
}
|
||||
_, err = collection.InsertOne(ctx, acct)
|
||||
} else {
|
||||
err = result.Decode(acct)
|
||||
}
|
||||
return acct, err
|
||||
}
|
||||
|
||||
// UpdateAccount 更新账户
|
||||
func (db *mongodb) UpdateAccount(ctx context.Context, name string,
|
||||
fields map[string]interface{}) error {
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionAccount)
|
||||
|
||||
filter := bson.M{"username": name}
|
||||
params := bson.M{}
|
||||
for k, v := range fields {
|
||||
params[k] = v
|
||||
}
|
||||
update := bson.M{"$set": params}
|
||||
_, err := collection.UpdateOne(ctx, filter, update)
|
||||
return err
|
||||
}
|
||||
|
||||
// InsertSeries 创建专题
|
||||
func (db *mongodb) InsertSeries(ctx context.Context, series *model.Series) error {
|
||||
collection := db.Database(mongoDBName).Collection(collectionSeries)
|
||||
@@ -222,7 +225,6 @@ func (db *mongodb) CleanArticles(ctx context.Context) error {
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
|
||||
exp := time.Now().Add(time.Duration(config.Conf.BlogApp.General.Trash) * time.Hour)
|
||||
fmt.Println(exp)
|
||||
filter := bson.M{"deletetime": bson.M{"$gt": time.Time{}, "$lt": exp}}
|
||||
_, err := collection.DeleteMany(ctx, filter)
|
||||
return err
|
||||
|
||||
1
pkg/cache/store/mongodb_test.go
vendored
1
pkg/cache/store/mongodb_test.go
vendored
@@ -155,7 +155,6 @@ func TestDeleteArticle(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
func TestCleanArticles(t *testing.T) {
|
||||
err := store.CleanArticles(context.Background())
|
||||
if err != nil {
|
||||
|
||||
10
pkg/cache/store/store.go
vendored
10
pkg/cache/store/store.go
vendored
@@ -17,16 +17,16 @@ var (
|
||||
|
||||
// Store 存储后端
|
||||
type Store interface {
|
||||
// LoadInsertAccount 读取或创建账户
|
||||
LoadInsertAccount(ctx context.Context, acct *model.Account) (*model.Account, error)
|
||||
// UpdateAccount 更新账户
|
||||
UpdateAccount(ctx context.Context, name string, fields map[string]interface{}) error
|
||||
|
||||
// LoadInsertBlogger 读取或创建博客
|
||||
LoadInsertBlogger(ctx context.Context, blogger *model.Blogger) (*model.Blogger, error)
|
||||
// UpdateBlogger 更新博客
|
||||
UpdateBlogger(ctx context.Context, fields map[string]interface{}) error
|
||||
|
||||
// LoadInsertAccount 读取或创建账户
|
||||
LoadInsertAccount(ctx context.Context, acct *model.Account) (*model.Account, error)
|
||||
// UpdateAccount 更新账户
|
||||
UpdateAccount(ctx context.Context, name string, fields map[string]interface{}) error
|
||||
|
||||
// InsertSeries 创建专题
|
||||
InsertSeries(ctx context.Context, series *model.Series) error
|
||||
// RemoveSeries 删除专题
|
||||
|
||||
Reference in New Issue
Block a user