🐛 Fix reaction and comment didn't count in spec api

This commit is contained in:
LittleSheep 2024-03-25 19:47:51 +08:00
parent 562af023f1
commit 97e1d1f87e
2 changed files with 26 additions and 2 deletions

View File

@ -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())
}

View File

@ -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