weiro/services/posts/list.go
Leon Mika 82feccf64a feat: add pagination to admin post list handler and service
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-22 14:33:31 +11:00

59 lines
1.4 KiB
Go

package posts
import (
"context"
"lmika.dev/lmika/weiro/models"
"lmika.dev/lmika/weiro/providers/db"
)
type PostWithCategories struct {
*models.Post
Categories []*models.Category
}
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 ListPostsResult{}, models.SiteRequiredError
}
posts, err := s.db.SelectPostsOfSite(ctx, site.ID, showDeleted, paging)
if err != nil {
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 ListPostsResult{}, err
}
result[i] = &PostWithCategories{Post: post, Categories: cats}
}
return ListPostsResult{Posts: result, TotalCount: count}, nil
}
func (s *Service) GetPost(ctx context.Context, pid int64) (*models.Post, error) {
post, _, err := s.fetchPostAndSite(ctx, pid)
if err != nil {
return nil, err
}
return post, nil
}
func (s *Service) GetPostCategories(ctx context.Context, postID int64) ([]*models.Category, error) {
return s.db.SelectCategoriesOfPost(ctx, postID)
}