2020-02-09 15:26:59 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2021-01-17 12:45:23 +01:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
|
2020-12-17 21:32:13 +01:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/clause"
|
2020-02-09 15:26:59 +01:00
|
|
|
)
|
|
|
|
|
2021-02-13 15:08:05 +01:00
|
|
|
func FormatSQL(tx *gorm.DB, order *Ordering, paginate *Pagination) *gorm.DB {
|
2020-11-28 21:29:31 +01:00
|
|
|
|
2021-02-13 15:08:05 +01:00
|
|
|
if paginate != nil {
|
|
|
|
if paginate.Limit != nil {
|
|
|
|
tx.Limit(*paginate.Limit)
|
|
|
|
}
|
2020-02-09 15:26:59 +01:00
|
|
|
|
2021-02-13 15:08:05 +01:00
|
|
|
if paginate.Offset != nil {
|
|
|
|
tx.Offset(*paginate.Offset)
|
|
|
|
}
|
2020-12-17 21:32:13 +01:00
|
|
|
}
|
2020-02-09 15:26:59 +01:00
|
|
|
|
2021-02-13 15:08:05 +01:00
|
|
|
if order != nil && order.OrderBy != nil {
|
2020-12-17 21:32:13 +01:00
|
|
|
desc := true
|
2021-02-13 15:08:05 +01:00
|
|
|
if order.OrderDirection != nil && order.OrderDirection.IsValid() {
|
|
|
|
if *order.OrderDirection == OrderDirectionAsc {
|
2020-12-17 21:32:13 +01:00
|
|
|
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{
|
2021-02-13 15:08:05 +01:00
|
|
|
Name: *order.OrderBy,
|
2020-12-17 21:32:13 +01:00
|
|
|
},
|
|
|
|
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
|
|
|
}
|
2021-01-17 12:45:23 +01:00
|
|
|
|
|
|
|
// MD5Hash hashes value to a 32 length digest, the result is the same as the MYSQL function md5()
|
|
|
|
func MD5Hash(value string) string {
|
|
|
|
hash := md5.Sum([]byte(value))
|
|
|
|
return hex.EncodeToString(hash[:])
|
|
|
|
}
|