mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-04 02:32:57 +00:00
dev day #65
This commit is contained in:
@@ -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})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user