usermngmt/internal/helper.go

58 lines
1.3 KiB
Go
Raw Permalink Normal View History

2021-11-10 01:44:22 +00:00
package internal
2021-11-08 10:04:01 +00:00
2021-11-09 04:48:24 +00:00
import (
2021-11-09 05:26:51 +00:00
"fmt"
2022-10-06 07:09:22 +00:00
"github.com/thoas/go-funk"
"go.mongodb.org/mongo-driver/bson/primitive"
2021-11-10 07:50:43 +00:00
"strings"
2021-11-09 05:26:51 +00:00
2022-10-10 03:46:33 +00:00
"git.selly.red/Selly-Modules/mongodb"
2021-11-09 04:48:24 +00:00
"golang.org/x/crypto/bcrypt"
)
2021-11-08 10:04:01 +00:00
2021-11-10 04:52:07 +00:00
// HashPassword ...
2021-11-10 01:44:22 +00:00
func HashPassword(password string) string {
2021-11-08 10:04:01 +00:00
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), passwordHashingCost)
return string(bytes)
}
2021-11-10 04:52:07 +00:00
// CheckPasswordHash ...
2021-11-10 01:44:22 +00:00
func CheckPasswordHash(password, hash string) bool {
2021-11-08 10:04:01 +00:00
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
2021-11-09 05:26:51 +00:00
2021-11-10 04:52:07 +00:00
// GetSearchString ...
2021-11-10 01:44:22 +00:00
func GetSearchString(fieldList ...string) string {
2021-11-09 05:26:51 +00:00
var (
searchList = make([]interface{}, 0)
format = ""
)
for i, value := range fieldList {
searchList = append(searchList, mongodb.NonAccentVietnamese(value))
if i == 0 {
format += "%s"
continue
}
format += " %s"
}
return fmt.Sprintf(format, searchList...)
}
2021-11-10 07:50:43 +00:00
2021-11-10 08:00:36 +00:00
// GenerateCode ...
func GenerateCode(s string) string {
2021-11-10 07:50:43 +00:00
var (
underscore = "_"
2021-11-10 08:00:36 +00:00
emptySpace = " "
2021-11-10 07:50:43 +00:00
)
2021-11-10 08:00:36 +00:00
return strings.ReplaceAll(mongodb.NonAccentVietnamese(s), emptySpace, underscore)
2021-11-10 07:50:43 +00:00
}
2022-10-06 07:09:22 +00:00
// ConvertObjectIDsToStrings ...
func ConvertObjectIDsToStrings(ids []primitive.ObjectID) []string {
return funk.Map(ids, func(item primitive.ObjectID) string {
return item.Hex()
}).([]string)
}