1
Fork 0
photoview/api/graphql/models/utils.go

47 lines
870 B
Go
Raw Normal View History

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
)
func FormatSQL(tx *gorm.DB, order *Ordering, paginate *Pagination) *gorm.DB {
if paginate != nil {
if paginate.Limit != nil {
tx.Limit(*paginate.Limit)
}
2020-02-09 15:26:59 +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
if order != nil && order.OrderBy != nil {
desc := false
if order.OrderDirection != nil && order.OrderDirection.IsValid() {
if *order.OrderDirection == OrderDirectionDesc {
desc = true
2020-12-17 21:32:13 +01:00
}
}
2020-12-17 21:32:13 +01:00
tx.Order(clause.OrderByColumn{
Column: clause.Column{
Name: *order.OrderBy,
2020-12-17 21:32:13 +01:00
},
Desc: desc,
})
}
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[:])
}