🐛 Fix count metrics issue
This commit is contained in:
		@@ -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"`
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user