Skip to content

Commit

Permalink
refactor(context): change parameters to use primitive type in user mo…
Browse files Browse the repository at this point in the history
…dule
  • Loading branch information
bastean committed Jun 9, 2024
1 parent f55bb9d commit db8fc5b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 42 deletions.
7 changes: 6 additions & 1 deletion pkg/context/user/application/create/command.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ type CommandHandler struct {
}

func (handler *CommandHandler) Handle(command *Command) error {
user, err := aggregate.NewUser(command.Id, command.Email, command.Username, command.Password)
user, err := aggregate.NewUser(&aggregate.UserPrimitive{
Id: command.Id,
Email: command.Email,
Username: command.Username,
Password: command.Password,
})

if err != nil {
return errors.BubbleUp(err, "Handle")
Expand Down
7 changes: 6 additions & 1 deletion pkg/context/user/application/create/command.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ func (suite *UserCreateTestSuite) SetupTest() {
func (suite *UserCreateTestSuite) TestCreate() {
command := create.RandomCommand()

user, _ := aggregate.NewUser(command.Id, command.Email, command.Username, command.Password)
user, _ := aggregate.NewUser(&aggregate.UserPrimitive{
Id: command.Id,
Email: command.Email,
Username: command.Username,
Password: command.Password,
})

messages := user.Messages

Expand Down
7 changes: 6 additions & 1 deletion pkg/context/user/application/update/command.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ func (suite *UserUpdateTestSuite) SetupTest() {
func (suite *UserUpdateTestSuite) TestUpdate() {
command := update.RandomCommand()

user, _ := aggregate.NewUser(command.Id, command.Email, command.Username, command.Password)
user, _ := aggregate.NewUser(&aggregate.UserPrimitive{
Id: command.Id,
Email: command.Email,
Username: command.Username,
Password: command.Password,
})

idVO, _ := valueobj.NewId(command.Id)

Expand Down
60 changes: 22 additions & 38 deletions pkg/context/user/domain/aggregate/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,23 @@ import (

type User struct {
*aggregates.AggregateRoot
Id models.ValueObject[string]
Email models.ValueObject[string]
Username models.ValueObject[string]
Password models.ValueObject[string]
Verified models.ValueObject[bool]
Id, Email, Username, Password models.ValueObject[string]
Verified models.ValueObject[bool]
}

type UserPrimitive struct {
Id string
Email string
Username string
Password string
Verified bool
Id, Email, Username, Password string
Verified bool
}

func create(id, email, username, password string, verified bool) (*User, error) {
func create(primitive *UserPrimitive) (*User, error) {
aggregateRoot := aggregates.NewAggregateRoot()

idVO, errId := valueobj.NewId(id)
emailVO, errEmail := valueobj.NewEmail(email)
usernameVO, errUsername := valueobj.NewUsername(username)
passwordVO, errPassword := valueobj.NewPassword(password)
verifiedVO, errVerified := valueobj.NewVerified(verified)
idVO, errId := valueobj.NewId(primitive.Id)
emailVO, errEmail := valueobj.NewEmail(primitive.Email)
usernameVO, errUsername := valueobj.NewUsername(primitive.Username)
passwordVO, errPassword := valueobj.NewPassword(primitive.Password)
verifiedVO, errVerified := valueobj.NewVerified(primitive.Verified)

err := errors.Join(errId, errEmail, errUsername, errPassword, errVerified)

Expand Down Expand Up @@ -60,14 +54,8 @@ func (user *User) ToPrimitives() *UserPrimitive {
}
}

func FromPrimitives(userPrimitive *UserPrimitive) (*User, error) {
user, err := create(
userPrimitive.Id,
userPrimitive.Email,
userPrimitive.Username,
userPrimitive.Password,
userPrimitive.Verified,
)
func FromPrimitives(primitive *UserPrimitive) (*User, error) {
user, err := create(primitive)

if err != nil {
return nil, errors.BubbleUp(err, "FromPrimitives")
Expand All @@ -76,26 +64,22 @@ func FromPrimitives(userPrimitive *UserPrimitive) (*User, error) {
return user, nil
}

func NewUser(id, email, username, password string) (*User, error) {
verified := false
func NewUser(primitive *UserPrimitive) (*User, error) {
primitive.Verified = false

user, err := create(
id,
email,
username,
password,
verified,
)
user, err := create(primitive)

if err != nil {
return nil, errors.BubbleUp(err, "NewUser")
}

eventMessage, err := message.NewCreatedSucceededEvent(&message.CreatedSucceededEventAttributes{
Id: id,
Email: email,
Username: username,
})
attributes := &message.CreatedSucceededEventAttributes{
Id: primitive.Id,
Email: primitive.Email,
Username: primitive.Username,
}

eventMessage, err := message.NewCreatedSucceededEvent(attributes)

if err != nil {
return nil, errors.BubbleUp(err, "NewUser")
Expand Down
7 changes: 6 additions & 1 deletion pkg/context/user/domain/aggregate/user.mother.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ func RandomUser() *User {
username, _ := valueobj.RandomUsername()
password, _ := valueobj.RandomPassword()

user, _ := NewUser(id.Value(), email.Value(), username.Value(), password.Value())
user, _ := NewUser(&UserPrimitive{
Id: id.Value(),
Email: email.Value(),
Username: username.Value(),
Password: password.Value(),
})

return user
}

0 comments on commit db8fc5b

Please sign in to comment.