diff --git a/pkg/server/posts_api.go b/pkg/server/posts_api.go index fe81cf8..b3192ab 100644 --- a/pkg/server/posts_api.go +++ b/pkg/server/posts_api.go @@ -40,7 +40,9 @@ func getPost(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusNotFound, err.Error()) } - item.ReactionList, err = mx.CountReactions(item.ID) + item.CommentCount = mx.CountComments(item.ID) + item.ReactionCount = mx.CountReactions(item.ID) + item.ReactionList, err = mx.ListReactions(item.ID) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } diff --git a/pkg/services/posts.go b/pkg/services/posts.go index a75e0f8..7391af7 100644 --- a/pkg/services/posts.go +++ b/pkg/services/posts.go @@ -133,7 +133,29 @@ func (v *PostTypeContext) Count() (int64, error) { return count, nil } -func (v *PostTypeContext) CountReactions(id uint) (map[string]int64, error) { +func (v *PostTypeContext) CountComments(id uint) int64 { + var count int64 + if err := database.C.Model(&models.Comment{}). + Where(v.ColumnName+"_id = ?", id). + Count(&count).Error; err != nil { + return 0 + } + + return count +} + +func (v *PostTypeContext) CountReactions(id uint) int64 { + var count int64 + if err := database.C.Model(&models.Reaction{}). + Where(v.ColumnName+"_id = ?", id). + Count(&count).Error; err != nil { + return 0 + } + + return count +} + +func (v *PostTypeContext) ListReactions(id uint) (map[string]int64, error) { var reactions []struct { Symbol string Count int64