fix: Fix order by sorting rule not taking effect issue (#11871)

This commit is contained in:
ssongliu
2026-02-11 06:39:07 +00:00
committed by GitHub
parent 140d20a21d
commit 3e1f2b4934
5 changed files with 13 additions and 17 deletions
+6 -10
View File
@@ -62,16 +62,12 @@ func WithByNode(node string) global.DBOption {
}
}
func WithOrderBy(orderStr string) global.DBOption {
if orderStr == "createdAt" {
orderStr = "created_at"
}
if !re.GetRegex(re.OrderByValidationPattern).MatchString(orderStr) {
orderStr = "created_at"
}
return func(g *gorm.DB) *gorm.DB {
return g.Order(orderStr)
}
func WithOrderDesc(orderBy string) global.DBOption {
return WithOrderRuleBy(orderBy, constant.OrderDesc)
}
func WithOrderAsc(orderBy string) global.DBOption {
return WithOrderRuleBy(orderBy, constant.OrderAsc)
}
func WithOrderRuleBy(orderBy, order string) global.DBOption {
+2 -2
View File
@@ -33,7 +33,7 @@ func NewICommandService() ICommandService {
}
func (u *CommandService) List(req dto.OperateByType) ([]dto.CommandInfo, error) {
commands, err := commandRepo.List(repo.WithOrderBy("name"), repo.WithByType(req.Type))
commands, err := commandRepo.List(repo.WithOrderAsc("name"), repo.WithByType(req.Type))
if err != nil {
return nil, buserr.New("ErrRecordNotFound")
}
@@ -49,7 +49,7 @@ func (u *CommandService) List(req dto.OperateByType) ([]dto.CommandInfo, error)
}
func (u *CommandService) SearchForTree(req dto.OperateByType) ([]dto.CommandTree, error) {
cmdList, err := commandRepo.List(repo.WithOrderBy("name"), repo.WithByType(req.Type))
cmdList, err := commandRepo.List(repo.WithOrderAsc("name"), repo.WithByType(req.Type))
if err != nil {
return nil, err
}
+2 -2
View File
@@ -32,8 +32,8 @@ func NewIGroupService() IGroupService {
func (u *GroupService) List(req dto.OperateByType) ([]dto.GroupInfo, error) {
options := []global.DBOption{
repo.WithOrderBy("is_default desc"),
repo.WithOrderBy("created_at desc"),
repo.WithOrderDesc("is_default"),
repo.WithOrderDesc("created_at"),
}
if len(req.Type) != 0 {
options = append(options, repo.WithByType(req.Type))
+2 -2
View File
@@ -38,7 +38,7 @@ func (u *LogService) CreateLoginLog(operation model.LoginLog) error {
func (u *LogService) PageLoginLog(ctx *gin.Context, req dto.SearchLgLogWithPage) (int64, interface{}, error) {
options := []global.DBOption{
repo.WithOrderBy("created_at desc"),
repo.WithOrderDesc("created_at"),
}
if len(req.IP) != 0 {
options = append(options, logRepo.WithByIP(req.IP))
@@ -72,7 +72,7 @@ func (u *LogService) CreateOperationLog(operation *model.OperationLog) error {
func (u *LogService) PageOperationLog(req dto.SearchOpLogWithPage) (int64, interface{}, error) {
options := []global.DBOption{
repo.WithOrderBy("created_at desc"),
repo.WithOrderDesc("created_at"),
logRepo.WithByLikeOperation(req.Operation),
}
if len(req.Source) != 0 {
+1 -1
View File
@@ -43,7 +43,7 @@ func NewIScriptService() IScriptService {
}
func (u *ScriptService) Search(ctx *gin.Context, req dto.SearchPageWithGroup) (int64, interface{}, error) {
options := []global.DBOption{repo.WithOrderBy("created_at desc")}
options := []global.DBOption{repo.WithOrderDesc("created_at")}
if len(req.Info) != 0 {
options = append(options, scriptRepo.WithByInfo(req.Info))
}