From 98e32b67e1f3d9c8cb10184a9b8e4eeb516ff667 Mon Sep 17 00:00:00 2001 From: Asai Neko Date: Fri, 26 Dec 2025 03:46:54 +0800 Subject: [PATCH] Add full search for user table Signed-off-by: Asai Neko --- data/user.go | 13 +++++++++-- service/user/full.go | 1 + service/user/handler.go | 1 + service/user/list.go | 50 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 service/user/full.go create mode 100644 service/user/list.go diff --git a/data/user.go b/data/user.go index 8630d90..7bbfd55 100644 --- a/data/user.go +++ b/data/user.go @@ -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 } diff --git a/service/user/full.go b/service/user/full.go new file mode 100644 index 0000000..a00006b --- /dev/null +++ b/service/user/full.go @@ -0,0 +1 @@ +package user diff --git a/service/user/handler.go b/service/user/handler.go index 5da7805..d439ca0 100644 --- a/service/user/handler.go +++ b/service/user/handler.go @@ -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) } diff --git a/service/user/list.go b/service/user/list.go new file mode 100644 index 0000000..2f3f206 --- /dev/null +++ b/service/user/list.go @@ -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) +}