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:
Leon Mika 2026-03-22 14:33:31 +11:00
parent 113789a972
commit 82feccf64a
2 changed files with 42 additions and 13 deletions

View file

@ -6,6 +6,7 @@ import (
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"lmika.dev/lmika/weiro/models" "lmika.dev/lmika/weiro/models"
"lmika.dev/lmika/weiro/providers/db"
"lmika.dev/lmika/weiro/services/categories" "lmika.dev/lmika/weiro/services/categories"
"lmika.dev/lmika/weiro/services/posts" "lmika.dev/lmika/weiro/services/posts"
) )
@ -18,22 +19,43 @@ type PostsHandler struct {
func (ph PostsHandler) Index(c fiber.Ctx) error { func (ph PostsHandler) Index(c fiber.Ctx) error {
var req struct { var req struct {
Filter string `query:"filter"` Filter string `query:"filter"`
Page int `query:"page"`
} }
if err := c.Bind().Query(&req); err != nil { if err := c.Bind().Query(&req); err != nil {
return fiber.ErrBadRequest return fiber.ErrBadRequest
} }
posts, err := ph.PostService.ListPosts(c.Context(), req.Filter == "deleted") const perPage = 25
if req.Page < 1 {
req.Page = 1
}
result, err := ph.PostService.ListPosts(c.Context(), req.Filter == "deleted", db.PagingParams{
Offset: int64((req.Page - 1) * perPage),
Limit: perPage,
})
if err != nil { if err != nil {
return err return err
} }
totalPages := int(result.TotalCount+int64(perPage)-1) / perPage
if totalPages < 1 {
totalPages = 1
}
pageInfo := models.PageInfo{
CurrentPage: req.Page,
TotalPages: totalPages,
PostsPerPage: perPage,
}
return accepts(c, json(func() any { return accepts(c, json(func() any {
return posts return result.Posts
}), html(func(c fiber.Ctx) error { }), html(func(c fiber.Ctx) error {
return c.Render("posts/index", fiber.Map{ return c.Render("posts/index", fiber.Map{
"req": req, "req": req,
"posts": posts, "posts": result.Posts,
"pageInfo": pageInfo,
}) })
})) }))
} }

View file

@ -12,29 +12,36 @@ type PostWithCategories struct {
Categories []*models.Category Categories []*models.Category
} }
func (s *Service) ListPosts(ctx context.Context, showDeleted bool) ([]*PostWithCategories, error) { type ListPostsResult struct {
site, ok := models.GetSite(ctx) Posts []*PostWithCategories
if !ok { TotalCount int64
return nil, models.SiteRequiredError
} }
posts, err := s.db.SelectPostsOfSite(ctx, site.ID, showDeleted, db.PagingParams{ func (s *Service) ListPosts(ctx context.Context, showDeleted bool, paging db.PagingParams) (ListPostsResult, error) {
Offset: 0, site, ok := models.GetSite(ctx)
Limit: 25, if !ok {
}) return ListPostsResult{}, models.SiteRequiredError
}
posts, err := s.db.SelectPostsOfSite(ctx, site.ID, showDeleted, paging)
if err != nil { 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)) result := make([]*PostWithCategories, len(posts))
for i, post := range posts { for i, post := range posts {
cats, err := s.db.SelectCategoriesOfPost(ctx, post.ID) cats, err := s.db.SelectCategoriesOfPost(ctx, post.ID)
if err != nil { if err != nil {
return nil, err return ListPostsResult{}, err
} }
result[i] = &PostWithCategories{Post: post, Categories: cats} 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) { func (s *Service) GetPost(ctx context.Context, pid int64) (*models.Post, error) {