Skip to content

Commit

Permalink
SAC-4 Create User POST (#42)
Browse files Browse the repository at this point in the history
Co-authored-by: OJisMe <[email protected]>
Co-authored-by: Garrett Ladley <[email protected]>
Co-authored-by: garrettladley <[email protected]>
  • Loading branch information
4 people authored Jan 22, 2024
1 parent de0127b commit 1829722
Show file tree
Hide file tree
Showing 9 changed files with 473 additions and 20 deletions.
13 changes: 5 additions & 8 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ require (
gorm.io/gorm v1.25.5
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/mcnijman/go-emailaddress v1.1.1 // indirect
)

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
Expand All @@ -47,7 +43,8 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mcnijman/go-emailaddress v1.1.1
github.com/mitchellh/mapstructure v1.5.0
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
Expand All @@ -64,11 +61,11 @@ require (
github.com/valyala/tcplisten v1.0.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/crypto v0.18.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/text v0.14.0
golang.org/x/tools v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
6 changes: 0 additions & 6 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTV
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
Expand All @@ -178,8 +176,6 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -196,8 +192,6 @@ golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
27 changes: 27 additions & 0 deletions backend/src/controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,33 @@ func (u *UserController) GetAllUsers(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(users)
}

// Create User godoc
//
// @Summary Creates a User
// @Description Creates a user
// @ID create-user
// @Tags user
// @Accept json
// @Produce json
// @Success 201 {object} models.User
// @Failure 400 {string} string "failed to create user"
// @Failure 500 {string} string "internal server error"
// @Router /api/v1/users/ [post]
func (u *UserController) CreateUser(c *fiber.Ctx) error {
var userBody models.UserRequestBody

if err := c.BodyParser(&userBody); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "failed to process the request")
}

user, err := u.userService.CreateUser(userBody)
if err != nil {
return err
}

return c.Status(fiber.StatusCreated).JSON(user)
}

// GetUser godoc
//
// @Summary Gets a user
Expand Down
2 changes: 1 addition & 1 deletion backend/src/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ type UserRequestBody struct {
Password string `json:"password" validate:"required,password"`
College College `json:"college" validate:"required,oneof=CAMD DMSB KCCS CE BCHS SL CPS CS CSSH"`
Year Year `json:"year" validate:"required,min=1,max=6"`
}
}
1 change: 1 addition & 0 deletions backend/src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func userRoutes(router fiber.Router, userService services.UserServiceInterface)
users := router.Group("/users")

users.Get("/", userController.GetAllUsers)
users.Post("/", userController.CreateUser)
users.Get("/:id", userController.GetUser)
users.Patch("/:id", userController.UpdateUser)
}
Expand Down
39 changes: 38 additions & 1 deletion backend/src/services/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

type UserServiceInterface interface {
GetAllUsers() ([]models.User, error)
CreateUser(userBody models.UserRequestBody) (*models.User, error)
GetUser(id string) (*models.User, error)
UpdateUser(id string, userBody models.UserRequestBody) (*models.User, error)
}
Expand All @@ -27,6 +28,43 @@ 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)

if err := validate.Struct(userBody); err != nil {
return nil, fiber.NewError(fiber.StatusBadRequest, err.Error())
}

passwordHash, err := auth.ComputePasswordHash(userBody.Password)

if err != nil {
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
}

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)
}

func (u *UserService) GetUser(id string) (*models.User, error) {
idAsUint, err := utilities.ValidateID(id)
if err != nil {
Expand All @@ -36,7 +74,6 @@ func (u *UserService) GetUser(id string) (*models.User, error) {
return transactions.GetUser(u.DB, *idAsUint)
}

// Updates a user
func (u *UserService) UpdateUser(id string, userBody models.UserRequestBody) (*models.User, error) {
idAsUint, err := utilities.ValidateID(id)
if err != nil {
Expand Down
29 changes: 27 additions & 2 deletions backend/src/transactions/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"

"github.com/GenerateNU/sac/backend/src/models"

"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)
Expand All @@ -21,7 +20,6 @@ func GetAllUsers(db *gorm.DB) ([]models.User, error) {

func GetUser(db *gorm.DB, id uint) (*models.User, error) {
var user models.User

if err := db.Omit("password_hash").First(&user, id).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.ErrNotFound
Expand All @@ -33,6 +31,33 @@ func GetUser(db *gorm.DB, id uint) (*models.User, error) {
return &user, nil
}

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 user, nil
}

func UpdateUser(db *gorm.DB, id uint, user models.User) (*models.User, error) {
var existingUser models.User

Expand Down
Loading

0 comments on commit 1829722

Please sign in to comment.