feat: add pagination to admin post list handler and service
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
113789a972
commit
82feccf64a
2 changed files with 42 additions and 13 deletions
|
|
@ -12,29 +12,36 @@ type PostWithCategories struct {
|
|||
Categories []*models.Category
|
||||
}
|
||||
|
||||
func (s *Service) ListPosts(ctx context.Context, showDeleted bool) ([]*PostWithCategories, error) {
|
||||
type ListPostsResult struct {
|
||||
Posts []*PostWithCategories
|
||||
TotalCount int64
|
||||
}
|
||||
|
||||
func (s *Service) ListPosts(ctx context.Context, showDeleted bool, paging db.PagingParams) (ListPostsResult, error) {
|
||||
site, ok := models.GetSite(ctx)
|
||||
if !ok {
|
||||
return nil, models.SiteRequiredError
|
||||
return ListPostsResult{}, models.SiteRequiredError
|
||||
}
|
||||
|
||||
posts, err := s.db.SelectPostsOfSite(ctx, site.ID, showDeleted, db.PagingParams{
|
||||
Offset: 0,
|
||||
Limit: 25,
|
||||
})
|
||||
posts, err := s.db.SelectPostsOfSite(ctx, site.ID, showDeleted, paging)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return ListPostsResult{}, err
|
||||
}
|
||||
|
||||
count, err := s.db.CountPostsOfSite(ctx, site.ID, showDeleted)
|
||||
if err != nil {
|
||||
return ListPostsResult{}, err
|
||||
}
|
||||
|
||||
result := make([]*PostWithCategories, len(posts))
|
||||
for i, post := range posts {
|
||||
cats, err := s.db.SelectCategoriesOfPost(ctx, post.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return ListPostsResult{}, err
|
||||
}
|
||||
result[i] = &PostWithCategories{Post: post, Categories: cats}
|
||||
}
|
||||
return result, nil
|
||||
return ListPostsResult{Posts: result, TotalCount: count}, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetPost(ctx context.Context, pid int64) (*models.Post, error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue