Skip to content

Instantly share code, notes, and snippets.

@axel-andrade
Created October 11, 2021 13:26
Show Gist options
  • Save axel-andrade/bbc4cd86040c02c852d933a6cca5af34 to your computer and use it in GitHub Desktop.
Save axel-andrade/bbc4cd86040c02c852d933a6cca5af34 to your computer and use it in GitHub Desktop.
package entities
import (
"errors"
ERROR "go_clean_api/api/shared/constants/errors"
v "go_clean_api/api/shared/validators"
"time"
)
type User struct {
Base
Name string `json:"name" bson:"name"`
Email string `json:"email" bson:"email"`
Password string `json:"-" bson:"-"`
}
func BuildUser(name string, email string, password string) (*User, error) {
u := &User{
Name: name,
Email: email,
Password: password,
}
if err := u.validate(); err != nil {
return err
}
u.CreatedAt = time.Now()
u.Password = string(u.Password)
return user, nil
}
func (u *User) validate() error {
if v.IsEmpty(u.Name) {
return errors.New(ERROR.NAME_IS_EMPTY)
}
if !v.IsValidEmail(u.Email) {
return errors.New(ERROR.INVALID_EMAIL)
}
if !v.IsValidPassword(u.Password) {
return errors.New(ERROR.INVALID_PASSWORD)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment