From c5a3bd6eabbd17b8a1a3d77414dcc29c2f1aecb1 Mon Sep 17 00:00:00 2001 From: deepzz0 Date: Wed, 28 Apr 2021 14:40:28 +0800 Subject: [PATCH] fix: delete trash article --- pkg/cache/cache.go | 74 ++++++++++++++---- pkg/cache/store/mongodb.go | 2 +- pkg/core/blog/admin/admin.go | 141 ++++++++++++++++------------------- pkg/core/blog/page/be.go | 23 ++++-- pkg/core/blog/page/page.go | 1 + 5 files changed, 144 insertions(+), 97 deletions(-) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index b41b1c0..0939264 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -4,6 +4,7 @@ package cache import ( "bytes" "context" + "errors" "fmt" "sort" "strings" @@ -106,23 +107,60 @@ func (c *Cache) RepArticle(oldArticle, newArticle *model.Article) { c.refreshCache(newArticle, false) } -// DelArticles 删除文章 -func (c *Cache) DelArticles(ids []int) error { +// DelArticle 删除文章 +func (c *Cache) DelArticle(id int) error { c.lock.Lock() defer c.lock.Unlock() - for _, id := range ids { - article, _ := c.FindArticleByID(id) + article, _ := c.FindArticleByID(id) + if article == nil { + return nil + } + // set delete + err := c.UpdateArticle(context.Background(), id, map[string]interface{}{ + "deleted_at": time.Now(), + }) + if err != nil { + return err + } + // drop from tags,series,archives + c.refreshCache(article, true) + return nil +} - // set delete - err := c.UpdateArticle(context.Background(), id, map[string]interface{}{ - "deleted_at": time.Now(), - }) - if err != nil { - return err +// AddSerie 添加专题 +func (c *Cache) AddSerie(serie *model.Serie) error { + c.lock.Lock() + defer c.lock.Unlock() + + err := c.InsertSerie(context.Background(), serie) + if err != nil { + return err + } + c.Series = append(c.Series, serie) + PagesCh <- PageSeries + return nil +} + +// DelSerie 删除专题 +func (c *Cache) DelSerie(id int) error { + c.lock.Lock() + defer c.lock.Unlock() + + for i, serie := range c.Series { + if serie.ID == id { + if len(serie.Articles) > 0 { + return errors.New("请删除该专题下的所有文章") + } + err := c.RemoveSerie(context.Background(), id) + if err != nil { + return err + } + c.Series[i] = nil + c.Series = append(c.Series[:i], c.Series[i+1:]...) + PagesCh <- PageSeries + break } - // drop from tags,series,archives - c.refreshCache(article, true) } return nil } @@ -403,6 +441,12 @@ func (c *Cache) loadOrInit() error { return err } c.Account = account + // series + series, err := c.LoadAllSerie(context.Background()) + if err != nil { + return err + } + c.Series = series // all articles search := store.SearchArticles{ Page: 1, @@ -423,10 +467,10 @@ func (c *Cache) loadOrInit() error { continue } if i > 0 { - v.Prev = Ei.Articles[i-1] + v.Prev = articles[i-1] } - if Ei.Articles[i+1].ID >= blogapp.General.StartID { - v.Next = Ei.Articles[i+1] + if articles[i+1].ID >= blogapp.General.StartID { + v.Next = articles[i+1] } c.readdArticle(v, false) } diff --git a/pkg/cache/store/mongodb.go b/pkg/cache/store/mongodb.go index d5a9999..b94c696 100644 --- a/pkg/cache/store/mongodb.go +++ b/pkg/cache/store/mongodb.go @@ -298,7 +298,7 @@ func (db *mongodb) LoadArticleList(ctx context.Context, search SearchArticles) ( case SearchArticleSerieID: filter["serie_id"] = v.(int) case SearchArticleTrash: - filter["deleted_at"] = bson.M{"$nq": time.Time{}} + filter["deleted_at"] = bson.M{"$ne": time.Time{}} } } // search count diff --git a/pkg/core/blog/admin/admin.go b/pkg/core/blog/admin/admin.go index ed0b352..a1e1454 100644 --- a/pkg/core/blog/admin/admin.go +++ b/pkg/core/blog/admin/admin.go @@ -35,8 +35,6 @@ func RegisterRoutes(e *gin.Engine) { // RegisterRoutesAuthz register routes func RegisterRoutesAuthz(group gin.IRoutes) { - group.GET("/draft-delete", handleDraftDelete) - group.POST("/api/account", handleAPIAccount) group.POST("/api/blog", handleAPIBlogger) group.POST("/api/password", handleAPIPassword) @@ -80,49 +78,6 @@ func handleAcctLogin(c *gin.Context) { c.Redirect(http.StatusFound, "/admin/profile") } -// handleDraftDelete 删除草稿, 物理删除 -func handleDraftDelete(c *gin.Context) { - id, err := strconv.Atoi(c.Query("cid")) - if err != nil || id < 1 { - c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"}) - return - } - err = cache.Ei.RemoveArticle(context.Background(), id) - if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "删除错误"}) - return - } - c.Redirect(http.StatusFound, "/admin/write-post") -} - -// handleAPIAccount 更新账户信息 -func handleAPIAccount(c *gin.Context) { - e := c.PostForm("email") - pn := c.PostForm("phoneNumber") - ad := c.PostForm("address") - if (e != "" && !tools.ValidateEmail(e)) || (pn != "" && - !tools.ValidatePhoneNo(pn)) { - responseNotice(c, NoticeNotice, "参数错误", "") - return - } - - err := cache.Ei.UpdateAccount(context.Background(), cache.Ei.Account.Username, - map[string]interface{}{ - "email": e, - "phone_n": pn, - "address": ad, - }) - if err != nil { - logrus.Error("handleAPIAccount.UpdateAccount: ", err) - responseNotice(c, NoticeNotice, err.Error(), "") - return - } - cache.Ei.Account.Email = e - cache.Ei.Account.PhoneN = pn - cache.Ei.Account.Address = ad - responseNotice(c, NoticeSuccess, "更新成功", "") -} - // handleAPIBlogger 更新博客信息 func handleAPIBlogger(c *gin.Context) { bn := c.PostForm("blogName") @@ -159,6 +114,34 @@ func handleAPIBlogger(c *gin.Context) { responseNotice(c, NoticeSuccess, "更新成功", "") } +// handleAPIAccount 更新账户信息 +func handleAPIAccount(c *gin.Context) { + e := c.PostForm("email") + pn := c.PostForm("phoneNumber") + ad := c.PostForm("address") + if (e != "" && !tools.ValidateEmail(e)) || (pn != "" && + !tools.ValidatePhoneNo(pn)) { + responseNotice(c, NoticeNotice, "参数错误", "") + return + } + + err := cache.Ei.UpdateAccount(context.Background(), cache.Ei.Account.Username, + map[string]interface{}{ + "email": e, + "phone_n": pn, + "address": ad, + }) + if err != nil { + logrus.Error("handleAPIAccount.UpdateAccount: ", err) + responseNotice(c, NoticeNotice, err.Error(), "") + return + } + cache.Ei.Account.Email = e + cache.Ei.Account.PhoneN = pn + cache.Ei.Account.Address = ad + responseNotice(c, NoticeSuccess, "更新成功", "") +} + // handleAPIPassword 更新密码 func handleAPIPassword(c *gin.Context) { od := c.PostForm("old") @@ -191,6 +174,24 @@ func handleAPIPassword(c *gin.Context) { responseNotice(c, NoticeSuccess, "更新成功", "") } +// handleDraftDelete 删除草稿, 物理删除 +func handleDraftDelete(c *gin.Context) { + for _, v := range c.PostFormArray("mid[]") { + id, err := strconv.Atoi(v) + if err != nil || id < 1 { + responseNotice(c, NoticeNotice, "参数错误", "") + return + } + err = cache.Ei.RemoveArticle(context.Background(), id) + if err != nil { + logrus.Error("handleDraftDelete.RemoveArticle: ", err) + responseNotice(c, NoticeNotice, "删除失败", "") + return + } + } + responseNotice(c, NoticeSuccess, "删除成功", "") +} + // handleAPIPostDelete 删除文章,移入回收箱 func handleAPIPostDelete(c *gin.Context) { var ids []int @@ -200,17 +201,17 @@ func handleAPIPostDelete(c *gin.Context) { responseNotice(c, NoticeNotice, "参数错误", "") return } + err = cache.Ei.DelArticle(id) + if err != nil { + logrus.Error("handleAPIPostDelete.DeleteArticles: ", err) + + responseNotice(c, NoticeNotice, err.Error(), "") + return + } ids = append(ids, id) } - err := cache.Ei.DelArticles(ids) - if err != nil { - logrus.Error("handleAPIPostDelete.DeleteArticles: ", err) - - responseNotice(c, NoticeNotice, err.Error(), "") - return - } // elasticsearch - err = internal.ElasticDelIndex(ids) + err := internal.ElasticDelIndex(ids) if err != nil { logrus.Error("handleAPIPostDelete.ElasticDelIndex: ", err) } @@ -343,24 +344,10 @@ func handleAPISerieDelete(c *gin.Context) { responseNotice(c, NoticeNotice, err.Error(), "") return } - for i, serie := range cache.Ei.Series { - if serie.ID == id { - if len(serie.Articles) > 0 { - logrus.Error("handleAPISerieDelete.failed: ") - responseNotice(c, NoticeNotice, "请删除该专题下的所有文章", "") - return - } - err = cache.Ei.RemoveSerie(context.Background(), id) - if err != nil { - logrus.Error("handleAPISerieDelete.RemoveSerie: ") - responseNotice(c, NoticeNotice, err.Error(), "") - return - } - cache.Ei.Series[i] = nil - cache.Ei.Series = append(cache.Ei.Series[:i], cache.Ei.Series[i+1:]...) - cache.PagesCh <- cache.PageSeries - break - } + err = cache.Ei.DelSerie(id) + if err != nil { + responseNotice(c, NoticeNotice, err.Error(), "") + return } } responseNotice(c, NoticeSuccess, "删除成功", "") @@ -404,11 +391,15 @@ func handleAPISerieCreate(c *gin.Context) { responseNotice(c, NoticeNotice, err.Error(), "") return } + serie.Slug = slug + serie.Name = name + serie.Desc = desc } else { - err = cache.Ei.InsertSerie(context.Background(), &model.Serie{ - Slug: slug, - Name: name, - Desc: desc, + err = cache.Ei.AddSerie(&model.Serie{ + Slug: slug, + Name: name, + Desc: desc, + CreatedAt: time.Now(), }) if err != nil { logrus.Error("handleAPISerieCreate.InsertSerie: ", err) diff --git a/pkg/core/blog/page/be.go b/pkg/core/blog/page/be.go index 5d5f1fa..e468c95 100644 --- a/pkg/core/blog/page/be.go +++ b/pkg/core/blog/page/be.go @@ -14,10 +14,9 @@ import ( "github.com/eiblog/eiblog/pkg/cache/store" "github.com/eiblog/eiblog/pkg/config" "github.com/eiblog/eiblog/pkg/core/blog" - "github.com/eiblog/eiblog/pkg/model" - "github.com/sirupsen/logrus" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" ) // baseBEParams 基础参数 @@ -140,11 +139,10 @@ func handleAdminSerie(c *gin.Context) { id, err := strconv.Atoi(c.Query("mid")) params["Title"] = "新增专题 | " + cache.Ei.Blogger.BTitle if err == nil && id > 0 { - var serie *model.Serie for _, v := range cache.Ei.Series { if v.ID == id { params["Title"] = "编辑专题 | " + cache.Ei.Blogger.BTitle - params["Edit"] = serie + params["Edit"] = v break } } @@ -164,6 +162,21 @@ func handleAdminTags(c *gin.Context) { renderHTMLAdminLayout(c, "admin-tags", params) } +// handleDraftDelete 编辑页删除草稿 +func handleDraftDelete(c *gin.Context) { + id, err := strconv.Atoi(c.Query("cid")) + if err != nil || id < 1 { + c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"}) + return + } + err = cache.Ei.RemoveArticle(context.Background(), id) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "删除错误"}) + return + } + c.Redirect(http.StatusFound, "/admin/write-post") +} + // handleAdminDraft 草稿箱页 func handleAdminDraft(c *gin.Context) { params := baseBEParams(c) @@ -202,8 +215,6 @@ func handleAdminTrash(c *gin.Context) { params["List"], _, err = cache.Ei.LoadArticleList(context.Background(), search) if err != nil { logrus.Error("handleTrash.LoadArticleList: ", err) - c.HTML(http.StatusBadRequest, "backLayout.html", params) - return } renderHTMLAdminLayout(c, "admin-trash", params) } diff --git a/pkg/core/blog/page/page.go b/pkg/core/blog/page/page.go index 3406f20..90f1795 100644 --- a/pkg/core/blog/page/page.go +++ b/pkg/core/blog/page/page.go @@ -54,6 +54,7 @@ func RegisterRoutesAuthz(group gin.IRoutes) { group.GET("/profile", handleAdminProfile) // write group.GET("/write-post", handleAdminPost) + group.GET("/draft-delete", handleDraftDelete) // manage group.GET("/manage-posts", handleAdminPosts) group.GET("/manage-series", handleAdminSeries)