-
Notifications
You must be signed in to change notification settings - Fork 4
User registration API #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Conversation
… unique or not while registering
db/db.go
Outdated
|
|
||
| type Storer interface { | ||
| ListUsers(context.Context) ([]User, error) | ||
| CreateNewUser(context.Context, User) (User, error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename CreateNewUser to CreateUser
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
db/user.go
Outdated
|
|
||
| // CreateNewUser = creates a new user in database | ||
| func (s *pgStore) CreateNewUser(ctx context.Context, u User) (newUser User, err error) { | ||
| tx, err := s.db.Beginx() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to use transaction. we can use s.db to run the query
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed transaction and instead used Exec
db/db.go
Outdated
| type Storer interface { | ||
| ListUsers(context.Context) ([]User, error) | ||
| CreateNewUser(context.Context, User) (User, error) | ||
| CheckUserByEmail(context.Context, string) (bool, User, error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think CheckUserByEmail(context.Context, string) (bool, User, error) is quite ambiguous.
if error occurred then ideally bool value should be true &
if error not occurred then bool value should be false
In CheckUserByEmail method we are using bool & err variable to denote if user is present or not, which i think is confusing
So, instead
Rename the method
From:- CheckUserByEmail(context.Context, string) (bool, User, error)
To :- GetUserByEmail(context.Context, string) (User, error)
so in GetUserByEmail method
if err == sql.NoRows then user is not present &
if err != nil && err != sql.NoRows then db error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
| func (s *pgStore) CheckUserByEmail(ctx context.Context, email string) (check bool, user User, err error) { | ||
| err = s.db.Get(&user, getUserByEmailQuery, email) | ||
| if err != nil { | ||
| if err == sql.ErrNoRows { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to add
if err == sql.ErrNoRows condition in this func
caller of the func should handle it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have used if err == sql.ErrNoRows condition to avoid logging no row found errors and to log only database related errors
service/user_http_test.go
Outdated
| } | ||
|
|
||
| func (suite *UsersHandlerTestSuite) TestRegisterUserSuccess() { | ||
| suite.dbMock.On("CreateNewUser", mock.Anything, mock.Anything).Return(db.User{}, nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avoid using mock.Anything instead use actual User object
for context you can use mock.Anything
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't use mock.Anything for user then we have to manually fill the user object
db/db.go
Outdated
| type Storer interface { | ||
| ListUsers(context.Context) ([]User, error) | ||
| CreateUser(context.Context, User) (User, error) | ||
| // CheckUserByEmail(context.Context, string) (bool, User, error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove commented code
service/user_http.go
Outdated
| // For checking if user already registered | ||
| _, err = deps.Store.GetUserByEmail(req.Context(), user.Email) | ||
|
|
||
| // If check true then user is already registered |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update the comment
service/user_http.go
Outdated
| _, err = deps.Store.GetUserByEmail(req.Context(), user.Email) | ||
|
|
||
| // If check true then user is already registered | ||
| if err != sql.ErrNoRows && err == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only err == nil is enough
service/user_http_test.go
Outdated
| } | ||
|
|
||
| func (suite *UsersHandlerTestSuite) TestRegisterUserFailure() { | ||
| suite.dbMock.On("CreateUser", mock.Anything, mock.Anything).Return(db.User{}, nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avoid using mock.Anything.
you can you mock.Anything for context.Context
| logger.WithField("err", err.Error()).Error("Error while preparing user insert query") | ||
| return | ||
| } | ||
| _, err = stmt.Exec(u) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s.db.NamedExec
No description provided.