feat: add category selection to post edit form and badges to post list

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Leon Mika 2026-03-18 21:45:28 +11:00
parent ffa86b12e9
commit 4c2ce7272d
6 changed files with 112 additions and 30 deletions

View file

@ -10,10 +10,11 @@ import (
)
type CreatePostParams struct {
GUID string `form:"guid" json:"guid"`
Title string `form:"title" json:"title"`
Body string `form:"body" json:"body"`
Action string `form:"action" json:"action"`
GUID string `form:"guid" json:"guid"`
Title string `form:"title" json:"title"`
Body string `form:"body" json:"body"`
Action string `form:"action" json:"action"`
CategoryIDs []int64 `form:"category_ids" json:"category_ids"`
}
func (s *Service) UpdatePost(ctx context.Context, params CreatePostParams) (*models.Post, error) {
@ -53,7 +54,21 @@ func (s *Service) UpdatePost(ctx context.Context, params CreatePostParams) (*mod
// Leave unchanged
}
if err := s.db.SavePost(ctx, post); err != nil {
// Use a transaction for atomicity of post save + category reassignment
tx, err := s.db.BeginTx(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()
txDB := s.db.QueriesWithTx(tx)
if err := txDB.SavePost(ctx, post); err != nil {
return nil, err
}
if err := txDB.SetPostCategories(ctx, post.ID, params.CategoryIDs); err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
}

View file

@ -7,7 +7,12 @@ import (
"lmika.dev/lmika/weiro/providers/db"
)
func (s *Service) ListPosts(ctx context.Context, showDeleted bool) ([]*models.Post, error) {
type PostWithCategories struct {
*models.Post
Categories []*models.Category
}
func (s *Service) ListPosts(ctx context.Context, showDeleted bool) ([]*PostWithCategories, error) {
site, ok := models.GetSite(ctx)
if !ok {
return nil, models.SiteRequiredError
@ -21,7 +26,15 @@ func (s *Service) ListPosts(ctx context.Context, showDeleted bool) ([]*models.Po
return nil, err
}
return posts, nil
result := make([]*PostWithCategories, len(posts))
for i, post := range posts {
cats, err := s.db.SelectCategoriesOfPost(ctx, post.ID)
if err != nil {
return nil, err
}
result[i] = &PostWithCategories{Post: post, Categories: cats}
}
return result, nil
}
func (s *Service) GetPost(ctx context.Context, pid int64) (*models.Post, error) {
@ -32,3 +45,7 @@ func (s *Service) GetPost(ctx context.Context, pid int64) (*models.Post, error)
return post, nil
}
func (s *Service) GetPostCategories(ctx context.Context, postID int64) ([]*models.Category, error) {
return s.db.SelectCategoriesOfPost(ctx, postID)
}