feat: articles pagination
This commit is contained in:
1 parent
a2d224240c
commit
73f4b539fe
45 files changed
+3528
-317
No files matched your search
@@ -11,21 +11,51 @@ func NewListArticlesUseCase(articleRepository article.Repository, bodyRenderer a
|
||||
return &ListArticlesUseCase{articleRepository, bodyRenderer}
|
||||
}
|
||||
|
||||
func (u *ListArticlesUseCase) ListOnlineArticles(format article.BodyFormat) ([]article.Article, error) {
|
||||
online := true
|
||||
articles, err := u.articleRepository.FindByFilters(article.Filters{
|
||||
IsOnline: &online,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return renderArticles(u.bodyRenderer, articles, format)
|
||||
type ListArticlesCommand struct {
|
||||
Format article.BodyFormat
|
||||
Verified bool
|
||||
Page int
|
||||
Size int
|
||||
Q string
|
||||
Status string
|
||||
Sort article.Sort
|
||||
}
|
||||
|
||||
func (u *ListArticlesUseCase) ListAllArticles(format article.BodyFormat) ([]article.Article, error) {
|
||||
articles, err := u.articleRepository.FindByFilters(article.Filters{})
|
||||
type ArticlePage struct {
|
||||
Items []article.Article
|
||||
Total int
|
||||
Page int
|
||||
Size int
|
||||
Counts article.Counts
|
||||
}
|
||||
|
||||
func (u *ListArticlesUseCase) ListArticlesPage(command ListArticlesCommand) (ArticlePage, error) {
|
||||
counts, err := u.articleRepository.CountByVisibility(!command.Verified)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return ArticlePage{}, err
|
||||
}
|
||||
return renderArticles(u.bodyRenderer, articles, format)
|
||||
page := ArticlePage{Items: []article.Article{}, Page: command.Page, Size: command.Size, Counts: counts}
|
||||
if !command.Verified && command.Status == article.StatusDraft {
|
||||
return page, nil
|
||||
}
|
||||
found, err := u.articleRepository.FindPage(pageFilters(command))
|
||||
if err != nil {
|
||||
return ArticlePage{}, err
|
||||
}
|
||||
rendered, err := renderArticles(u.bodyRenderer, found.Items, command.Format)
|
||||
if err != nil {
|
||||
return ArticlePage{}, err
|
||||
}
|
||||
page.Items = rendered
|
||||
page.Total = found.Total
|
||||
return page, nil
|
||||
}
|
||||
|
||||
func pageFilters(command ListArticlesCommand) article.Filters {
|
||||
filters := article.Filters{Page: command.Page, Size: command.Size, Q: command.Q, Status: command.Status, Sort: command.Sort}
|
||||
if !command.Verified {
|
||||
online := true
|
||||
filters.IsOnline = &online
|
||||
}
|
||||
return filters
|
||||
}
|
||||
Reference in new issue
Block a user