2020-02-09 15:26:59 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2020-12-17 21:32:13 +01:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/clause"
|
2020-02-09 15:26:59 +01:00
|
|
|
)
|
|
|
|
|
2020-12-17 21:32:13 +01:00
|
|
|
func (filter *Filter) FormatSQL(tx *gorm.DB) *gorm.DB {
|
2020-11-28 21:29:31 +01:00
|
|
|
|
2020-12-17 21:32:13 +01:00
|
|
|
if filter.Limit != nil {
|
|
|
|
tx.Limit(*filter.Limit)
|
2020-02-09 15:26:59 +01:00
|
|
|
}
|
|
|
|
|
2020-12-17 21:32:13 +01:00
|
|
|
if filter.Offset != nil {
|
|
|
|
tx.Offset(*filter.Offset)
|
|
|
|
}
|
2020-02-09 15:26:59 +01:00
|
|
|
|
|
|
|
if filter.OrderBy != nil {
|
|
|
|
|
2020-12-17 21:32:13 +01:00
|
|
|
desc := true
|
2020-10-13 17:27:28 +02:00
|
|
|
if filter.OrderDirection != nil && filter.OrderDirection.IsValid() {
|
2020-12-17 21:32:13 +01:00
|
|
|
if *filter.OrderDirection == OrderDirectionAsc {
|
|
|
|
desc = false
|
|
|
|
}
|
2020-02-24 09:59:20 +01:00
|
|
|
}
|
|
|
|
|
2020-12-17 21:32:13 +01:00
|
|
|
tx.Order(clause.OrderByColumn{
|
|
|
|
Column: clause.Column{
|
|
|
|
Name: *filter.OrderBy,
|
|
|
|
},
|
|
|
|
Desc: desc,
|
|
|
|
})
|
2020-02-24 09:59:20 +01:00
|
|
|
}
|
|
|
|
|
2020-12-17 21:32:13 +01:00
|
|
|
return tx
|
2020-02-09 15:26:59 +01:00
|
|
|
}
|