-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SAC-4 Create User POST #42
Changes from 5 commits
2ab62a2
9b96c72
e49c5af
256b786
3897e19
599e5bd
b229f76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,15 @@ package services | |
import ( | ||
"github.com/GenerateNU/sac/backend/src/models" | ||
"github.com/GenerateNU/sac/backend/src/transactions" | ||
|
||
"github.com/GenerateNU/sac/backend/src/utilities" | ||
"github.com/go-playground/validator/v10" | ||
"github.com/gofiber/fiber/v2" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type UserServiceInterface interface { | ||
GetAllUsers() ([]models.User, error) | ||
CreateUser(userBody models.CreateUserRequestBody) (*models.User, error) | ||
} | ||
|
||
type UserService struct { | ||
|
@@ -19,3 +22,36 @@ type UserService struct { | |
func (u *UserService) GetAllUsers() ([]models.User, error) { | ||
return transactions.GetAllUsers(u.DB) | ||
} | ||
|
||
// temporary | ||
func createUserFromRequestBody(userBody models.CreateUserRequestBody) (models.User, error) { | ||
// TL DAVID -- some validation still needs to be done but depends on design | ||
|
||
validate := validator.New() | ||
validate.RegisterValidation("neu_email", utilities.ValidateEmail) | ||
validate.RegisterValidation("password", utilities.ValidatePassword) | ||
if err := validate.Struct(userBody); err != nil { | ||
return models.User{}, fiber.NewError(fiber.StatusBadRequest, err.Error()) | ||
} | ||
|
||
var user models.User | ||
user.NUID = userBody.NUID | ||
user.FirstName = userBody.FirstName | ||
user.LastName = userBody.LastName | ||
user.Email = userBody.Email | ||
// TODO: hash | ||
user.PasswordHash = userBody.Password | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
user.College = models.College(userBody.College) | ||
user.Year = models.Year(userBody.Year) | ||
|
||
return user, nil | ||
} | ||
|
||
func (u *UserService) CreateUser(userBody models.CreateUserRequestBody) (*models.User, error) { | ||
user, err := createUserFromRequestBody(userBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return transactions.CreateUser(u.DB, &user) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ package transactions | |
|
||
import ( | ||
"github.com/GenerateNU/sac/backend/src/models" | ||
|
||
"errors" | ||
"github.com/gofiber/fiber/v2" | ||
"gorm.io/gorm" | ||
) | ||
|
@@ -16,3 +16,43 @@ func GetAllUsers(db *gorm.DB) ([]models.User, error) { | |
|
||
return users, nil | ||
} | ||
|
||
func GetUser(db *gorm.DB, id uint) (*models.User, error) { | ||
var user models.User | ||
if err := db.First(&user, id).Error; err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
return nil, fiber.NewError(fiber.StatusNotFound, err.Error()) | ||
} | ||
|
||
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error()) | ||
} | ||
|
||
return &user, nil | ||
} | ||
|
||
func CreateUser(db *gorm.DB, user *models.User) (*models.User, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need these separate queries or can we allow the DB unique constraints to handle this for us? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just didn't want to throw 500 errors for things related to uniqueness, imo that's a 400 |
||
|
||
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 user, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DOOduneye ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error messages given by the validators seemed kinda wordy and unreadable. For example if a required field like College is missing, the error message is
"Key: 'CreateUserRequestBody.College' Error:Field validation for 'College' failed on the 'required' tag"
and we feel like this can be better written. Thoughts?