Created
October 11, 2021 13:26
-
-
Save axel-andrade/bbc4cd86040c02c852d933a6cca5af34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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