diff --git a/pkg/internal/server/api/articles_api.go b/pkg/internal/server/api/articles_api.go index 42027ae..3d80045 100644 --- a/pkg/internal/server/api/articles_api.go +++ b/pkg/internal/server/api/articles_api.go @@ -61,7 +61,8 @@ func listArticle(c *fiber.Ctx) error { tx = services.FilterArticleWithTag(tx, c.Query("tag")) } - count, err := services.CountArticle(tx) + counTx := tx + count, err := services.CountArticle(counTx) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } diff --git a/pkg/internal/server/api/posts_api.go b/pkg/internal/server/api/posts_api.go index 187b889..860dfc2 100644 --- a/pkg/internal/server/api/posts_api.go +++ b/pkg/internal/server/api/posts_api.go @@ -62,7 +62,8 @@ func listPost(c *fiber.Ctx) error { tx = services.FilterPostWithTag(tx, c.Query("tag")) } - count, err := services.CountPost(tx) + countTx := tx + count, err := services.CountPost(countTx) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } diff --git a/pkg/internal/services/articles.go b/pkg/internal/services/articles.go index cdcc9e0..ee37aa9 100644 --- a/pkg/internal/services/articles.go +++ b/pkg/internal/services/articles.go @@ -121,7 +121,7 @@ func ListArticle(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models. // Load reactions 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 } else { itemMap := lo.SliceToMap(items, func(item *models.Article) (uint, *models.Article) { diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index c2c1fe5..0776a8e 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -152,7 +152,7 @@ func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.Pos // Load reactions 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 } else { itemMap := lo.SliceToMap(items, func(item *models.Post) (uint, *models.Post) { diff --git a/pkg/internal/services/reactions.go b/pkg/internal/services/reactions.go index 56568ef..ff96985 100644 --- a/pkg/internal/services/reactions.go +++ b/pkg/internal/services/reactions.go @@ -1,6 +1,8 @@ package services import ( + "fmt" + "git.solsynth.dev/hydrogen/interactive/pkg/internal/models" "github.com/samber/lo" "gorm.io/gorm" @@ -28,26 +30,26 @@ func ListResourceReactions(tx *gorm.DB) (map[string]int64, error) { }), 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 { - ArticleID uint - Symbol string - Count int64 + ID uint + Symbol string + Count int64 } reactInfo := map[uint]map[string]int64{} if err := tx.Model(&models.Reaction{}). - Select("article_id, symbol, COUNT(id) as count"). - Group("article_id, symbol"). + Select(fmt.Sprintf("%s as id, symbol, COUNT(id) as count"), indexField). + Group("id, symbol"). Scan(&reactions).Error; err != nil { return reactInfo, err } for _, info := range reactions { - if _, ok := reactInfo[info.ArticleID]; !ok { - reactInfo[info.ArticleID] = make(map[string]int64) + if _, ok := reactInfo[info.ID]; !ok { + reactInfo[info.ID] = make(map[string]int64) } - reactInfo[info.ArticleID][info.Symbol] = info.Count + reactInfo[info.ID][info.Symbol] = info.Count } return reactInfo, nil