Interactive/pkg/internal/services/reactions.go
2024-07-05 21:06:54 +08:00

57 lines
1.2 KiB
Go

package services
import (
"fmt"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
"github.com/samber/lo"
"gorm.io/gorm"
)
func ListResourceReactions(tx *gorm.DB) (map[string]int64, error) {
var reactions []struct {
Symbol string
Count int64
}
if err := tx.Model(&models.Reaction{}).
Select("symbol, COUNT(id) as count").
Group("symbol").
Scan(&reactions).Error; err != nil {
return map[string]int64{}, err
}
return lo.SliceToMap(reactions, func(item struct {
Symbol string
Count int64
},
) (string, int64) {
return item.Symbol, item.Count
}), nil
}
func BatchListResourceReactions(tx *gorm.DB, indexField string) (map[uint]map[string]int64, error) {
var reactions []struct {
ID uint
Symbol string
Count int64
}
reactInfo := map[uint]map[string]int64{}
if err := tx.Model(&models.Reaction{}).
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.ID]; !ok {
reactInfo[info.ID] = make(map[string]int64)
}
reactInfo[info.ID][info.Symbol] = info.Count
}
return reactInfo, nil
}