From 890148d5807e7a0369f2d1f87a6148b045608724 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 25 May 2024 17:43:26 +0800 Subject: [PATCH] :bug: Fix list posts have no reply count --- pkg/services/posts.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/pkg/services/posts.go b/pkg/services/posts.go index aff71b2..05ae904 100644 --- a/pkg/services/posts.go +++ b/pkg/services/posts.go @@ -3,6 +3,8 @@ package services import ( "errors" "fmt" + "time" + "git.solsynth.dev/hydrogen/interactive/pkg/database" "git.solsynth.dev/hydrogen/interactive/pkg/models" "git.solsynth.dev/hydrogen/passport/pkg/grpc/proto" @@ -10,7 +12,6 @@ import ( "github.com/samber/lo" "github.com/spf13/viper" "gorm.io/gorm" - "time" ) func FilterPostWithCategory(tx *gorm.DB, alias string) *gorm.DB { @@ -197,6 +198,36 @@ func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.Pos } } + if len(noReact) <= 0 || !noReact[0] { + var replies []struct { + PostID uint + Count int64 + } + + if err := database.C.Model(&models.Post{}). + Select("id as post_id, COUNT(id) as count"). + Where("reply_id IN (?)", idx). + Group("post_id"). + Scan(&replies).Error; err != nil { + return items, err + } + + itemMap := lo.SliceToMap(items, func(item *models.Post) (uint, *models.Post) { + return item.ID, item + }) + + list := map[uint]int64{} + for _, info := range replies { + list[info.PostID] = info.Count + } + + for k, v := range list { + if post, ok := itemMap[k]; ok { + post.ReplyCount = v + } + } + } + return items, nil }