Introduce batch get user api

This commit is contained in:
LittleSheep 2025-02-17 22:30:07 +08:00
parent 09dddf7721
commit 11ca7c0ba6
2 changed files with 30 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"time"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"gorm.io/gorm"
"git.solsynth.dev/hypernet/passport/pkg/internal/http/exts"
@ -15,9 +16,37 @@ import (
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
jsoniter "github.com/json-iterator/go"
"github.com/samber/lo"
"github.com/spf13/viper"
)
func getUserInBatch(c *fiber.Ctx) error {
id := c.Query("id")
list := strings.Split(id, ",")
numericList := lo.Filter(lo.Map(list, func(str string, i int) int {
value, err := strconv.Atoi(str)
if err != nil {
return 0
}
return value
}), func(vak int, idx int) bool {
return vak > 0
})
var accounts []models.Account
if err := database.C.
Where("id IN ?", numericList).
Preload("Profile").
Preload("Badges", func(db *gorm.DB) *gorm.DB {
return db.Order("badges.type DESC")
}).
First(&accounts).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(accounts)
}
func lookupAccount(c *fiber.Ctx) error {
probe := c.Query("probe")
if len(probe) == 0 {

View File

@ -48,6 +48,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
}
}
api.Get("/users", getUserInBatch)
api.Get("/users/lookup", lookupAccount)
api.Get("/users/search", searchAccount)