Files
RenewCMS/internal/infrastructure/useCases/ListSitemap.go
T

71 lines
1.9 KiB
Go

package useCases
import (
"RenewCMS/internal/domain/article"
"RenewCMS/internal/domain/language"
"RenewCMS/internal/domain/localization"
"RenewCMS/internal/domain/site"
"time"
)
type ListSitemapUseCase struct {
articleRepository article.Repository
sites site.Repository
localization localization.Repository
}
func NewListSitemapUseCase(articleRepository article.Repository, sites site.Repository, localizationRepository localization.Repository) *ListSitemapUseCase {
return &ListSitemapUseCase{articleRepository: articleRepository, sites: sites, localization: localizationRepository}
}
type SitemapEntry struct {
URL string
ModifiedAt time.Time
Noindex bool
Lang string
}
type SitemapPage struct {
Entries []SitemapEntry
Total int
}
func (u *ListSitemapUseCase) List(siteKey string, page int, size int) (SitemapPage, error) {
owner, err := u.sites.GetByKey(siteKey)
if err != nil {
return SitemapPage{}, err
}
rows, err := u.localization.Rows(owner.ID)
if err != nil {
return SitemapPage{}, err
}
langs := []string{language.Unassigned}
for _, row := range rows {
if row.Enabled {
langs = append(langs, row.Tag)
}
}
online := true
found, err := u.articleRepository.FindPage(article.Filters{
IsOnline: &online,
SiteID: &owner.ID,
Langs: &langs,
Page: page,
Size: size,
Sort: article.Sort{Field: article.SortByID, Direction: article.SortAsc},
})
if err != nil {
return SitemapPage{}, err
}
entries := make([]SitemapEntry, 0, len(found.Items))
for _, item := range found.Items {
entries = append(entries, SitemapEntry{URL: sitemapURL(owner, item), ModifiedAt: item.UpdatedAt, Noindex: item.Noindex, Lang: item.Lang})
}
return SitemapPage{Entries: entries, Total: found.Total}, nil
}
func sitemapURL(owner site.Site, item article.Article) string {
url, _ := owner.ArticleURL(item.Lang, item.Slug)
return url
}