chore: tags use array

This commit is contained in:
deepzz0
2021-04-28 12:50:06 +08:00
parent 9f563f0ae9
commit beea4f1746
6 changed files with 25 additions and 23 deletions

6
pkg/cache/cache.go vendored
View File

@@ -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:]...)

View File

@@ -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"))

View File

@@ -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,
}

View File

@@ -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"` // 更新时间