Multiple implementations of the same back-end application. The aim is to provide quick, side-by-side comparisons of different technologies (languages, frameworks, libraries) while preserving consistent business logic across all implementations.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

69 lines
1.5 KiB

package entities
import (
"autostore/internal/domain/value_objects"
"golang.org/x/crypto/bcrypt"
)
type UserEntity struct {
id value_objects.UserID
username string
passwordHash string
}
func NewUser(id value_objects.UserID, username string, password string) (*UserEntity, error) {
if username == "" {
return nil, ErrInvalidUsername
}
if password == "" {
return nil, ErrInvalidPassword
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return nil, ErrFailedToHashPassword
}
return &UserEntity{
id: id,
username: username,
passwordHash: string(hashedPassword),
}, nil
}
// NewUserWithHashedPassword creates a user entity with a pre-hashed password
// This is used when reconstructing a user from the database
func NewUserWithHashedPassword(id value_objects.UserID, username string, passwordHash string) (*UserEntity, error) {
if username == "" {
return nil, ErrInvalidUsername
}
if passwordHash == "" {
return nil, ErrInvalidPassword
}
return &UserEntity{
id: id,
username: username,
passwordHash: passwordHash,
}, nil
}
func (u *UserEntity) GetID() value_objects.UserID {
return u.id
}
func (u *UserEntity) GetUsername() string {
return u.username
}
func (u *UserEntity) GetPasswordHash() string {
return u.passwordHash
}
func (u *UserEntity) ValidatePassword(password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(u.passwordHash), []byte(password))
return err == nil
}