From beea4f1746e2a986e234df4d881458523f32d2da Mon Sep 17 00:00:00 2001 From: deepzz0 Date: Wed, 28 Apr 2021 12:50:06 +0800 Subject: [PATCH] chore: tags use array --- go.mod | 3 +-- go.sum | 6 ++---- pkg/cache/cache.go | 6 ++---- pkg/core/blog/admin/admin.go | 6 +++++- pkg/internal/es.go | 3 +-- pkg/model/article.go | 24 ++++++++++++++---------- 6 files changed, 25 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index 34afde8..739bb7e 100644 --- a/go.mod +++ b/go.mod @@ -4,13 +4,13 @@ go 1.15 require ( github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 - github.com/deepzz0/logd v0.0.0-20171206094927-f91dd8c6316f github.com/eiblog/blackfriday v0.0.0-20161010144836-c0ec111761ae github.com/eiblog/utils v0.0.0-20181119015747-92c93e218753 github.com/gin-contrib/sessions v0.0.3 github.com/gin-gonic/gin v1.6.3 github.com/gofrs/uuid v3.3.0+incompatible github.com/golang/protobuf v1.4.2 + github.com/lib/pq v1.10.1 github.com/qiniu/go-sdk/v7 v7.9.5 github.com/sirupsen/logrus v1.4.2 github.com/swaggo/gin-swagger v1.3.0 @@ -18,6 +18,5 @@ require ( go.mongodb.org/mongo-driver v1.5.1 google.golang.org/grpc v1.35.0 google.golang.org/protobuf v1.25.0 - gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect gopkg.in/yaml.v2 v2.3.0 ) diff --git a/go.sum b/go.sum index 1d7f0b0..1860fd6 100644 --- a/go.sum +++ b/go.sum @@ -22,8 +22,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/deepzz0/logd v0.0.0-20171206094927-f91dd8c6316f h1:hjWy8ptp0ggYgv/3A8dixSB9KTRgDcZH2D3Ap5hrwOs= -github.com/deepzz0/logd v0.0.0-20171206094927-f91dd8c6316f/go.mod h1:8jMj6ab9czIU5udq3ovaK9/5sCIyQ1JWteFMn8w2QRI= github.com/eiblog/blackfriday v0.0.0-20161010144836-c0ec111761ae h1:V6YC640Gs5jEUYfCimyuXsTW5gzNcIEESG4MGmOJCtA= github.com/eiblog/blackfriday v0.0.0-20161010144836-c0ec111761ae/go.mod h1:HzHqTCGEAkSSzBM3shBvQHsHRQYUvjNOIC4mHipZ6tI= github.com/eiblog/utils v0.0.0-20181119015747-92c93e218753 h1:Nygjtnh1nF5zejJF7pZnsoFh77wOPS+jlfhikjkJg60= @@ -162,6 +160,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/lib/pq v1.10.1 h1:6VXZrLU0jHBYyAqrSPa+MgPfnSvTPuMgK+k0o5kVFWo= +github.com/lib/pq v1.10.1/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq0y3QnmWAArdw9PqbmotexnWx/FU8= @@ -352,8 +352,6 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= -gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw= -gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 6733059..b41b1c0 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -263,8 +263,7 @@ func (c *Cache) recalcLinkedList(article *model.Article, del bool) { // readdArticle 添加文章到tag、series、archive func (c *Cache) readdArticle(article *model.Article, needSort bool) { // tag - tags := strings.Split(article.Tags, ",") - for _, tag := range tags { + for _, tag := range article.Tags { c.TagArticles[tag] = append(c.TagArticles[tag], article) if needSort { sort.Sort(c.TagArticles[tag]) @@ -305,8 +304,7 @@ func (c *Cache) readdArticle(article *model.Article, needSort bool) { // redelArticle 从tag、series、archive删除文章 func (c *Cache) redelArticle(article *model.Article) { // tag - tags := strings.Split(article.Tags, ",") - for _, tag := range tags { + for _, tag := range article.Tags { for i, v := range c.TagArticles[tag] { if v == article { c.TagArticles[tag] = append(c.TagArticles[tag][0:i], c.TagArticles[tag][i+1:]...) diff --git a/pkg/core/blog/admin/admin.go b/pkg/core/blog/admin/admin.go index c9586a8..ed0b352 100644 --- a/pkg/core/blog/admin/admin.go +++ b/pkg/core/blog/admin/admin.go @@ -258,6 +258,10 @@ func handleAPIPostCreate(c *gin.Context) { err = errors.New("参数错误") return } + var tags []string + if tag != "" { + tags = strings.Split(tag, ",") + } serieid, _ := strconv.Atoi(serie) article := &model.Article{ Title: title, @@ -266,7 +270,7 @@ func handleAPIPostCreate(c *gin.Context) { IsDraft: do != "publish", Author: cache.Ei.Account.Username, SerieID: serieid, - Tags: tag, + Tags: tags, CreatedAt: date, } cid, err = strconv.Atoi(c.PostForm("cid")) diff --git a/pkg/internal/es.go b/pkg/internal/es.go index 6250d1f..9c4aedc 100644 --- a/pkg/internal/es.go +++ b/pkg/internal/es.go @@ -111,13 +111,12 @@ func ElasticAddIndex(article *model.Article) error { return err } - tags := strings.Split(article.Tags, ",") img := tools.PickFirstImage(article.Content) mapping := map[string]interface{}{ "title": article.Title, "content": tools.IgnoreHtmlTag(article.Content), "slug": article.Slug, - "tag": tags, + "tag": article.Tags, "img": img, "date": article.CreatedAt, } diff --git a/pkg/model/article.go b/pkg/model/article.go index a6c8ea6..745b960 100644 --- a/pkg/model/article.go +++ b/pkg/model/article.go @@ -1,21 +1,25 @@ // Package model provides ... package model -import "time" +import ( + "time" + + "github.com/lib/pq" +) // use snake_case as column name // Article 文章 type Article struct { - ID int `gorm:"column:id;primaryKey" bson:"id"` // 自增ID - Author string `gorm:"column:author;not null" bson:"author"` // 作者名 - Slug string `gorm:"column:slug;not null;uniqueIndex" bson:"slug"` // 文章缩略名 - Title string `gorm:"column:title;not null" bson:"title"` // 标题 - Count int `gorm:"column:count;not null" bson:"count"` // 评论数量 - Content string `gorm:"column:content;not null" bson:"content"` // markdown内容 - SerieID int `gorm:"column:serie_id;not null" bson:"serie_id"` // 专题ID - Tags string `gorm:"column:tags;not null" bson:"tags"` // tag,以逗号隔开 - IsDraft bool `gorm:"column:is_draft;not null" bson:"is_draft"` // 是否是草稿 + ID int `gorm:"column:id;primaryKey" bson:"id"` // 自增ID + Author string `gorm:"column:author;not null" bson:"author"` // 作者名 + Slug string `gorm:"column:slug;not null;uniqueIndex" bson:"slug"` // 文章缩略名 + Title string `gorm:"column:title;not null" bson:"title"` // 标题 + Count int `gorm:"column:count;not null" bson:"count"` // 评论数量 + Content string `gorm:"column:content;not null" bson:"content"` // markdown内容 + SerieID int `gorm:"column:serie_id;not null" bson:"serie_id"` // 专题ID + Tags pq.StringArray `gorm:"column:tags;type:text[];not null" bson:"tags"` // tags + IsDraft bool `gorm:"column:is_draft;not null" bson:"is_draft"` // 是否是草稿 DeletedAt time.Time `gorm:"column:deleted_at;default:null" bson:"deleted_at"` // 删除时间 UpdatedAt time.Time `gorm:"column:updated_at;default:now()" bson:"updated_at"` // 更新时间