This commit is contained in:
Tomas Dvorak
2025-10-19 17:16:57 +02:00
parent e9a63073e5
commit 77213f4e83
76 changed files with 9728 additions and 935 deletions
+46
View File
@@ -553,6 +553,11 @@ func (bc *BaseController) GetArticle(c *gin.Context) {
if art.ReadTime == 0 {
art.ReadTime = computeEstimatedReadMinutes(art.Content)
}
// Load match link if exists
var matchLink models.ArticleMatchLink
if err := bc.DB.Where("article_id = ?", art.ID).First(&matchLink).Error; err == nil {
art.MatchLink = &matchLink
}
c.JSON(http.StatusOK, art)
}
@@ -712,6 +717,11 @@ func (bc *BaseController) GetArticleBySlug(c *gin.Context) {
if art.ReadTime == 0 {
art.ReadTime = computeEstimatedReadMinutes(art.Content)
}
// Load match link if exists
var matchLink models.ArticleMatchLink
if err := bc.DB.Where("article_id = ?", art.ID).First(&matchLink).Error; err == nil {
art.MatchLink = &matchLink
}
c.JSON(http.StatusOK, art)
}
@@ -2619,6 +2629,14 @@ func (bc *BaseController) CreateArticle(c *gin.Context) {
// Best-effort: refresh published articles cache
go bc.writeArticlesCache()
// Reload article with associations and match link for complete response
bc.DB.Preload("Author").Preload("Category").First(&art, art.ID)
var matchLink models.ArticleMatchLink
if err := bc.DB.Where("article_id = ?", art.ID).First(&matchLink).Error; err == nil {
art.MatchLink = &matchLink
}
c.JSON(http.StatusCreated, art)
}
@@ -2787,6 +2805,14 @@ func (bc *BaseController) UpdateArticle(c *gin.Context) {
// Best-effort: refresh published articles cache
go bc.writeArticlesCache()
// Reload article with associations and match link for complete response
bc.DB.Preload("Author").Preload("Category").First(&art, art.ID)
var matchLink models.ArticleMatchLink
if err := bc.DB.Where("article_id = ?", art.ID).First(&matchLink).Error; err == nil {
art.MatchLink = &matchLink
}
c.JSON(http.StatusOK, art)
}
@@ -2855,6 +2881,26 @@ func (bc *BaseController) GetArticles(c *gin.Context) {
items[i].ImageURL = "/dist/img/logo-club-empty.svg"
}
}
// Batch load match links for all articles
if len(items) > 0 {
articleIDs := make([]uint, len(items))
for i, art := range items {
articleIDs[i] = art.ID
}
var matchLinks []models.ArticleMatchLink
bc.DB.Where("article_id IN ?", articleIDs).Find(&matchLinks)
// Create map for quick lookup
matchLinkMap := make(map[uint]*models.ArticleMatchLink)
for i := range matchLinks {
matchLinkMap[matchLinks[i].ArticleID] = &matchLinks[i]
}
// Assign match links to articles
for i := range items {
if ml, ok := matchLinkMap[items[i].ID]; ok {
items[i].MatchLink = ml
}
}
}
c.JSON(http.StatusOK, gin.H{"items": items, "total": total, "page": page, "page_size": size})
}