Add full search for user table

Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
2025-12-26 03:46:54 +08:00
parent 6681ffccdf
commit 98e32b67e1
4 changed files with 63 additions and 2 deletions

View File

@@ -133,7 +133,16 @@ func (self *User) UpdateByUserID(userId uuid.UUID) error {
})
}
func (self *User) FastListUsers(limit, offset int64) ([]UserSearchDoc, error) {
func (self *User) GetFullTable() (*[]User, error) {
var users []User
err := Database.Find(&users).Error
if err != nil {
return nil, err
}
return &users, nil
}
func (self *User) FastListUsers(limit, offset int64) (*[]UserSearchDoc, error) {
index := MeiliSearch.Index("user")
result, err := index.Search("", &meilisearch.SearchRequest{
Limit: limit,
@@ -146,5 +155,5 @@ func (self *User) FastListUsers(limit, offset int64) ([]UserSearchDoc, error) {
if err := mapstructure.Decode(result.Hits, &list); err != nil {
return nil, err
}
return list, nil
return &list, nil
}

1
service/user/full.go Normal file
View File

@@ -0,0 +1 @@
package user

View File

@@ -11,4 +11,5 @@ func Handler(r *gin.RouterGroup) {
r.GET("/info", Info)
r.POST("/checkin", Checkin)
r.PATCH("/update", Update)
r.GET("/list", List)
}

50
service/user/list.go Normal file
View File

@@ -0,0 +1,50 @@
package user
import (
"nixcn-cms/data"
"strconv"
"github.com/gin-gonic/gin"
)
func List(c *gin.Context) {
data := new(data.User)
// Get limit and offset from query
limit, ok := c.GetQuery("limit")
if !ok {
limit = "0"
}
offset, ok := c.GetQuery("offset")
if !ok {
c.JSON(400, gin.H{
"status": "offset not found",
})
return
}
// Parse string to int64
limitNum, err := strconv.ParseInt(limit, 10, 64)
if err != nil {
c.JSON(400, gin.H{
"status": "parse string to int error",
})
return
}
offsetNum, err := strconv.ParseInt(offset, 10, 64)
if err != nil {
c.JSON(400, gin.H{
"status": "parse string to int error",
})
return
}
// Get user list from search engine
list, err := data.FastListUsers(limitNum, offsetNum)
if err != nil {
c.JSON(500, gin.H{
"status": "failed list users from meilisearch",
})
}
c.JSON(200, list)
}