feat(user): add user email duplication check

This commit is contained in:
LinkinStar 2022-12-12 14:29:24 +08:00
parent 10ae0bb4c9
commit 18ca572a38
3 changed files with 22 additions and 1 deletions

View File

@ -89,6 +89,17 @@ func (ur *userBackyardRepo) GetUserInfo(ctx context.Context, userID string) (use
return
}
// GetUserInfoByEmail get user info
func (ur *userBackyardRepo) GetUserInfoByEmail(ctx context.Context, email string) (user *entity.User, exist bool, err error) {
userInfo := &entity.User{}
exist, err = ur.data.DB.Where("e_mail = ?", email).
Where("status != ?", entity.UserStatusDeleted).Get(userInfo)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}
// GetUserPage get user page
func (ur *userBackyardRepo) GetUserPage(ctx context.Context, page, pageSize int, user *entity.User,
usernameOrDisplayName string, isStaff bool) (users []*entity.User, total int64, err error) {

View File

@ -151,7 +151,8 @@ func (ur *userRepo) GetByUsername(ctx context.Context, username string) (userInf
// GetByEmail get user by email
func (ur *userRepo) GetByEmail(ctx context.Context, email string) (userInfo *entity.User, exist bool, err error) {
userInfo = &entity.User{}
exist, err = ur.data.DB.Where("e_mail = ?", email).Get(userInfo)
exist, err = ur.data.DB.Where("e_mail = ?", email).
Where("status != ?", entity.UserStatusDeleted).Get(userInfo)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}

View File

@ -25,6 +25,7 @@ import (
type UserBackyardRepo interface {
UpdateUserStatus(ctx context.Context, userID string, userStatus, mailStatus int, email string) (err error)
GetUserInfo(ctx context.Context, userID string) (user *entity.User, exist bool, err error)
GetUserInfoByEmail(ctx context.Context, email string) (user *entity.User, exist bool, err error)
GetUserPage(ctx context.Context, page, pageSize int, user *entity.User,
usernameOrDisplayName string, isStaff bool) (users []*entity.User, total int64, err error)
AddUser(ctx context.Context, user *entity.User) (err error)
@ -103,6 +104,14 @@ func (us *UserBackyardService) UpdateUserRole(ctx context.Context, req *schema.U
// AddUser add user
func (us *UserBackyardService) AddUser(ctx context.Context, req *schema.AddUserReq) (err error) {
_, has, err := us.userRepo.GetUserInfoByEmail(ctx, req.Email)
if err != nil {
return err
}
if has {
return errors.BadRequest(reason.EmailDuplicate)
}
hashPwd, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
if err != nil {
return err