🐛 Fix count metrics issue

This commit is contained in:
LittleSheep 2024-07-16 16:52:00 +08:00
parent 06acabcd1a
commit 7f79c1a5ad
3 changed files with 16 additions and 10 deletions

View File

@ -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"`
}

View File

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

View File

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