From 9ea313a88a00f473279047022d15722fd089bf76 Mon Sep 17 00:00:00 2001 From: David Oduneye Date: Mon, 22 Jan 2024 18:02:56 -0500 Subject: [PATCH] rebase --- backend/src/services/user.go | 33 ++++++++------------------------ backend/src/transactions/user.go | 21 +------------------- 2 files changed, 9 insertions(+), 45 deletions(-) diff --git a/backend/src/services/user.go b/backend/src/services/user.go index 5ea9f5f31..f8e048492 100644 --- a/backend/src/services/user.go +++ b/backend/src/services/user.go @@ -28,39 +28,22 @@ func (u *UserService) GetAllUsers() ([]models.User, error) { return transactions.GetAllUsers(u.DB) } -func createUserFromRequestBody(userBody models.UserRequestBody) (*models.User, error) { - validate := validator.New() - - validate.RegisterValidation("neu_email", utilities.ValidateEmail) - validate.RegisterValidation("password", utilities.ValidatePassword) +func (u *UserService) CreateUser(userBody models.UserRequestBody) (*models.User, error) { + if err := u.Validate.Struct(userBody); err != nil { + return nil, fiber.ErrBadRequest + } - if err := validate.Struct(userBody); err != nil { - return nil, fiber.NewError(fiber.StatusBadRequest, err.Error()) + user, err := utilities.MapResponseToModel(userBody, &models.User{}) + if err != nil { + return nil, fiber.ErrInternalServerError } passwordHash, err := auth.ComputePasswordHash(userBody.Password) - if err != nil { - return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error()) + return nil, fiber.ErrInternalServerError } - var user models.User - user.NUID = userBody.NUID - user.FirstName = userBody.FirstName - user.LastName = userBody.LastName - user.Email = userBody.Email user.PasswordHash = *passwordHash - user.College = models.College(userBody.College) - user.Year = models.Year(userBody.Year) - - return &user, nil -} - -func (u *UserService) CreateUser(userBody models.UserRequestBody) (*models.User, error) { - user, err := createUserFromRequestBody(userBody) - if err != nil { - return nil, err - } return transactions.CreateUser(u.DB, user) } diff --git a/backend/src/transactions/user.go b/backend/src/transactions/user.go index 472e7d56e..a735a06fa 100644 --- a/backend/src/transactions/user.go +++ b/backend/src/transactions/user.go @@ -32,27 +32,8 @@ func GetUser(db *gorm.DB, id uint) (*models.User, error) { } func CreateUser(db *gorm.DB, user *models.User) (*models.User, error) { - - var existing models.User - - if err := db.Where("email = ?", user.Email).First(&existing).Error; err != nil { - if !errors.Is(err, gorm.ErrRecordNotFound) { - return nil, fiber.NewError(fiber.StatusInternalServerError, "failed to create user") - } - } else { - return nil, fiber.NewError(fiber.StatusBadRequest, "user with that email already exists") - } - - if err := db.Where("nuid = ?", user.NUID).First(&existing).Error; err != nil { - if !errors.Is(err, gorm.ErrRecordNotFound) { - return nil, fiber.NewError(fiber.StatusInternalServerError, "failed to create user") - } - } else { - return nil, fiber.NewError(fiber.StatusBadRequest, "user with that nuid already exists") - } - if err := db.Create(user).Error; err != nil { - return nil, fiber.NewError(fiber.StatusInternalServerError, "failed to create user") + return nil, fiber.ErrInternalServerError } return user, nil