diff --git a/pkg/context/user/application/create/command.handler.go b/pkg/context/user/application/create/command.handler.go index 49b0d27f..0b5c06a8 100644 --- a/pkg/context/user/application/create/command.handler.go +++ b/pkg/context/user/application/create/command.handler.go @@ -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") diff --git a/pkg/context/user/application/create/command.handler_test.go b/pkg/context/user/application/create/command.handler_test.go index d48ccf96..80dcc9e3 100644 --- a/pkg/context/user/application/create/command.handler_test.go +++ b/pkg/context/user/application/create/command.handler_test.go @@ -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 diff --git a/pkg/context/user/application/update/command.handler_test.go b/pkg/context/user/application/update/command.handler_test.go index d1dc151e..9b853fb3 100644 --- a/pkg/context/user/application/update/command.handler_test.go +++ b/pkg/context/user/application/update/command.handler_test.go @@ -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) diff --git a/pkg/context/user/domain/aggregate/user.go b/pkg/context/user/domain/aggregate/user.go index 2cbde6a3..8e5e4311 100644 --- a/pkg/context/user/domain/aggregate/user.go +++ b/pkg/context/user/domain/aggregate/user.go @@ -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) @@ -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") @@ -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") diff --git a/pkg/context/user/domain/aggregate/user.mother.go b/pkg/context/user/domain/aggregate/user.mother.go index c7da657f..5c7ecbcc 100644 --- a/pkg/context/user/domain/aggregate/user.mother.go +++ b/pkg/context/user/domain/aggregate/user.mother.go @@ -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 }