refactor: order filter
This commit is contained in:
parent
c69b63abda
commit
c2e8ec3ba6
|
|
@ -1,6 +1,8 @@
|
|||
package dao
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"yintai-api/base/global"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
|
@ -13,3 +15,16 @@ func getDB(db ...*gorm.DB) *gorm.DB {
|
|||
|
||||
return global.GetDB()
|
||||
}
|
||||
|
||||
func ezSort(a string) string {
|
||||
var sortCol = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
|
||||
s := strings.Fields(a)
|
||||
if len(s) != 2 || !sortCol.MatchString(s[0]) {
|
||||
return ""
|
||||
}
|
||||
dir := strings.ToUpper(s[1])
|
||||
if dir != "ASC" && dir != "DESC" {
|
||||
return ""
|
||||
}
|
||||
return "`" + s[0] + "` " + dir
|
||||
}
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ func (r *Category) List(pid int64, name, nameEn, types string, page, size int, s
|
|||
tx = tx.Where("name_en LIKE ?", fmt.Sprintf("%%%s%%", nameEn))
|
||||
}
|
||||
|
||||
if sort != "" {
|
||||
if sort = ezSort(sort); sort != "" {
|
||||
tx = tx.Order(sort)
|
||||
} else {
|
||||
tx = tx.Order("create_time desc")
|
||||
|
|
|
|||
|
|
@ -87,10 +87,10 @@ func (r *Document) List(lang string, categoryID int64, fileName, title, subTitle
|
|||
return nil, 0, err
|
||||
}
|
||||
|
||||
if sort == "" {
|
||||
tx = tx.Order("p.weight DESC")
|
||||
} else {
|
||||
if sort = ezSort(sort); sort != "" {
|
||||
tx = tx.Order(sort)
|
||||
} else {
|
||||
tx = tx.Order("p.weight DESC")
|
||||
}
|
||||
|
||||
if err := tx.Offset((page - 1) * size).Limit(size).Select("p.*,t.*,c.name as category_name,c.name_en as category_en,c.id as category_id").Find(&list).Error; err != nil {
|
||||
|
|
|
|||
|
|
@ -93,9 +93,7 @@ func (r *Job) List(lang, city, title, desc, reqs, jobType, jobArea, jobUnit stri
|
|||
return nil, 0, err
|
||||
}
|
||||
|
||||
if sort == "" {
|
||||
tx = tx.Order(sort)
|
||||
} else {
|
||||
if sort = ezSort(sort); sort != "" {
|
||||
tx = tx.Order(sort)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ func (r *News) List(lang, title, content, publishTime string, categoryID, pinned
|
|||
return nil, 0, err
|
||||
}
|
||||
|
||||
if sort != "" {
|
||||
if sort = ezSort(sort); sort != "" {
|
||||
tx = tx.Order(sort)
|
||||
} else {
|
||||
tx = tx.Order("pinned desc,weight desc,create_time desc")
|
||||
|
|
@ -127,7 +127,7 @@ func (r *News) GetN(categoryID int64, lang string, page, size int, sort string)
|
|||
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s c ON c.id = l.category_id", NewCategory().TableName()))
|
||||
tx = tx.Where("c.id = ?", cast.ToInt64(categoryID))
|
||||
tx = tx.Where("t.lang = ?", lang)
|
||||
if sort != "" {
|
||||
if sort = ezSort(sort); sort != "" {
|
||||
tx = tx.Order(sort)
|
||||
} else {
|
||||
tx = tx.Order("pinned desc,weight desc,create_time desc")
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ func (r *Page) List(name string, page, size int, sort string) ([]*model.Page, in
|
|||
return nil, 0, err
|
||||
}
|
||||
|
||||
if sort != "" {
|
||||
if sort = ezSort(sort); sort != "" {
|
||||
tx = tx.Order(sort)
|
||||
} else {
|
||||
tx = tx.Order("id,pid,weight desc")
|
||||
|
|
|
|||
|
|
@ -82,9 +82,7 @@ func (r *Process) List(lang, title, content, processTime string, categoryID int6
|
|||
return nil, 0, err
|
||||
}
|
||||
|
||||
if sort == "" {
|
||||
tx = tx.Order(sort)
|
||||
} else {
|
||||
if sort = ezSort(sort); sort != "" {
|
||||
tx = tx.Order(sort)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ func (r *Resource) List(name, types, code, method, path string, page, size int,
|
|||
return nil, 0, err
|
||||
}
|
||||
|
||||
if sort != "" {
|
||||
if sort = ezSort(sort); sort != "" {
|
||||
tx = tx.Order(sort)
|
||||
} else {
|
||||
tx = tx.Order("id desc")
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ func (r *Role) List(name, desc string, page, size int, sort string) ([]*model.Ro
|
|||
return nil, 0, err
|
||||
}
|
||||
|
||||
if sort != "" {
|
||||
if sort = ezSort(sort); sort != "" {
|
||||
tx = tx.Order(sort)
|
||||
} else {
|
||||
tx = tx.Order("id desc")
|
||||
|
|
|
|||
|
|
@ -128,7 +128,7 @@ func (r *User) List(username, account string, role int64, page, size int, sort s
|
|||
}
|
||||
|
||||
tx = tx.Order("id DESC")
|
||||
if sort != "" {
|
||||
if sort = ezSort(sort); sort != "" {
|
||||
tx = tx.Order(sort)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue