From 7f79c1a5adb6c43f60c35d9f71be318955bcee1a Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 16 Jul 2024 16:52:00 +0800 Subject: [PATCH] :bug: Fix count metrics issue --- pkg/internal/models/metrics.go | 4 ++-- pkg/internal/services/articles.go | 8 +++++--- pkg/internal/services/posts.go | 14 +++++++++----- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pkg/internal/models/metrics.go b/pkg/internal/models/metrics.go index c033268..bdc61aa 100644 --- a/pkg/internal/models/metrics.go +++ b/pkg/internal/models/metrics.go @@ -1,7 +1,7 @@ package models type PostMetric struct { - ReplyCount int64 `json:"reply_count,omitempty"` - ReactionCount int64 `json:"reaction_count,omitempty"` + ReplyCount int64 `json:"reply_count"` + ReactionCount int64 `json:"reaction_count"` ReactionList map[string]int64 `json:"reaction_list,omitempty"` } diff --git a/pkg/internal/services/articles.go b/pkg/internal/services/articles.go index 19d4d79..84835d5 100644 --- a/pkg/internal/services/articles.go +++ b/pkg/internal/services/articles.go @@ -106,8 +106,8 @@ func CountArticleReactions(id uint) int64 { } func ListArticle(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.Article, error) { - if take > 20 { - take = 20 + if take > 100 { + take = 100 } var items []*models.Article @@ -137,7 +137,9 @@ func ListArticle(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models. for k, v := range mapping { if post, ok := itemMap[k]; ok { - post.Metric.ReactionList = v + post.Metric = models.PostMetric{ + ReactionList: v, + } } } } diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 8c2866b..f56d0c8 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -141,8 +141,8 @@ func CountPostReactions(id uint) int64 { } func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.Post, error) { - if take > 20 { - take = 20 + if take > 100 { + take = 100 } var items []*models.Post @@ -166,7 +166,6 @@ func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.Pos } idx := lo.Map(items, func(item *models.Post, index int) uint { - item.Metric = models.PostMetric{} return item.ID }) @@ -181,7 +180,9 @@ func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.Pos for k, v := range mapping { if post, ok := itemMap[k]; ok { - post.Metric.ReactionList = v + post.Metric = models.PostMetric{ + ReactionList: v, + } } } } @@ -213,7 +214,10 @@ func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.Pos for k, v := range list { if post, ok := itemMap[k]; ok { - post.Metric.ReplyCount = v + post.Metric = models.PostMetric{ + ReactionList: post.Metric.ReactionList, + ReplyCount: v, + } } } }