From d2beaa67b44ea039461f8b20b3bbce32205540b5 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 30 Jul 2024 20:28:38 +0800 Subject: [PATCH] :sparkles: Able get multiple userinfo in one request --- pkg/internal/server/api/index.go | 1 + pkg/internal/server/api/userinfo_api.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index fe05c60..5f5eb14 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -63,6 +63,7 @@ func MapAPIs(app *fiber.App, baseURL string) { directory.Get("/status", getStatus) } + api.Get("/users", getOtherUserinfoBatch) api.Post("/users", doRegister) auth := api.Group("/auth").Name("Auth") diff --git a/pkg/internal/server/api/userinfo_api.go b/pkg/internal/server/api/userinfo_api.go index bf9471e..0b13c0e 100644 --- a/pkg/internal/server/api/userinfo_api.go +++ b/pkg/internal/server/api/userinfo_api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "strings" "git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/models" @@ -35,3 +36,19 @@ func getOtherUserinfo(c *fiber.Ctx) error { return c.JSON(account) } + +func getOtherUserinfoBatch(c *fiber.Ctx) error { + idSet := strings.Split(c.Query("id"), ",") + if len(idSet) == 0 { + return fiber.NewError(fiber.StatusBadRequest, "id list is required") + } + + var accounts []models.Account + if err := database.C. + Where("id IN ?", idSet). + Find(&accounts).Error; err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } + + return c.JSON(accounts) +}