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

50 lines
834 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
)
2020-12-17 21:32:13 +01:00
func (filter *Filter) FormatSQL(tx *gorm.DB) *gorm.DB {
2020-12-17 22:29:24 +01:00
if filter == nil {
return tx
}
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
if filter.OrderDirection != nil && filter.OrderDirection.IsValid() {
2020-12-17 21:32:13 +01:00
if *filter.OrderDirection == OrderDirectionAsc {
desc = false
}
}
2020-12-17 21:32:13 +01:00
tx.Order(clause.OrderByColumn{
Column: clause.Column{
Name: *filter.OrderBy,
},
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[:])
}