From 1ff66bdd16057d15683fd4692e6da33be05f38a4 Mon Sep 17 00:00:00 2001 From: deepzz0 Date: Mon, 3 Oct 2016 15:51:54 +0800 Subject: [PATCH] update --- api.go | 110 ++++++++++++++----------------- check.go | 2 +- conf/app.yml | 2 +- db.go | 146 +++++++++++++++++++---------------------- docker-compose.yml | 2 +- front.go | 2 + views/admin/posts.html | 2 +- views/article.html | 2 +- 8 files changed, 125 insertions(+), 143 deletions(-) diff --git a/api.go b/api.go index 86875da..48b0165 100644 --- a/api.go +++ b/api.go @@ -152,12 +152,12 @@ func apiPostAdd(c *gin.Context) { var cid int defer func() { if !publish { - // {"success":1,"time":"10:40:46 AM","cid":"4"} if err == nil { c.JSON(http.StatusOK, gin.H{"success": SUCCESS, "time": time.Now().Format("15:04:05 PM"), "cid": cid}) - return + } else { + logd.Error(err) + c.JSON(http.StatusOK, gin.H{"fail": FAIL, "time": time.Now().Format("15:04:05 PM"), "cid": cid}) } - logd.Error(err) } if err == nil { c.Redirect(http.StatusFound, "/admin/manage-posts") @@ -183,17 +183,18 @@ func apiPostAdd(c *gin.Context) { t := CheckDate(date) serieid := CheckSerieID(serie) publish = CheckPublish(do) - if cid, err = strconv.Atoi(c.PostForm("cid")); err != nil || cid < 1 { - artc := &Article{ - Title: title, - Content: text, - Slug: slug, - CreateTime: t, - IsDraft: !publish, - Author: Ei.Username, - SerieID: serieid, - Tags: tags, - } + artc := &Article{ + Title: title, + Content: text, + Slug: slug, + CreateTime: t, + IsDraft: !publish, + Author: Ei.Username, + SerieID: serieid, + Tags: tags, + } + cid, err = strconv.Atoi(c.PostForm("cid")) + if err != nil || cid < 1 { err = AddArticle(artc) if err != nil { logd.Error(err) @@ -203,53 +204,44 @@ func apiPostAdd(c *gin.Context) { if publish { ElasticIndex(artc) } - } else { - artc := QueryArticle(int32(cid)) - if artc == nil { - err = errors.New("没有发现该文章") - return - } - if publish { - if Ei.MapArticles[artc.Slug] != nil { - i, a := GetArticle(artc.ID) - DelFromLinkedList(a) - Ei.Articles[i] = artc - if a.SerieID != serieid { - ManageSeriesArticle(a, false, DELETE) - } - if strings.Join(a.Tags, ",") != tag { - ManageTagsArticle(a, false, DELETE) - } - ManageArchivesArticle(a, false, DELETE) - } else { - Ei.Articles = append(Ei.Articles, artc) - artc.IsDraft = false - } - Ei.MapArticles[artc.Slug] = artc - } - artc.Title = title - artc.Slug = slug - artc.Content = text - artc.CreateTime = t + return + } + artc.ID = int32(cid) + if CheckBool(c.PostForm("update")) { artc.UpdateTime = time.Now() - artc.SerieID = serieid - artc.Tags = tags - err = UpdateArticle(bson.M{"id": artc.ID}, artc) - if err != nil { - logd.Error(err) - return + } + i, a := GetArticle(artc.ID) + if a != nil { + artc.IsDraft = false + artc.Count = a.Count + artc.UpdateTime = a.UpdateTime + } + err = UpdateArticle(bson.M{"id": artc.ID}, artc) + if err != nil { + logd.Error(err) + return + } + if !artc.IsDraft { + if a != nil { + Ei.Articles = append(Ei.Articles[0:i], Ei.Articles[i+1:]...) + DelFromLinkedList(a) + ManageTagsArticle(a, false, DELETE) + ManageSeriesArticle(a, false, DELETE) + ManageArchivesArticle(a, false, DELETE) + delete(Ei.MapArticles, a.Slug) + a = nil } - if publish { - sort.Sort(Ei.Articles) - GenerateExcerptAndRender(artc) - // elasticsearch 索引 - ElasticIndex(artc) - if artc.ID >= setting.Conf.StartID { - ManageTagsArticle(artc, true, ADD) - ManageSeriesArticle(artc, true, ADD) - ManageArchivesArticle(artc, true, ADD) - AddToLinkedList(artc.ID) - } + Ei.MapArticles[artc.Slug] = artc + Ei.Articles = append(Ei.Articles, artc) + sort.Sort(Ei.Articles) + GenerateExcerptAndRender(artc) + // elasticsearch 索引 + ElasticIndex(artc) + if artc.ID >= setting.Conf.StartID { + ManageTagsArticle(artc, true, ADD) + ManageSeriesArticle(artc, true, ADD) + ManageArchivesArticle(artc, true, ADD) + AddToLinkedList(artc.ID) } } } diff --git a/check.go b/check.go index 48b14c8..d915c3d 100644 --- a/check.go +++ b/check.go @@ -40,7 +40,7 @@ func CheckSerieID(sid string) int32 { } func CheckBool(str string) bool { - return str == "true" + return str == "true" || str == "1" } func CheckPublish(do string) bool { diff --git a/conf/app.yml b/conf/app.yml index be162a7..15e164f 100644 --- a/conf/app.yml +++ b/conf/app.yml @@ -13,7 +13,7 @@ pagesize: 20 # 自动截取预览, 字符数 length: 200 # 截取预览标识 -identifier: +identifier: # favicon favicon: //st.deepzz.com/static/img/favicon.ico # 起始ID,预留id不时之需, 不用管 diff --git a/db.go b/db.go index cb1f1d0..13b4582 100644 --- a/db.go +++ b/db.go @@ -6,7 +6,6 @@ import ( "fmt" "regexp" "sort" - // "strings" "sync" "time" @@ -262,24 +261,50 @@ func PageList(p, n int) (prev int, next int, artcs []*Article) { return } -func ManageTagsArticle(artc *Article, s bool, dos ...string) { - for _, do := range dos { - switch do { - case ADD: - for _, tag := range artc.Tags { - Ei.Tags[tag] = append(Ei.Tags[tag], artc) - if s { - sort.Sort(Ei.Tags[tag]) +func ManageTagsArticle(artc *Article, s bool, do string) { + switch do { + case ADD: + for _, tag := range artc.Tags { + Ei.Tags[tag] = append(Ei.Tags[tag], artc) + if s { + sort.Sort(Ei.Tags[tag]) + } + } + case DELETE: + for _, tag := range artc.Tags { + for i, v := range Ei.Tags[tag] { + if v == artc { + Ei.Tags[tag] = append(Ei.Tags[tag][0:i], Ei.Tags[tag][i+1:]...) + if len(Ei.Tags[tag]) == 0 { + delete(Ei.Tags, tag) + } + return } } - case DELETE: - for _, tag := range artc.Tags { - for i, v := range Ei.Tags[tag] { + } + } +} + +func ManageSeriesArticle(artc *Article, s bool, do string) { + switch do { + case ADD: + for i, serie := range Ei.Series { + if serie.ID == artc.SerieID { + Ei.Series[i].Articles = append(Ei.Series[i].Articles, artc) + if s { + sort.Sort(Ei.Series[i].Articles) + Ei.CH <- SERIES_MD + return + } + } + } + case DELETE: + for i, serie := range Ei.Series { + if serie.ID == artc.SerieID { + for j, v := range serie.Articles { if v == artc { - Ei.Tags[tag] = append(Ei.Tags[tag][0:i], Ei.Tags[tag][i+1:]...) - if len(Ei.Tags[tag]) == 0 { - delete(Ei.Tags, tag) - } + Ei.Series[i].Articles = append(Ei.Series[i].Articles[0:j], Ei.Series[i].Articles[j+1:]...) + Ei.CH <- SERIES_MD return } } @@ -288,78 +313,40 @@ func ManageTagsArticle(artc *Article, s bool, dos ...string) { } } -func ManageSeriesArticle(artc *Article, s bool, dos ...string) { - for _, do := range dos { - switch do { - case ADD: - if artc.SerieID != 0 { - for i, serie := range Ei.Series { - if serie.ID == artc.SerieID { - Ei.Series[i].Articles = append(Ei.Series[i].Articles, artc) - if s { - sort.Sort(Ei.Series[i].Articles) - Ei.CH <- SERIES_MD - return - } - } - } - } - case DELETE: - if artc.SerieID != 0 { - for i, serie := range Ei.Series { - if serie.ID == artc.SerieID { - for j, v := range serie.Articles { - if v == artc { - Ei.Series[i].Articles = append(Ei.Series[i].Articles[0:j], Ei.Series[i].Articles[j+1:]...) - Ei.CH <- SERIES_MD - return - } - } - } +func ManageArchivesArticle(artc *Article, s bool, do string) { + switch do { + case ADD: + add := false + y, m, _ := artc.CreateTime.Date() + for i, archive := range Ei.Archives { + ay, am, _ := archive.Time.Date() + if y == ay && m == am { + add = true + Ei.Archives[i].Articles = append(Ei.Archives[i].Articles, artc) + if s { + sort.Sort(Ei.Archives[i].Articles) + Ei.CH <- ARCHIVE_MD + break } } } - } - -} - -func ManageArchivesArticle(artc *Article, s bool, dos ...string) { - for _, do := range dos { - switch do { - case ADD: - add := false - y, m, _ := artc.CreateTime.Date() - for i, archive := range Ei.Archives { - ay, am, _ := archive.Time.Date() - if y == ay && m == am { - add = true - Ei.Archives[i].Articles = append(Ei.Archives[i].Articles, artc) - if s { - sort.Sort(Ei.Archives[i].Articles) + if !add { + Ei.Archives = append(Ei.Archives, &Archive{Time: artc.CreateTime, Articles: SortArticles{artc}}) + } + case DELETE: + for i, archive := range Ei.Archives { + ay, am, _ := archive.Time.Date() + if y, m, _ := artc.CreateTime.Date(); ay == y && am == m { + for j, v := range archive.Articles { + if v == artc { + Ei.Archives[i].Articles = append(Ei.Archives[i].Articles[0:j], Ei.Archives[i].Articles[j+1:]...) Ei.CH <- ARCHIVE_MD - break - } - } - } - if !add { - Ei.Archives = append(Ei.Archives, &Archive{Time: artc.CreateTime, Articles: SortArticles{artc}}) - } - case DELETE: - for i, archive := range Ei.Archives { - ay, am, _ := archive.Time.Date() - if y, m, _ := artc.CreateTime.Date(); ay == y && am == m { - for j, v := range archive.Articles { - if v == artc { - Ei.Archives[i].Articles = append(Ei.Archives[i].Articles[0:j], Ei.Archives[i].Articles[j+1:]...) - Ei.CH <- ARCHIVE_MD - return - } + return } } } } } - } // 渲染markdown操作和截取摘要操作 @@ -438,6 +425,7 @@ func DelArticles(ids ...int32) error { if err != nil { return err } + artc = nil } Ei.CH <- ARCHIVE_MD Ei.CH <- SERIES_MD diff --git a/docker-compose.yml b/docker-compose.yml index 2ff2010..1f128f6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,7 +12,7 @@ services: volumes: - /data/eiblog/conf/es/config:/usr/share/elasticsearch/config - /data/eiblog/conf/es/plugins:/usr/share/elasticsearch/plugins - - /data/eiblog/conf/es/data:/usr/share/elasticsearch/data + - /data/eiblog/esdata/data:/usr/share/elasticsearch/data - /data/eiblog/conf/es/logs:/usr/share/elasticsearch/logs environment: ES_JAVA_OPTS: "-Xms512m -Xmx512m" diff --git a/front.go b/front.go index c3edc9d..93fec4d 100644 --- a/front.go +++ b/front.go @@ -132,6 +132,8 @@ func HandleArticlePage(c *gin.Context) { h["Copyright"] = Ei.Copyright if !artc.UpdateTime.IsZero() { h["Days"] = int(time.Now().Sub(artc.UpdateTime).Hours()) / 24 + } else { + h["Days"] = int(time.Now().Sub(artc.CreateTime).Hours()) / 24 } if artc.SerieID > 0 { h["Serie"] = QuerySerie(artc.SerieID) diff --git a/views/admin/posts.html b/views/admin/posts.html index 963fd8c..339c3e9 100644 --- a/views/admin/posts.html +++ b/views/admin/posts.html @@ -61,7 +61,7 @@ - {{.Count}} + {{.Count}} {{.Title}} diff --git a/views/article.html b/views/article.html index e238230..d471c7b 100644 --- a/views/article.html +++ b/views/article.html @@ -1 +1 @@ -{{define "article"}}
{{with .Article}}

{{.Title}}

{{str2html .Content}}

本文链接:参与评论 »

--EOF--

{{with $.Copyright}}{{end}} {{with $.Serie}}{{if gt $.Days 100}}

提醒:本文最后更新于 {{$.Days}} 天前,文中所描述的信息可能已发生改变,请谨慎使用。

{{end}}

专题「{{.Name}}」的其它文章 »

    {{range .Articles}}{{if ne .ID $.Article.ID}}
  • {{.Title}} ({{dateformat .CreateTime "Jan 02, 2006"}})
  • {{end}}{{end}}
{{end}}

Comments

{{end}}
{{end}} +{{define "article"}}
{{with .Article}}

{{.Title}}

{{str2html .Content}}

本文链接:参与评论 »

--EOF--

{{with $.Copyright}}{{end}}{{if gt $.Days 100}}

提醒:本文最后更新于 {{$.Days}} 天前,文中所描述的信息可能已发生改变,请谨慎使用。

{{end}}{{with $.Serie}}

专题「{{.Name}}」的其它文章 »

    {{range .Articles}}{{if ne .ID $.Article.ID}}
  • {{.Title}} ({{dateformat .CreateTime "Jan 02, 2006"}})
  • {{end}}{{end}}
{{end}}

Comments

{{end}}
{{end}}