forked from nixcn/nixcn-cms
Add full search for user table
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
13
data/user.go
13
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")
|
index := MeiliSearch.Index("user")
|
||||||
result, err := index.Search("", &meilisearch.SearchRequest{
|
result, err := index.Search("", &meilisearch.SearchRequest{
|
||||||
Limit: limit,
|
Limit: limit,
|
||||||
@@ -146,5 +155,5 @@ func (self *User) FastListUsers(limit, offset int64) ([]UserSearchDoc, error) {
|
|||||||
if err := mapstructure.Decode(result.Hits, &list); err != nil {
|
if err := mapstructure.Decode(result.Hits, &list); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return list, nil
|
return &list, nil
|
||||||
}
|
}
|
||||||
|
|||||||
1
service/user/full.go
Normal file
1
service/user/full.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package user
|
||||||
@@ -11,4 +11,5 @@ func Handler(r *gin.RouterGroup) {
|
|||||||
r.GET("/info", Info)
|
r.GET("/info", Info)
|
||||||
r.POST("/checkin", Checkin)
|
r.POST("/checkin", Checkin)
|
||||||
r.PATCH("/update", Update)
|
r.PATCH("/update", Update)
|
||||||
|
r.GET("/list", List)
|
||||||
}
|
}
|
||||||
|
|||||||
50
service/user/list.go
Normal file
50
service/user/list.go
Normal 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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user