github.com/lovung/GoCleanArchitecture@v0.0.0-20210302152432-50d91fd29f9f/app/internal/domain/entity/user_entity.go (about)

     1  package entity
     2  
     3  import (
     4  	"crypto/rand"
     5  	"time"
     6  
     7  	"github.com/lovung/GoCleanArchitecture/pkg/hasher"
     8  
     9  	"github.com/oklog/ulid"
    10  )
    11  
    12  // User entity
    13  type User struct {
    14  	ID       string
    15  	Username string
    16  	Password string
    17  }
    18  
    19  // HashPassword user.Password <- hash(user.Password)
    20  func (e *User) HashPassword() error {
    21  	hashed, err := hasher.HashPassword(e.Password)
    22  	if err != nil {
    23  		return err
    24  	}
    25  	e.Password = hashed
    26  	return nil
    27  }
    28  
    29  // GenID to generate ID
    30  func (e *User) GenID() (err error) {
    31  	entropy := ulid.Monotonic(rand.Reader, 0)
    32  	id, err := ulid.New(ulid.Timestamp(time.Now()), entropy)
    33  	if err != nil {
    34  		return err
    35  	}
    36  	e.ID = id.String()
    37  	return err
    38  }