Refactored database pkg - added function to detect duplicated rows error
Fixed Register method for detect duplicated username/email
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
db "git.ego.freeddns.org/egommerce/identity-service/pkg/database"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
@@ -44,6 +45,9 @@ func (a *Auth) Register(email, login, passwd string) (string, error) {
|
||||
sql := `INSERT INTO identity.users(email, username, password) VALUES($1, $2, $3) LIMIT 1 RETURNING id`
|
||||
err := a.db.QueryRow(context.Background(), sql, email, login, passwd).Scan(&id)
|
||||
if err != nil {
|
||||
if err = db.IsDuplicatedRow(err); err != nil {
|
||||
return "", errors.New("username/email is already taken")
|
||||
}
|
||||
return "", errors.New("Failed to create new user: " + err.Error())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user