125 lines
2.2 KiB
Go
125 lines
2.2 KiB
Go
package entity
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.heldm.com/held/qrcode/internal/qrcode"
|
|
"git.heldm.com/held/qrcode/internal/qrcode/service/auth"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type User struct {
|
|
id uuid.UUID
|
|
name string
|
|
email string
|
|
password string
|
|
createdAt time.Time
|
|
updatedAt time.Time
|
|
deletedAt *time.Time
|
|
}
|
|
|
|
func NewUser(name, email, password string) (*User, error) {
|
|
u := &User{
|
|
id: uuid.New(),
|
|
createdAt: time.Now(),
|
|
updatedAt: time.Now(),
|
|
}
|
|
|
|
if err := u.setName(name); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := u.setEmail(email); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := u.setPassword(password); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return u, nil
|
|
}
|
|
|
|
func (u *User) ID() uuid.UUID {
|
|
return u.id
|
|
}
|
|
|
|
func (u *User) setName(name string) error {
|
|
name = strings.TrimSpace(name)
|
|
if name == "" || len(name) > 255 {
|
|
return qrcode.ErrInvalidName
|
|
}
|
|
u.name = name
|
|
return nil
|
|
}
|
|
|
|
func (u *User) Name() string {
|
|
return u.name
|
|
}
|
|
|
|
func (u *User) setEmail(email string) error {
|
|
email = strings.TrimSpace(email)
|
|
if !auth.IsValidEmail(email) {
|
|
return qrcode.ErrInvalidEmail
|
|
}
|
|
u.email = email
|
|
return nil
|
|
}
|
|
|
|
func (u *User) Email() string {
|
|
return u.email
|
|
}
|
|
|
|
func (u *User) setPassword(password string) error {
|
|
password = strings.TrimSpace(password)
|
|
if password == "" || len(password) < 8 || len(password) > 72 {
|
|
return qrcode.ErrPasswordLength
|
|
}
|
|
hashedPass, err := auth.HashPassword(password)
|
|
if err != nil {
|
|
return fmt.Errorf("%w: %v", qrcode.ErrHashPassword, err)
|
|
}
|
|
u.password = hashedPass
|
|
return nil
|
|
}
|
|
|
|
func (u *User) Password() string {
|
|
return u.password
|
|
}
|
|
|
|
func (u *User) setHashedPassword(hashedPass string) {
|
|
u.password = hashedPass
|
|
}
|
|
|
|
func (u *User) CreatedAt() time.Time {
|
|
return u.createdAt
|
|
}
|
|
|
|
func (u *User) UpdatedAt() time.Time {
|
|
return u.updatedAt
|
|
}
|
|
|
|
func (u *User) DeletedAt() *time.Time {
|
|
return u.deletedAt
|
|
}
|
|
|
|
func (u *User) Restore(
|
|
id uuid.UUID,
|
|
name string,
|
|
email string,
|
|
password string,
|
|
createdAt time.Time,
|
|
updatedAt time.Time,
|
|
deletedAt *time.Time,
|
|
) {
|
|
u.id = id
|
|
u.name = name
|
|
u.email = email
|
|
u.password = password
|
|
u.createdAt = createdAt
|
|
u.updatedAt = updatedAt
|
|
u.deletedAt = deletedAt
|
|
}
|