You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.3 KiB

package base
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"strings"
"time"
)
type GetReq struct {
ID uint `form:"id"`
Keyword string `form:"keyword"`
PageIndex int `form:"pageIndex"`
PageSize int `form:"pageSize"`
SortField string `form:"sortField"`
SortOrder string `form:"sortOrder"`
}
const PageSizeMax = 100
const PageSizeDefault = 12
const PageSizeAll = -1
const PageIndexDefault = 1
const SortFieldDefault = "updated_at"
const SortOrderDefault = "DESC"
func (getReq *GetReq) Init(ctx *gin.Context) error {
if err := ctx.ShouldBindQuery(&getReq); err != nil {
return err
}
if getReq.PageIndex <= 0 {
getReq.PageIndex = PageIndexDefault
}
if getReq.PageSize < PageSizeAll || getReq.PageSize > PageSizeMax || getReq.PageSize == 0 {
getReq.PageSize = PageSizeDefault
}
if getReq.SortField == "" {
getReq.SortField = SortFieldDefault
}
getReq.SortOrder = strings.ToUpper(getReq.SortOrder)
if getReq.SortOrder != "ASC" && getReq.SortOrder != "DESC" {
getReq.SortOrder = SortOrderDefault
}
return nil
}
type Model struct {
ID uint `gorm:"primarykey" json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}