All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
104 lines
2.5 KiB
Go
104 lines
2.5 KiB
Go
package helpers
|
|
|
|
import (
|
|
"cargo-erp-backend/internal/config"
|
|
"cargo-erp-backend/internal/database"
|
|
"cargo-erp-backend/internal/domain"
|
|
"encoding/json"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
"go.uber.org/zap"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type HelperInterface interface {
|
|
GetDB(key string) *gorm.DB
|
|
GetRedis(key string) *redis.Client
|
|
Config() config.ConfigInterface
|
|
Log() *zap.Logger
|
|
HashPassword(password string) string
|
|
VerifyPassword(hashedPassword, password string) bool
|
|
StringFromPtr(ptr *string) string
|
|
StructTobyte(data interface{}) ([]byte, error)
|
|
CloneStructWithoutField(input interface{}, field string) (map[string]interface{}, error)
|
|
GetAuthInfo(authinfo string) (*domain.User, error)
|
|
}
|
|
type Helper struct {
|
|
Conf config.ConfigInterface
|
|
DB database.DatabaseInterface
|
|
Logger *zap.Logger
|
|
RedisDB database.RedisInterface
|
|
}
|
|
|
|
func NewHelper(config config.ConfigInterface, db database.DatabaseInterface, logger *zap.Logger, dbredis database.RedisInterface) HelperInterface {
|
|
return &Helper{
|
|
Conf: config,
|
|
DB: db,
|
|
Logger: logger,
|
|
RedisDB: dbredis,
|
|
}
|
|
}
|
|
func (h *Helper) GetDB(key string) *gorm.DB {
|
|
return h.DB.Get(key)
|
|
}
|
|
func (h *Helper) GetRedis(key string) *redis.Client {
|
|
return h.RedisDB.Get(key)
|
|
}
|
|
func (h *Helper) Config() config.ConfigInterface {
|
|
return h.Conf
|
|
}
|
|
func (h *Helper) Log() *zap.Logger {
|
|
return h.Logger
|
|
}
|
|
func (h *Helper) HashPassword(password string) string {
|
|
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
return string(bytes)
|
|
}
|
|
func (h *Helper) VerifyPassword(hashedPassword, password string) bool {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
|
return err == nil
|
|
}
|
|
func (h *Helper) StringFromPtr(ptr *string) string {
|
|
if ptr == nil {
|
|
return ""
|
|
}
|
|
return *ptr
|
|
}
|
|
func (h *Helper) StructTobyte(data interface{}) ([]byte, error) {
|
|
var output []byte
|
|
s, err := json.Marshal(data)
|
|
if err != nil {
|
|
return output, err
|
|
}
|
|
output = s
|
|
return output, nil
|
|
}
|
|
func (h *Helper) CloneStructWithoutField(input interface{}, field string) (map[string]interface{}, error) {
|
|
|
|
data, err := json.Marshal(input)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var result map[string]interface{}
|
|
|
|
if err := json.Unmarshal(data, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(field) > 0 {
|
|
delete(result, field)
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (h *Helper) GetAuthInfo(authinfo string) (*domain.User, error) {
|
|
|
|
var auth domain.User
|
|
if err := json.Unmarshal([]byte(authinfo), &auth); err != nil {
|
|
return nil, err
|
|
}
|
|
return &auth, nil
|
|
}
|