Skip to content

Commit

Permalink
refactor(context): add pointer to search criteria type
Browse files Browse the repository at this point in the history
  • Loading branch information
bastean committed Jun 9, 2024
1 parent 5027a31 commit 8648a18
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 31 deletions.
4 changes: 2 additions & 2 deletions pkg/context/user/application/delete/command.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func (suite *UserDeleteTestSuite) TestDelete() {
Password: user.Password.Value(),
}

filter := model.RepositorySearchCriteria{
criteria := &model.RepositorySearchCriteria{
Id: user.Id,
}

suite.repository.On("Search", filter).Return(user)
suite.repository.On("Search", criteria).Return(user)

suite.hashing.On("IsNotEqual", user.Password.Value(), user.Password.Value()).Return(false)

Expand Down
2 changes: 1 addition & 1 deletion pkg/context/user/application/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Delete struct {
}

func (delete *Delete) Run(input *Input) (types.Empty, error) {
user, err := delete.Repository.Search(model.RepositorySearchCriteria{
user, err := delete.Repository.Search(&model.RepositorySearchCriteria{
Id: input.Id,
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/context/user/application/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Login struct {
}

func (login *Login) Run(input *Input) (*aggregate.User, error) {
user, err := login.Repository.Search(model.RepositorySearchCriteria{
user, err := login.Repository.Search(&model.RepositorySearchCriteria{
Email: input.Email,
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/context/user/application/login/query.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func (suite *UserLoginTestSuite) TestLogin() {
Password: user.Password.Value(),
}

filter := model.RepositorySearchCriteria{
criteria := &model.RepositorySearchCriteria{
Email: user.Email,
}

suite.repository.On("Search", filter).Return(user)
suite.repository.On("Search", criteria).Return(user)

suite.hashing.On("IsNotEqual", user.Password.Value(), user.Password.Value()).Return(false)

Expand Down
4 changes: 2 additions & 2 deletions pkg/context/user/application/read/query.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func (suite *UserReadTestSuite) TestLogin() {
Id: user.Id.Value(),
}

filter := model.RepositorySearchCriteria{
criteria := &model.RepositorySearchCriteria{
Id: user.Id,
}

suite.repository.On("Search", filter).Return(user)
suite.repository.On("Search", criteria).Return(user)

expected := user.ToPrimitives()

Expand Down
2 changes: 1 addition & 1 deletion pkg/context/user/application/read/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Read struct {
}

func (read *Read) Run(id models.ValueObject[string]) (*aggregate.User, error) {
user, err := read.Repository.Search(model.RepositorySearchCriteria{
user, err := read.Repository.Search(&model.RepositorySearchCriteria{
Id: id,
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/context/user/application/update/command.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ func (suite *UserUpdateTestSuite) TestUpdate() {

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

filter := model.RepositorySearchCriteria{
criteria := &model.RepositorySearchCriteria{
Id: idVO,
}

suite.repository.On("Search", filter).Return(user)
suite.repository.On("Search", criteria).Return(user)

suite.hashing.On("IsNotEqual", user.Password.Value(), command.Password).Return(false)

Expand Down
2 changes: 1 addition & 1 deletion pkg/context/user/application/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (update *Update) Run(userUpdate *Command) (types.Empty, error) {
return nil, errors.BubbleUp(err, "Run")
}

userRegistered, err := update.Repository.Search(model.RepositorySearchCriteria{
userRegistered, err := update.Repository.Search(&model.RepositorySearchCriteria{
Id: idVO,
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/context/user/application/verify/command.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func (suite *UserVerifyTestSuite) TestVerify() {

user.Password = nil

filter := model.RepositorySearchCriteria{
criteria := &model.RepositorySearchCriteria{
Id: idVO,
}

suite.repository.On("Search", filter).Return(user)
suite.repository.On("Search", criteria).Return(user)

suite.repository.On("Update", user)

Expand Down
2 changes: 1 addition & 1 deletion pkg/context/user/application/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Verify struct {
}

func (verify *Verify) Run(id models.ValueObject[string]) (types.Empty, error) {
userRegistered, err := verify.Repository.Search(model.RepositorySearchCriteria{
userRegistered, err := verify.Repository.Search(&model.RepositorySearchCriteria{
Id: id,
})

Expand Down
5 changes: 2 additions & 3 deletions pkg/context/user/domain/model/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
)

type RepositorySearchCriteria struct {
Id models.ValueObject[string]
Email models.ValueObject[string]
Id, Email models.ValueObject[string]
}

type Repository interface {
Save(user *aggregate.User) error
Update(user *aggregate.User) error
Delete(id models.ValueObject[string]) error
Search(filter RepositorySearchCriteria) (*aggregate.User, error)
Search(criteria *RepositorySearchCriteria) (*aggregate.User, error)
}
18 changes: 9 additions & 9 deletions pkg/context/user/infrastructure/persistence/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,18 @@ func (db *UserCollection) Delete(id models.ValueObject[string]) error {
return nil
}

func (db *UserCollection) Search(filter model.RepositorySearchCriteria) (*aggregate.User, error) {
func (db *UserCollection) Search(criteria *model.RepositorySearchCriteria) (*aggregate.User, error) {
var searchFilter bson.M
var index string

if filter.Email != nil {
searchFilter = bson.M{"email": filter.Email.Value()}
index = filter.Email.Value()
if criteria.Email != nil {
searchFilter = bson.M{"email": criteria.Email.Value()}
index = criteria.Email.Value()
}

if filter.Id != nil {
searchFilter = bson.M{"id": filter.Id.Value()}
index = filter.Id.Value()
if criteria.Id != nil {
searchFilter = bson.M{"id": criteria.Id.Value()}
index = criteria.Id.Value()
}

result := db.collection.FindOne(context.Background(), searchFilter)
Expand All @@ -150,8 +150,8 @@ func (db *UserCollection) Search(filter model.RepositorySearchCriteria) (*aggreg
Where: "Search",
What: "failure to search for a user",
Why: errors.Meta{
"Id": filter.Id.Value(),
"Email": filter.Email.Value(),
"Id": criteria.Id.Value(),
"Email": criteria.Email.Value(),
},
Who: err,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ func (suite *UserMongoRepositoryTestSuite) TestSearch() {

suite.NoError(suite.sut.Save(expected))

filter := model.RepositorySearchCriteria{
criteria := &model.RepositorySearchCriteria{
Email: expected.Email,
}

user, err := suite.sut.Search(filter)
user, err := suite.sut.Search(criteria)

suite.NoError(err)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (repository *RepositoryMock) Delete(id models.ValueObject[string]) error {
return nil
}

func (repository *RepositoryMock) Search(filter model.RepositorySearchCriteria) (*aggregate.User, error) {
args := repository.Called(filter)
func (repository *RepositoryMock) Search(criteria *model.RepositorySearchCriteria) (*aggregate.User, error) {
args := repository.Called(criteria)
return args.Get(0).(*aggregate.User), nil
}

0 comments on commit 8648a18

Please sign in to comment.