Search accounts w/ username or nick

This commit is contained in:
LittleSheep 2024-07-30 18:20:45 +08:00
parent 7c09138ef7
commit 57b4b314fe
4 changed files with 28 additions and 6 deletions

5
.idea/workspace.xml generated
View File

@ -4,10 +4,7 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix permissions in groups"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix permissions in groups" />
<change beforePath="$PROJECT_DIR$/.idea/dataSources/74bcf3ef-a2b9-435b-b9e5-f32902a33b25.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/dataSources/74bcf3ef-a2b9-435b-b9e5-f32902a33b25.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/dataSources/74bcf3ef-a2b9-435b-b9e5-f32902a33b25/storage_v2/_src_/database/hy_passport.gNOKQQ/schema/public.abK9xQ.meta" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/dataSources/74bcf3ef-a2b9-435b-b9e5-f32902a33b25/storage_v2/_src_/database/hy_passport.gNOKQQ/schema/public.abK9xQ.meta" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />

View File

@ -18,7 +18,7 @@ import (
func lookupAccount(c *fiber.Ctx) error { func lookupAccount(c *fiber.Ctx) error {
probe := c.Query("probe") probe := c.Query("probe")
if len(probe) == 0 { if len(probe) == 0 {
return fiber.NewError(fiber.StatusBadRequest, "you must provide a probe") return fiber.NewError(fiber.StatusBadRequest, "lookup probe is required")
} }
user, err := services.LookupAccount(probe) user, err := services.LookupAccount(probe)
@ -29,6 +29,20 @@ func lookupAccount(c *fiber.Ctx) error {
return c.JSON(user) return c.JSON(user)
} }
func searchAccount(c *fiber.Ctx) error {
probe := c.Query("probe")
if len(probe) == 0 {
return fiber.NewError(fiber.StatusBadRequest, "search probe is required")
}
users, err := services.SearchAccount(probe)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(users)
}
func getUserinfo(c *fiber.Ctx) error { func getUserinfo(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil { if err := exts.EnsureAuthenticated(c); err != nil {
return err return err

View File

@ -18,6 +18,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
} }
api.Get("/users/lookup", lookupAccount) api.Get("/users/lookup", lookupAccount)
api.Get("/users/search", searchAccount)
me := api.Group("/users/me").Name("Myself Operations") me := api.Group("/users/me").Name("Myself Operations")
{ {

View File

@ -2,9 +2,10 @@ package services
import ( import (
"fmt" "fmt"
"gorm.io/gorm/clause"
"time" "time"
"gorm.io/gorm/clause"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/viper" "github.com/spf13/viper"
"gorm.io/datatypes" "gorm.io/datatypes"
@ -65,6 +66,15 @@ func LookupAccount(probe string) (models.Account, error) {
return account, fmt.Errorf("account was not found") return account, fmt.Errorf("account was not found")
} }
func SearchAccount(probe string) ([]models.Account, error) {
probe = "%" + probe + "%"
var accounts []models.Account
if err := database.C.Where("name LIKE ? OR nick LIKE ?", probe).Find(&accounts).Error; err != nil {
return accounts, err
}
return accounts, nil
}
func CreateAccount(name, nick, email, password string) (models.Account, error) { func CreateAccount(name, nick, email, password string) (models.Account, error) {
user := models.Account{ user := models.Account{
Name: name, Name: name,