摘要为空取前120字符,删除tag同步删除关联
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"goblog/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 获取标签列表
|
||||
@@ -103,7 +104,13 @@ func DeleteTag(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := database.DB.Delete(&tag).Error; err != nil {
|
||||
// 使用事务:先清除 post_tags 中该标签的关联记录,再删除标签本身,保证数据一致性
|
||||
if err := database.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Exec("DELETE FROM post_tags WHERE tag_id = ?", tag.ID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Delete(&tag).Error
|
||||
}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "删除标签失败"})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -131,10 +131,10 @@ func IndexView(c *gin.Context) {
|
||||
|
||||
// 如果摘要为空,则从内容中提取摘要
|
||||
for i := range posts {
|
||||
if posts[i].Summary == "" && posts[i].Content != "" && len(posts[i].Content) > 120 {
|
||||
posts[i].Summary = posts[i].Content[:120]
|
||||
} else {
|
||||
posts[i].Summary = posts[i].Content
|
||||
p := &posts[i]
|
||||
if p.Summary == "" && p.SearchableText != "" {
|
||||
runes := []rune(p.SearchableText)
|
||||
p.Summary = string(runes[:min(len(runes), 120)])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user