Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
DOOduneye committed Jan 22, 2024
1 parent 08cb5c0 commit 9ea313a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 45 deletions.
33 changes: 8 additions & 25 deletions backend/src/services/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
21 changes: 1 addition & 20 deletions backend/src/transactions/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9ea313a

Please sign in to comment.