Skip to content

Commit

Permalink
Convenience method to clear db err added for sql
Browse files Browse the repository at this point in the history
  • Loading branch information
markdicksonjr committed Jan 1, 2019
1 parent 04590e2 commit 8551a4a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
6 changes: 6 additions & 0 deletions database/sql/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ func (s *Extension) Destroy(app *nibbler.Application) error {
return nil
}

func (s *Extension) GetAndClearError() error {
err := s.Db.Error
s.Db.Error = nil
return err
}

func (s *Extension) getBestDialect(app *nibbler.Application) (*string, error) {
var urlParsed *url.URL = nil
var parseError error = nil
Expand Down
18 changes: 10 additions & 8 deletions user/database/sql/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s *Extension) GetUserById(id string) (*user.User, error) {
return nil, nil
}

return &userValue, s.SqlExtension.Db.Error
return &userValue, s.SqlExtension.GetAndClearError()
}

func (s *Extension) GetUserByEmail(email string) (*user.User, error) {
Expand All @@ -39,7 +39,7 @@ func (s *Extension) GetUserByEmail(email string) (*user.User, error) {
return nil, nil
}

return &userValue, s.SqlExtension.Db.Error
return &userValue, s.SqlExtension.GetAndClearError()
}

func (s *Extension) GetUserByUsername(username string) (*user.User, error) {
Expand All @@ -53,7 +53,7 @@ func (s *Extension) GetUserByUsername(username string) (*user.User, error) {
}

// TODO: nil, return code?, db error code?
return &userValue, s.SqlExtension.Db.Error
return &userValue, s.SqlExtension.GetAndClearError()
}

func (s *Extension) GetUserByPasswordResetToken(token string) (*user.User, error) {
Expand All @@ -66,7 +66,7 @@ func (s *Extension) GetUserByPasswordResetToken(token string) (*user.User, error
return nil, nil
}

return &userValue, s.SqlExtension.Db.Error
return &userValue, s.SqlExtension.GetAndClearError()
}

func (s *Extension) GetUserByEmailValidationToken(token string) (*user.User, error) {
Expand All @@ -79,39 +79,41 @@ func (s *Extension) GetUserByEmailValidationToken(token string) (*user.User, err
return nil, nil
}

return &userValue, s.SqlExtension.Db.Error
return &userValue, s.SqlExtension.GetAndClearError()
}

func (s *Extension) Create(user *user.User) (*user.User, error) {
s.SqlExtension.Db.Error = nil
s.SqlExtension.Db = s.SqlExtension.Db.Create(user)
// TODO: nil, return code?, db error code?
return user, s.SqlExtension.Db.Error
return user, s.SqlExtension.GetAndClearError()
}

func (s *Extension) Update(userValue *user.User) error {
// TODO: possibly use First(), update fields we care about, then use Save
// Update will not save nil values, but Save will, presumably

s.SqlExtension.Db.Error = nil

s.SqlExtension.Db = s.SqlExtension.Db.Model(userValue).Updates(user.User{
ID: userValue.ID,
FirstName: userValue.FirstName,
LastName: userValue.LastName,
PasswordResetToken: userValue.PasswordResetToken,
PasswordResetExpiration: userValue.PasswordResetExpiration,
})
return s.SqlExtension.Db.Error
return s.SqlExtension.GetAndClearError()
}

func (s *Extension) UpdatePassword(userValue *user.User) (error) {
s.SqlExtension.Db.Error = nil

s.SqlExtension.Db = s.SqlExtension.Db.Model(userValue).Updates(user.User{
ID: userValue.ID,
Password: userValue.Password,
})

s.SqlExtension.Db = sql.NullifyField(s.SqlExtension.Db, "password_reset_token")
s.SqlExtension.Db = sql.NullifyField(s.SqlExtension.Db, "password_reset_token_expiration")
return s.SqlExtension.Db.Error
return s.SqlExtension.GetAndClearError()
}

0 comments on commit 8551a4a

Please sign in to comment.