feat: implement tag management and search functionality
Features: - Tag system with multi-language support (Chinese/English) - Auto-create tags when associating with articles - Tag filtering on homepage with visual indicators - Full-text search across title, summary, and content - Tag cloud sidebar showing article counts - Search page with results display Technical Implementation: - Database models: Tag, ArticleTag (many-to-many) - Tag count caching for performance - Automatic tag slug generation - Responsive design with mobile support - I18n support for all new UI elements Bug Fix: - Fixed SQL column ambiguity error in tag filtering - Added table prefix to ORDER BY clause in publishedArticleOrder Routes: - GET /search - Search results page - GET /?tag=<slug> - Filter articles by tag - GET /api/articles?tag=<slug> - API with tag filter support Templates: - Added tag input field to article create/edit form - Added search box to homepage header - Added tag cloud sidebar on homepage and search page - Created new search results page template Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
+113
-7
@@ -3,6 +3,7 @@ package handlers
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
@@ -14,7 +15,7 @@ import (
|
||||
|
||||
// publishedArticleOrder is the ordering used to list published articles:
|
||||
// pinned first, then by most recent publish/creation time.
|
||||
const publishedArticleOrder = "is_top DESC, published_at DESC, created_at DESC"
|
||||
const publishedArticleOrder = "articles.is_top DESC, articles.published_at DESC, articles.created_at DESC"
|
||||
|
||||
// HomePage renders the public home page with the latest published articles.
|
||||
func HomePage(db *gorm.DB) gin.HandlerFunc {
|
||||
@@ -23,12 +24,35 @@ func HomePage(db *gorm.DB) gin.HandlerFunc {
|
||||
data := DefaultData(c)
|
||||
data["Title"] = tr["page_home"]
|
||||
|
||||
// Get tag filter if present
|
||||
tagSlug := c.Query("tag")
|
||||
|
||||
// Build query
|
||||
query := db.Where("status = ?", models.ArticlePublished)
|
||||
|
||||
if tagSlug != "" {
|
||||
// Join with article_tags to filter by tag
|
||||
query = query.Joins("JOIN article_tags ON article_tags.article_id = articles.id").
|
||||
Joins("JOIN tags ON tags.id = article_tags.tag_id").
|
||||
Where("tags.slug = ?", tagSlug)
|
||||
|
||||
// Get tag info for display
|
||||
var tag models.Tag
|
||||
if err := db.Where("slug = ?", tagSlug).First(&tag).Error; err == nil {
|
||||
data["FilterTag"] = tag
|
||||
}
|
||||
}
|
||||
|
||||
var articles []models.Article
|
||||
db.Where("status = ?", models.ArticlePublished).
|
||||
query.Preload("Tags").
|
||||
Order(publishedArticleOrder).
|
||||
Limit(10).
|
||||
Find(&articles)
|
||||
|
||||
// Load all tags for sidebar
|
||||
var tags []models.Tag
|
||||
db.Where("count > 0").Order("count DESC, name_zh ASC").Find(&tags)
|
||||
|
||||
// Get comment counts for all articles
|
||||
articleIDs := make([]uint, len(articles))
|
||||
for i, article := range articles {
|
||||
@@ -55,8 +79,9 @@ func HomePage(db *gorm.DB) gin.HandlerFunc {
|
||||
commentCountMap[cc.ArticleID] = cc.Count
|
||||
}
|
||||
|
||||
// Add comment counts to template data
|
||||
// Add data to template
|
||||
data["Articles"] = articles
|
||||
data["Tags"] = tags
|
||||
data["CommentCounts"] = commentCountMap
|
||||
|
||||
c.HTML(http.StatusOK, "home", data)
|
||||
@@ -77,17 +102,33 @@ func HomeArticlesAPI(db *gorm.DB) gin.HandlerFunc {
|
||||
pageSize := 10
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
// Get tag filter if present
|
||||
tagSlug := c.Query("tag")
|
||||
|
||||
// Build query
|
||||
query := db.Where("status = ?", models.ArticlePublished)
|
||||
countQuery := db.Model(&models.Article{}).Where("status = ?", models.ArticlePublished)
|
||||
|
||||
if tagSlug != "" {
|
||||
// Join with article_tags to filter by tag
|
||||
query = query.Joins("JOIN article_tags ON article_tags.article_id = articles.id").
|
||||
Joins("JOIN tags ON tags.id = article_tags.tag_id").
|
||||
Where("tags.slug = ?", tagSlug)
|
||||
|
||||
countQuery = countQuery.Joins("JOIN article_tags ON article_tags.article_id = articles.id").
|
||||
Joins("JOIN tags ON tags.id = article_tags.tag_id").
|
||||
Where("tags.slug = ?", tagSlug)
|
||||
}
|
||||
|
||||
var articles []models.Article
|
||||
db.Where("status = ?", models.ArticlePublished).
|
||||
query.Preload("Tags").
|
||||
Order(publishedArticleOrder).
|
||||
Limit(pageSize).
|
||||
Offset(offset).
|
||||
Find(&articles)
|
||||
|
||||
var total int64
|
||||
db.Model(&models.Article{}).
|
||||
Where("status = ?", models.ArticlePublished).
|
||||
Count(&total)
|
||||
countQuery.Count(&total)
|
||||
|
||||
// Get comment counts for these articles
|
||||
articleIDs := make([]uint, len(articles))
|
||||
@@ -303,3 +344,68 @@ func recordArticleView(db *gorm.DB, articleID uint, c *gin.Context) {
|
||||
db.Create(&view)
|
||||
// Ignore errors - this is a best-effort tracking system
|
||||
}
|
||||
|
||||
// SearchPage handles article search by keyword.
|
||||
func SearchPage(db *gorm.DB) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
tr := getTr(c)
|
||||
data := DefaultData(c)
|
||||
|
||||
keyword := strings.TrimSpace(c.Query("q"))
|
||||
data["SearchKeyword"] = keyword
|
||||
data["Title"] = tr["search_title"]
|
||||
|
||||
if keyword == "" {
|
||||
data["Articles"] = []models.Article{}
|
||||
data["Tags"] = []models.Tag{}
|
||||
data["CommentCounts"] = make(map[uint]int64)
|
||||
c.HTML(http.StatusOK, "search", data)
|
||||
return
|
||||
}
|
||||
|
||||
// Search in title, summary, and content
|
||||
searchPattern := "%" + keyword + "%"
|
||||
var articles []models.Article
|
||||
db.Where("status = ?", models.ArticlePublished).
|
||||
Where("title LIKE ? OR summary LIKE ? OR content LIKE ?", searchPattern, searchPattern, searchPattern).
|
||||
Preload("Tags").
|
||||
Order(publishedArticleOrder).
|
||||
Limit(50). // Limit search results
|
||||
Find(&articles)
|
||||
|
||||
// Load all tags for sidebar
|
||||
var tags []models.Tag
|
||||
db.Where("count > 0").Order("count DESC, name_zh ASC").Find(&tags)
|
||||
|
||||
// Get comment counts
|
||||
articleIDs := make([]uint, len(articles))
|
||||
for i, article := range articles {
|
||||
articleIDs[i] = article.ID
|
||||
}
|
||||
|
||||
type CommentCount struct {
|
||||
ArticleID uint
|
||||
Count int64
|
||||
}
|
||||
var commentCounts []CommentCount
|
||||
if len(articleIDs) > 0 {
|
||||
db.Model(&models.Comment{}).
|
||||
Select("article_id, COUNT(*) as count").
|
||||
Where("article_id IN ?", articleIDs).
|
||||
Where("status = ?", models.CommentApproved).
|
||||
Group("article_id").
|
||||
Scan(&commentCounts)
|
||||
}
|
||||
|
||||
commentCountMap := make(map[uint]int64)
|
||||
for _, cc := range commentCounts {
|
||||
commentCountMap[cc.ArticleID] = cc.Count
|
||||
}
|
||||
|
||||
data["Articles"] = articles
|
||||
data["Tags"] = tags
|
||||
data["CommentCounts"] = commentCountMap
|
||||
|
||||
c.HTML(http.StatusOK, "search", data)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user