Search accounts w/ username or nick

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

View File

@ -18,7 +18,7 @@ import (
func lookupAccount(c *fiber.Ctx) error {
probe := c.Query("probe")
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)
@ -29,6 +29,20 @@ func lookupAccount(c *fiber.Ctx) error {
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 {
if err := exts.EnsureAuthenticated(c); err != nil {
return err

View File

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

View File

@ -2,9 +2,10 @@ package services
import (
"fmt"
"gorm.io/gorm/clause"
"time"
"gorm.io/gorm/clause"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"gorm.io/datatypes"
@ -65,6 +66,15 @@ func LookupAccount(probe string) (models.Account, error) {
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) {
user := models.Account{
Name: name,