🐛 Fix batch list reactions cannot work properly
This commit is contained in:
		@@ -61,7 +61,8 @@ func listArticle(c *fiber.Ctx) error {
 | 
				
			|||||||
		tx = services.FilterArticleWithTag(tx, c.Query("tag"))
 | 
							tx = services.FilterArticleWithTag(tx, c.Query("tag"))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	count, err := services.CountArticle(tx)
 | 
						counTx := tx
 | 
				
			||||||
 | 
						count, err := services.CountArticle(counTx)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return fiber.NewError(fiber.StatusInternalServerError, err.Error())
 | 
							return fiber.NewError(fiber.StatusInternalServerError, err.Error())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -62,7 +62,8 @@ func listPost(c *fiber.Ctx) error {
 | 
				
			|||||||
		tx = services.FilterPostWithTag(tx, c.Query("tag"))
 | 
							tx = services.FilterPostWithTag(tx, c.Query("tag"))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	count, err := services.CountPost(tx)
 | 
						countTx := tx
 | 
				
			||||||
 | 
						count, err := services.CountPost(countTx)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return fiber.NewError(fiber.StatusInternalServerError, err.Error())
 | 
							return fiber.NewError(fiber.StatusInternalServerError, err.Error())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -121,7 +121,7 @@ func ListArticle(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// Load reactions
 | 
						// Load reactions
 | 
				
			||||||
	if len(noReact) <= 0 || !noReact[0] {
 | 
						if len(noReact) <= 0 || !noReact[0] {
 | 
				
			||||||
		if mapping, err := BatchListResourceReactions(database.C.Where("article_id IN ?", idx)); err != nil {
 | 
							if mapping, err := BatchListResourceReactions(database.C.Where("article_id IN ?", idx), "article_id"); err != nil {
 | 
				
			||||||
			return items, err
 | 
								return items, err
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			itemMap := lo.SliceToMap(items, func(item *models.Article) (uint, *models.Article) {
 | 
								itemMap := lo.SliceToMap(items, func(item *models.Article) (uint, *models.Article) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -152,7 +152,7 @@ func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.Pos
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// Load reactions
 | 
						// Load reactions
 | 
				
			||||||
	if len(noReact) <= 0 || !noReact[0] {
 | 
						if len(noReact) <= 0 || !noReact[0] {
 | 
				
			||||||
		if mapping, err := BatchListResourceReactions(database.C.Where("post_id IN ?", idx)); err != nil {
 | 
							if mapping, err := BatchListResourceReactions(database.C.Where("post_id IN ?", idx), "post_id"); err != nil {
 | 
				
			||||||
			return items, err
 | 
								return items, err
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			itemMap := lo.SliceToMap(items, func(item *models.Post) (uint, *models.Post) {
 | 
								itemMap := lo.SliceToMap(items, func(item *models.Post) (uint, *models.Post) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,8 @@
 | 
				
			|||||||
package services
 | 
					package services
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
 | 
						"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
 | 
				
			||||||
	"github.com/samber/lo"
 | 
						"github.com/samber/lo"
 | 
				
			||||||
	"gorm.io/gorm"
 | 
						"gorm.io/gorm"
 | 
				
			||||||
@@ -28,26 +30,26 @@ func ListResourceReactions(tx *gorm.DB) (map[string]int64, error) {
 | 
				
			|||||||
	}), nil
 | 
						}), nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func BatchListResourceReactions(tx *gorm.DB) (map[uint]map[string]int64, error) {
 | 
					func BatchListResourceReactions(tx *gorm.DB, indexField string) (map[uint]map[string]int64, error) {
 | 
				
			||||||
	var reactions []struct {
 | 
						var reactions []struct {
 | 
				
			||||||
		ArticleID uint
 | 
							ID     uint
 | 
				
			||||||
		Symbol string
 | 
							Symbol string
 | 
				
			||||||
		Count  int64
 | 
							Count  int64
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	reactInfo := map[uint]map[string]int64{}
 | 
						reactInfo := map[uint]map[string]int64{}
 | 
				
			||||||
	if err := tx.Model(&models.Reaction{}).
 | 
						if err := tx.Model(&models.Reaction{}).
 | 
				
			||||||
		Select("article_id, symbol, COUNT(id) as count").
 | 
							Select(fmt.Sprintf("%s as id, symbol, COUNT(id) as count"), indexField).
 | 
				
			||||||
		Group("article_id, symbol").
 | 
							Group("id, symbol").
 | 
				
			||||||
		Scan(&reactions).Error; err != nil {
 | 
							Scan(&reactions).Error; err != nil {
 | 
				
			||||||
		return reactInfo, err
 | 
							return reactInfo, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, info := range reactions {
 | 
						for _, info := range reactions {
 | 
				
			||||||
		if _, ok := reactInfo[info.ArticleID]; !ok {
 | 
							if _, ok := reactInfo[info.ID]; !ok {
 | 
				
			||||||
			reactInfo[info.ArticleID] = make(map[string]int64)
 | 
								reactInfo[info.ID] = make(map[string]int64)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		reactInfo[info.ArticleID][info.Symbol] = info.Count
 | 
							reactInfo[info.ID][info.Symbol] = info.Count
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return reactInfo, nil
 | 
						return reactInfo, nil
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user