From 8041ab2bc48cc197edac6f0642816b711e41b99d Mon Sep 17 00:00:00 2001 From: smavros Date: Wed, 22 May 2019 15:33:34 +0200 Subject: [PATCH] Implements user registration endpoint --- routes/user/userEndpoints.go | 47 +++++++++++++++++++++++++++++++++--- routes/user/userMethods.go | 13 +++++++--- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/routes/user/userEndpoints.go b/routes/user/userEndpoints.go index db161d6..0bb7cf3 100644 --- a/routes/user/userEndpoints.go +++ b/routes/user/userEndpoints.go @@ -2,6 +2,7 @@ package user import ( //"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" + "fmt" "github.com/gin-gonic/gin" "net/http" ) @@ -12,9 +13,9 @@ func VisitorAuthenticate(r *gin.RouterGroup) { } func UsersRegister(r *gin.RouterGroup) { - r.POST("/users", userRegistrationEp) + r.POST("", userRegistrationEp) r.PUT("/:UserID", userUpdateEp) - r.GET("/", usersReadEp) + r.GET("", usersReadEp) r.GET("/:UserID", userReadEp) //r.GET("/me", userSelfEp) // TODO: this conflicts with GET /:userID r.DELETE("/:UserID", userDeleteEp) @@ -84,8 +85,48 @@ func usersReadEp(c *gin.Context) { func userRegistrationEp(c *gin.Context) { + // Bind the response (context) with the User struct + var newUser User + if err := c.BindJSON(&newUser); err != nil { + // TODO: do something other than panic ... + panic(err) + } + + // TODO: validate the User for: + // - username + // - email + // - role + // and in case of error raise 422 + + // Check that the username is NOT taken + _, err := FindUserByUsername(newUser.Username) + if err == nil { + c.JSON(http.StatusUnprocessableEntity, gin.H{ + "message": "Username is already taken", + }) + return + } + + // Hash the password before saving it to the DB + err = newUser.SetPassword(newUser.Password) + if err != nil { + c.JSON(http.StatusUnprocessableEntity, gin.H{ + "message": "Unable to encrypt the password", + }) + return + } + + // Save the user in the DB + err = newUser.save() + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "Unable to create new user", + }) + return + } + c.JSON(http.StatusOK, gin.H{ - "message": "NOT implemented", + "user": fmt.Sprintf(newUser.Username), }) } diff --git a/routes/user/userMethods.go b/routes/user/userMethods.go index dd88f9f..e88bd49 100644 --- a/routes/user/userMethods.go +++ b/routes/user/userMethods.go @@ -30,6 +30,12 @@ func FindUserByUsername(username string) (User, error) { return user, err } +func (u *User) save() error { + db := common.GetDB() + err := db.Create(u).Error + return err +} + func (u *User) SetPassword(password string) error { if len(password) == 0 { return fmt.Errorf("Password cannot be empty") @@ -49,7 +55,8 @@ func (u *User) validatePassword(password string) error { return bcrypt.CompareHashAndPassword(hashedPassword, loginPassword) } -func (u *User) update(data interface{}) error { - // TODO: Not implemented - return nil +func (u *User) update(modifiedUser User) error { + db := common.GetDB() + err := db.Model(u).Update(modifiedUser).Error + return err }