fix: handle duplicate input

This commit is contained in:
ericprd 2025-02-24 19:47:40 +08:00
parent bd305f0edf
commit 3b2321d5db
7 changed files with 19 additions and 10 deletions

View File

@ -7,8 +7,8 @@ import (
"gorm.io/gorm"
)
func (sr *StaffRepository) GetStaffByEmail(email string) (*authdomain.LoginRepoResponse, error) {
var staff authdomain.LoginRepoResponse
func (sr *StaffRepository) GetStaffByEmail(email string) (*authdomain.Staff, error) {
var staff authdomain.Staff
if email == "" {
return nil, errors.New("email is empty")

View File

@ -10,7 +10,7 @@ type StaffRepository struct {
}
type StaffIntf interface {
GetStaffByEmail(string) (*authdomain.LoginRepoResponse, error)
GetStaffByEmail(string) (*authdomain.Staff, error)
Create(*authdomain.Staff) (*authdomain.Staff, error)
}

View File

@ -3,12 +3,12 @@ package userrepository
import (
"errors"
usermodel "github.com/ardeman/project-legalgo-go/database/user"
authdomain "github.com/ardeman/project-legalgo-go/internal/domain/auth"
"gorm.io/gorm"
)
func (ur *UserRepository) GetUserByEmail(email string) (*usermodel.User, error) {
var user usermodel.User
func (ur *UserRepository) GetUserByEmail(email string) (*authdomain.User, error) {
var user authdomain.User
if email == "" {
return nil, errors.New("email is empty")

View File

@ -2,7 +2,6 @@ package userrepository
import (
"github.com/ardeman/project-legalgo-go/database"
usermodel "github.com/ardeman/project-legalgo-go/database/user"
authdomain "github.com/ardeman/project-legalgo-go/internal/domain/auth"
)
@ -11,7 +10,7 @@ type UserRepository struct {
}
type UserIntf interface {
GetUserByEmail(string) (*usermodel.User, error)
GetUserByEmail(string) (*authdomain.User, error)
CreateUser(*authdomain.User) (*authdomain.User, error)
}

View File

@ -7,8 +7,8 @@ import (
"github.com/ardeman/project-legalgo-go/internal/utilities/utils"
)
func (sv *AuthSvc) LoginAsUser(spec authdomain.LoginReq) (string, error) {
user, err := sv.userRepo.GetUserByEmail(spec.Email)
func (a *AuthSvc) LoginAsUser(spec authdomain.LoginReq) (string, error) {
user, err := a.userRepo.GetUserByEmail(spec.Email)
if err != nil {
return "", errors.New(err.Error())
}

View File

@ -9,6 +9,10 @@ import (
)
func (a *AuthSvc) RegisterStaff(spec authdomain.RegisterStaffReq) (string, error) {
_, err := a.staffRepo.GetStaffByEmail(spec.Email)
if err == nil {
return "", errors.New("this email address is already in use")
}
hashedPwd, err := HashPassword(spec.Password)
if err != nil {
return "", err

View File

@ -9,6 +9,12 @@ import (
)
func (a *AuthSvc) RegisterUser(spec authdomain.RegisterUserReq) (string, error) {
_, err := a.userRepo.GetUserByEmail(spec.Email)
if err == nil {
return "", errors.New("this email address is already in use")
}
subsId, err := a.subsRepo.Create(spec.SubscribePlanID)
if err != nil {
return "", nil