parent
a6cc6ba84d
commit
80612a99ab
|
@ -25,6 +25,15 @@ func (s HandlerService) GetList(c *gin.Context) (data interface{}, err error) {
|
||||||
repo := service.NewEdgeService(s.Logger)
|
repo := service.NewEdgeService(s.Logger)
|
||||||
var req proto.ListRequest
|
var req proto.ListRequest
|
||||||
err = c.ShouldBindJSON(&req)
|
err = c.ShouldBindJSON(&req)
|
||||||
|
if req.Size < 1 {
|
||||||
|
req.Size = 20
|
||||||
|
}
|
||||||
|
if req.Size > 1000 {
|
||||||
|
req.Size = 1000
|
||||||
|
}
|
||||||
|
if req.Page < 1 {
|
||||||
|
req.Page = 1
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, e.NewValidErr(err)
|
return nil, e.NewValidErr(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,12 @@ package proto
|
||||||
type ListRequest struct {
|
type ListRequest struct {
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
IsIncludeSubdirectories bool `json:"isIncludeSubdirectories"`
|
IsIncludeSubdirectories bool `json:"isIncludeSubdirectories"`
|
||||||
|
BasePageList
|
||||||
|
}
|
||||||
|
|
||||||
|
type BasePageList struct {
|
||||||
|
Page int64 `json:"page,omitempty" form:"page"`
|
||||||
|
Size int64 `json:"pageSize,omitempty" form:"pageSize"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LabelRequest struct {
|
type LabelRequest struct {
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PagingStruct struct {
|
type PagingStruct struct {
|
||||||
|
@ -68,7 +69,10 @@ func (rp *repo) GetList(ctx context.Context, req proto.ListRequest) (rsp *proto.
|
||||||
if req.IsIncludeSubdirectories {
|
if req.IsIncludeSubdirectories {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
var files []os.DirEntry
|
var (
|
||||||
|
files []os.DirEntry
|
||||||
|
count int
|
||||||
|
)
|
||||||
files, err = os.ReadDir(req.Path)
|
files, err = os.ReadDir(req.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
goto ReturnPoint
|
goto ReturnPoint
|
||||||
|
@ -88,10 +92,23 @@ func (rp *repo) GetList(ctx context.Context, req proto.ListRequest) (rsp *proto.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 从小到大排序(稳定排序)
|
||||||
|
sort.SliceStable(list, func(i, j int) bool {
|
||||||
|
if list[i].Name < list[j].Name {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
count = len(list)
|
||||||
|
if len(list) >= int(req.Page*req.Size) {
|
||||||
|
list = list[int((req.Page-1)*req.Size):int(req.Page*req.Size)]
|
||||||
|
} else {
|
||||||
|
list = list[int((req.Page-1)*req.Size):]
|
||||||
|
}
|
||||||
rsp.Code = http.StatusOK
|
rsp.Code = http.StatusOK
|
||||||
rsp.Status = http.StatusText(http.StatusOK)
|
rsp.Status = http.StatusText(http.StatusOK)
|
||||||
rsp.Message = "成功"
|
rsp.Message = "成功"
|
||||||
rsp.Data = list
|
rsp = FillPaging(int64(count), req.Page, req.Size, list, rsp)
|
||||||
rsp.Err = err
|
rsp.Err = err
|
||||||
return rsp, err
|
return rsp, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue