48 lines
1.5 KiB
Go
48 lines
1.5 KiB
Go
package persistence
|
|
|
|
import (
|
|
domain "RenewCMS/internal/domain/article"
|
|
entity "RenewCMS/internal/infrastructure/persistence/models"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const (
|
|
seoStatusColumn = "seo_status"
|
|
seoSuggestionsColumn = "seo_suggestions"
|
|
seoVersionColumn = "seo_version"
|
|
)
|
|
|
|
func withStaleSEO(columns map[string]any) map[string]any {
|
|
columns[seoStatusColumn] = ""
|
|
columns[seoVersionColumn] = gorm.Expr(seoVersionColumn + " + 1")
|
|
return columns
|
|
}
|
|
|
|
func (a *ArticleRepository) StaleSEO(afterID uint32, limit int) ([]domain.Article, error) {
|
|
var found []entity.Article
|
|
err := a.db.Model(&entity.Article{}).Where(seoStatusColumn+" = ? AND id > ?", "", afterID).Order("id ASC").Limit(limit).Find(&found).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return a.withAuthors(a.db, toDomainArticles(found))
|
|
}
|
|
|
|
func (a *ArticleRepository) StoreSEO(records []domain.SEORecord) error {
|
|
return a.db.Transaction(func(tx *gorm.DB) error {
|
|
for _, record := range records {
|
|
stored := map[string]any{seoStatusColumn: record.Summary.Status, seoSuggestionsColumn: record.Summary.Suggestions}
|
|
if err := tx.Model(&entity.Article{}).Where("id = ? AND "+seoVersionColumn+" = ?", record.ArticleID, record.Version).UpdateColumns(stored).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (a *ArticleRepository) MarkSiteSEOStale(siteID uint32) error {
|
|
return a.db.Model(&entity.Article{}).Where("site_id = ?", siteID).UpdateColumns(withStaleSEO(map[string]any{})).Error
|
|
}
|
|
|
|
var _ domain.SEOIndex = &ArticleRepository{}
|