提交 b961746b authored 作者: mooncake's avatar mooncake

pagination

上级 252fddf8
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"errors" "errors"
"net/http" "net/http"
"os" "os"
"reflect"
"strings" "strings"
"testing" "testing"
"time" "time"
...@@ -14,8 +15,10 @@ import ( ...@@ -14,8 +15,10 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/response" "gitlab.wanzhuangkj.com/tush/xpkg/gin/response"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/httpcli" "gitlab.wanzhuangkj.com/tush/xpkg/pkg/httpcli"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/sgorm/query"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/xerrors/xerror" "gitlab.wanzhuangkj.com/tush/xpkg/pkg/xerrors/xerror"
xvalid "gitlab.wanzhuangkj.com/tush/xpkg/xcommon/validator" xvalid "gitlab.wanzhuangkj.com/tush/xpkg/xcommon/validator"
"gitlab.wanzhuangkj.com/tush/xpkg/xutils/ctxutils"
maputils "gitlab.wanzhuangkj.com/tush/xpkg/xutils/maputils" maputils "gitlab.wanzhuangkj.com/tush/xpkg/xutils/maputils"
) )
...@@ -52,20 +55,49 @@ func (e *Controller) Bind(c *gin.Context, req interface{}) error { ...@@ -52,20 +55,49 @@ func (e *Controller) Bind(c *gin.Context, req interface{}) error {
} }
} }
// if err := xvalid.Validator.Struct(req); err != nil {
// return zhError(err)
// }
if valid, ok := req.(IValid); ok { if valid, ok := req.(IValid); ok {
if err := valid.Valid(); err != nil { if err := valid.Valid(); err != nil {
return err return err
} }
} }
storePagination(req, reflectPagination(c))
c.Set("reqbody", req) c.Set("reqbody", req)
return nil return nil
} }
func storePagination(data any, f func(v reflect.Value, i int)) {
v := reflect.ValueOf(data)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return
}
pt := reflect.TypeOf(query.Pagination{})
t := v.Type()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.Type == pt {
f(v, i)
return
}
}
}
func reflectPagination(c *gin.Context) func(v reflect.Value, i int) {
return func(v reflect.Value, i int) {
pv := v.Field(i)
if pv.CanInterface() {
if p, ok := pv.Interface().(query.Pagination); ok {
c.Set(ctxutils.KeyApiType, "page")
c.Set(ctxutils.KeyPagination, p)
}
}
}
}
func zhError(err error) error { func zhError(err error) error {
if errs, ok := err.(validator.ValidationErrors); ok { if errs, ok := err.(validator.ValidationErrors); ok {
strs := make([]string, 0, len(errs)) strs := make([]string, 0, len(errs))
......
...@@ -301,14 +301,6 @@ type IListParams interface { ...@@ -301,14 +301,6 @@ type IListParams interface {
} }
func (x *ODao[T]) Page(ctx context.Context, where IPageParams) (rs []*T, total int64, err error) { func (x *ODao[T]) Page(ctx context.Context, where IPageParams) (rs []*T, total int64, err error) {
if ginCtx, err := ctxutils.GetGinCtx(ctx); err == nil && ginCtx != nil {
ginCtx.Set(ctxutils.KeyApiType, "page")
pagination := query.Pagination{
PageIndex: where.GetPageIndex(),
PageSize: where.GetPageSize(),
}
ginCtx.Set(ctxutils.KeyPagination, pagination)
}
condition := x.DB().WithContext(ctx).Model(new(T)).Scopes(x.MakeCondition(where)) condition := x.DB().WithContext(ctx).Model(new(T)).Scopes(x.MakeCondition(where))
if where.Unscoped() { if where.Unscoped() {
condition = condition.Unscoped() condition = condition.Unscoped()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论