mirror of
https://github.com/eiblog/eiblog.git
synced 2026-02-10 16:42:26 +08:00
chore: complete api
This commit is contained in:
115
pkg/cache/store/mongodb.go
vendored
115
pkg/cache/store/mongodb.go
vendored
@@ -233,16 +233,6 @@ func (db *mongodb) RemoveArticle(ctx context.Context, id int) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteArticle 软删除文章,放入回收箱
|
||||
func (db *mongodb) DeleteArticle(ctx context.Context, id int) error {
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
|
||||
filter := bson.M{"id": id}
|
||||
update := bson.M{"$set": bson.M{"deletetime": time.Now()}}
|
||||
_, err := collection.UpdateOne(ctx, filter, update)
|
||||
return err
|
||||
}
|
||||
|
||||
// CleanArticles 清理回收站文章
|
||||
func (db *mongodb) CleanArticles(ctx context.Context) error {
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
@@ -270,16 +260,6 @@ func (db *mongodb) UpdateArticle(ctx context.Context, id int,
|
||||
return err
|
||||
}
|
||||
|
||||
// RecoverArticle 恢复文章到草稿
|
||||
func (db *mongodb) RecoverArticle(ctx context.Context, id int) error {
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
|
||||
filter := bson.M{"id": id}
|
||||
update := bson.M{"$set": bson.M{"deletetime": time.Time{}, "isdraft": true}}
|
||||
_, err := collection.UpdateOne(ctx, filter, update)
|
||||
return err
|
||||
}
|
||||
|
||||
// LoadArticle 查找文章
|
||||
func (db *mongodb) LoadArticle(ctx context.Context, id int) (*model.Article, error) {
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
@@ -295,76 +275,55 @@ func (db *mongodb) LoadArticle(ctx context.Context, id int) (*model.Article, err
|
||||
return article, err
|
||||
}
|
||||
|
||||
// LoadAllArticle 读取所有文章
|
||||
func (db *mongodb) LoadAllArticle(ctx context.Context) (model.SortedArticles, error) {
|
||||
// LoadArticleList 获取文章列表
|
||||
func (db *mongodb) LoadArticleList(ctx context.Context, search SearchArticles) (
|
||||
model.SortedArticles, int, error) {
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
|
||||
filter := bson.M{"isdraft": false, "deletetime": bson.M{"$eq": time.Time{}}}
|
||||
cur, err := collection.Find(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
filter := bson.M{}
|
||||
for k, v := range search.Fields {
|
||||
switch k {
|
||||
case SearchArticleDraft:
|
||||
if ok := v.(bool); ok {
|
||||
filter["is_draft"] = true
|
||||
} else {
|
||||
filter["is_draft"] = false
|
||||
filter["deleted_at"] = bson.M{"$eq": time.Time{}}
|
||||
}
|
||||
case SearchArticleTitle:
|
||||
filter["title"] = bson.M{
|
||||
"$regex": v.(string),
|
||||
"$options": "$i",
|
||||
}
|
||||
case SearchArticleSerieID:
|
||||
filter["serie_id"] = v.(int)
|
||||
case SearchArticleTrash:
|
||||
filter["deleted_at"] = bson.M{"$nq": time.Time{}}
|
||||
}
|
||||
}
|
||||
// search count
|
||||
count, err := collection.CountDocuments(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
opts := options.Find().SetLimit(int64(search.Limit)).
|
||||
SetSkip(int64((search.Page - 1) * search.Limit)).
|
||||
SetSort(bson.M{"created_at": -1})
|
||||
cur, err := collection.Find(ctx, filter, opts)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer cur.Close(ctx)
|
||||
|
||||
var articles model.SortedArticles
|
||||
for cur.Next(ctx) {
|
||||
obj := model.Article{}
|
||||
err = cur.Decode(&obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
articles = append(articles, &obj)
|
||||
}
|
||||
sort.Sort(articles)
|
||||
return articles, nil
|
||||
}
|
||||
|
||||
// LoadTrashArticles 读取回收箱
|
||||
func (db *mongodb) LoadTrashArticles(ctx context.Context) (model.SortedArticles, error) {
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
|
||||
filter := bson.M{"deletetime": bson.M{"$ne": time.Time{}}}
|
||||
cur, err := collection.Find(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cur.Close(ctx)
|
||||
|
||||
var articles model.SortedArticles
|
||||
for cur.Next(ctx) {
|
||||
obj := model.Article{}
|
||||
err = cur.Decode(&obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
articles = append(articles, &obj)
|
||||
}
|
||||
sort.Sort(articles)
|
||||
return articles, nil
|
||||
}
|
||||
|
||||
// LoadDraftArticles 读取草稿箱
|
||||
func (db *mongodb) LoadDraftArticles(ctx context.Context) (model.SortedArticles, error) {
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
|
||||
filter := bson.M{"isdraft": true}
|
||||
cur, err := collection.Find(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cur.Close(ctx)
|
||||
|
||||
var articles model.SortedArticles
|
||||
for cur.Next(ctx) {
|
||||
obj := model.Article{}
|
||||
err = cur.Decode(&obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
articles = append(articles, &obj)
|
||||
}
|
||||
sort.Sort(articles)
|
||||
return articles, nil
|
||||
return articles, int(count), nil
|
||||
}
|
||||
|
||||
// counter counter
|
||||
|
||||
27
pkg/cache/store/store.go
vendored
27
pkg/cache/store/store.go
vendored
@@ -15,6 +15,21 @@ var (
|
||||
stores = make(map[string]Driver)
|
||||
)
|
||||
|
||||
// search field
|
||||
const (
|
||||
SearchArticleDraft = "draft"
|
||||
SearchArticleTrash = "trash"
|
||||
SearchArticleTitle = "title"
|
||||
SearchArticleSerieID = "serieid"
|
||||
)
|
||||
|
||||
// SearchArticles 搜索字段
|
||||
type SearchArticles struct {
|
||||
Page int // 第几页/1
|
||||
Limit int // 每页大小
|
||||
Fields map[string]interface{} // 字段:值
|
||||
}
|
||||
|
||||
// Store 存储后端
|
||||
type Store interface {
|
||||
// LoadInsertBlogger 读取或创建博客
|
||||
@@ -40,22 +55,14 @@ type Store interface {
|
||||
InsertArticle(ctx context.Context, article *model.Article) error
|
||||
// RemoveArticle 硬删除文章
|
||||
RemoveArticle(ctx context.Context, id int) error
|
||||
// DeleteArticle 软删除文章,放入回收箱
|
||||
DeleteArticle(ctx context.Context, id int) error
|
||||
// CleanArticles 清理回收站文章
|
||||
CleanArticles(ctx context.Context) error
|
||||
// UpdateArticle 更新文章
|
||||
UpdateArticle(ctx context.Context, id int, fields map[string]interface{}) error
|
||||
// RecoverArticle 恢复文章到草稿
|
||||
RecoverArticle(ctx context.Context, id int) error
|
||||
// LoadArticle 查找文章
|
||||
LoadArticle(ctx context.Context, id int) (*model.Article, error)
|
||||
// LoadAllArticle 读取所有文章
|
||||
LoadAllArticle(ctx context.Context) (model.SortedArticles, error)
|
||||
// LoadTrashArticles 读取回收箱
|
||||
LoadTrashArticles(ctx context.Context) (model.SortedArticles, error)
|
||||
// LoadDraftArticles 读取草稿箱
|
||||
LoadDraftArticles(ctx context.Context) (model.SortedArticles, error)
|
||||
// LoadArticleList 查找文章列表
|
||||
LoadArticleList(ctx context.Context, search SearchArticles) (model.SortedArticles, int, error)
|
||||
}
|
||||
|
||||
// Driver 存储驱动
|
||||
|
||||
Reference in New Issue
Block a user