created: utils package
This commit is contained in:
parent
f4c4065812
commit
5ffb6dde46
4 changed files with 52 additions and 0 deletions
8
internal/interfaces/utils.go
Normal file
8
internal/interfaces/utils.go
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package interfaces
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
type Utils interface {
|
||||||
|
IsEmail(email string) bool
|
||||||
|
GetUserUuidFromContext(c *gin.Context) (string, error)
|
||||||
|
}
|
||||||
7
pkg/utils/handler.go
Normal file
7
pkg/utils/handler.go
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
type Utils struct{}
|
||||||
|
|
||||||
|
func NewUtils() *Utils {
|
||||||
|
return &Utils{}
|
||||||
|
}
|
||||||
8
pkg/utils/isEmail.go
Normal file
8
pkg/utils/isEmail.go
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import "regexp"
|
||||||
|
|
||||||
|
func (u *Utils) IsEmail(email string) bool {
|
||||||
|
re := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)
|
||||||
|
return re.MatchString(email)
|
||||||
|
}
|
||||||
29
pkg/utils/userUuid.go
Normal file
29
pkg/utils/userUuid.go
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (u *Utils) GetUserUuidFromContext(c *gin.Context) (string, error) {
|
||||||
|
if c == nil {
|
||||||
|
return "", errors.New("context is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
uuidRaw, exists := c.Get("userUuid")
|
||||||
|
if !exists {
|
||||||
|
return "", errors.New("user uuid not found in context")
|
||||||
|
}
|
||||||
|
|
||||||
|
userUuidStr, ok := uuidRaw.(string)
|
||||||
|
if !ok {
|
||||||
|
return "", errors.New("user uuid is not a string")
|
||||||
|
}
|
||||||
|
|
||||||
|
userUuid, err := uuid.Parse(userUuidStr)
|
||||||
|
if err != nil {
|
||||||
|
return "", errors.New("error parsing user uuid")
|
||||||
|
}
|
||||||
|
return userUuid.String(), nil
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue