This commit is contained in:
Tomas Dvorak
2025-10-21 15:02:05 +02:00
parent 68e69e00cc
commit 63700eedb2
103 changed files with 12442 additions and 446 deletions
+27
View File
@@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
@@ -2448,6 +2449,19 @@ func makeSlug(s string) string {
return s
}
// getPrefetchBaseURL returns the base URL for internal API calls (used for prefetch trigger)
func getPrefetchBaseURL() string {
base := strings.TrimSpace(os.Getenv("PREFETCH_TARGET"))
if base == "" {
port := strings.TrimSpace(os.Getenv("PORT"))
if port == "" {
port = "8080"
}
base = "http://127.0.0.1:" + port + "/api/v1"
}
return base
}
// CreateArticle creates a new article (protected)
func (bc *BaseController) CreateArticle(c *gin.Context) {
// Require authenticated user
@@ -2806,6 +2820,14 @@ func (bc *BaseController) UpdateArticle(c *gin.Context) {
// Best-effort: refresh published articles cache
go bc.writeArticlesCache()
// Trigger full prefetch cache update if article is published
if art.Published {
go func() {
base := getPrefetchBaseURL()
services.PrefetchOnce(base)
}()
}
// Reload article with associations and match link for complete response
bc.DB.Preload("Author").Preload("Category").First(&art, art.ID)
var matchLink models.ArticleMatchLink
@@ -2889,17 +2911,22 @@ func (bc *BaseController) GetArticles(c *gin.Context) {
}
var matchLinks []models.ArticleMatchLink
bc.DB.Where("article_id IN ?", articleIDs).Find(&matchLinks)
log.Printf("[GetArticles] Loaded %d match links for %d articles", len(matchLinks), len(items))
// Create map for quick lookup
matchLinkMap := make(map[uint]*models.ArticleMatchLink)
for i := range matchLinks {
matchLinkMap[matchLinks[i].ArticleID] = &matchLinks[i]
log.Printf("[GetArticles] Match link: article_id=%d, external_match_id=%s", matchLinks[i].ArticleID, matchLinks[i].ExternalMatchID)
}
// Assign match links to articles
matchCount := 0
for i := range items {
if ml, ok := matchLinkMap[items[i].ID]; ok {
items[i].MatchLink = ml
matchCount++
}
}
log.Printf("[GetArticles] Assigned %d match links to articles", matchCount)
}
c.JSON(http.StatusOK, gin.H{"items": items, "total": total, "page": page, "page_size": size})
}