2026-02-21 23:09:34 +00:00
|
|
|
package posts
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"lmika.dev/lmika/weiro/models"
|
2026-03-05 11:04:24 +00:00
|
|
|
"lmika.dev/lmika/weiro/providers/db"
|
2026-02-21 23:09:34 +00:00
|
|
|
)
|
|
|
|
|
|
2026-03-18 10:45:28 +00:00
|
|
|
type PostWithCategories struct {
|
|
|
|
|
*models.Post
|
|
|
|
|
Categories []*models.Category
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 03:33:31 +00:00
|
|
|
type ListPostsResult struct {
|
|
|
|
|
Posts []*PostWithCategories
|
|
|
|
|
TotalCount int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) ListPosts(ctx context.Context, showDeleted bool, paging db.PagingParams) (ListPostsResult, error) {
|
2026-02-21 23:09:34 +00:00
|
|
|
site, ok := models.GetSite(ctx)
|
|
|
|
|
if !ok {
|
2026-03-22 03:33:31 +00:00
|
|
|
return ListPostsResult{}, models.SiteRequiredError
|
2026-02-21 23:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-22 03:33:31 +00:00
|
|
|
posts, err := s.db.SelectPostsOfSite(ctx, site.ID, showDeleted, paging)
|
2026-02-21 23:09:34 +00:00
|
|
|
if err != nil {
|
2026-03-22 03:33:31 +00:00
|
|
|
return ListPostsResult{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
count, err := s.db.CountPostsOfSite(ctx, site.ID, showDeleted)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ListPostsResult{}, err
|
2026-02-21 23:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-18 10:45:28 +00:00
|
|
|
result := make([]*PostWithCategories, len(posts))
|
|
|
|
|
for i, post := range posts {
|
|
|
|
|
cats, err := s.db.SelectCategoriesOfPost(ctx, post.ID)
|
|
|
|
|
if err != nil {
|
2026-03-22 03:33:31 +00:00
|
|
|
return ListPostsResult{}, err
|
2026-03-18 10:45:28 +00:00
|
|
|
}
|
|
|
|
|
result[i] = &PostWithCategories{Post: post, Categories: cats}
|
|
|
|
|
}
|
2026-03-22 03:33:31 +00:00
|
|
|
return ListPostsResult{Posts: result, TotalCount: count}, nil
|
2026-02-21 23:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) GetPost(ctx context.Context, pid int64) (*models.Post, error) {
|
2026-02-23 10:35:12 +00:00
|
|
|
post, _, err := s.fetchPostAndSite(ctx, pid)
|
2026-02-21 23:09:34 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return post, nil
|
|
|
|
|
}
|
2026-03-18 10:45:28 +00:00
|
|
|
|
|
|
|
|
func (s *Service) GetPostCategories(ctx context.Context, postID int64) ([]*models.Category, error) {
|
|
|
|
|
return s.db.SelectCategoriesOfPost(ctx, postID)
|
|
|
|
|
}
|