Last active
January 17, 2020 04:24
-
-
Save manakuro/43a49635a12f136b0602e7ec39c6eb15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
// ... | |
e.POST("/users", func(c echo.Context) error { | |
var params model.User | |
user := model.User{} | |
if err := c.Bind(¶ms); !errors.Is(err, nil) { | |
return c.JSON(http.StatusBadRequest, err.Error()) | |
} | |
// Check to see if the user already registered | |
err = db.Where(&model.User{UUID: params.UUID}).First(&user).Error | |
if !errors.Is(err, nil) && !gorm.IsRecordNotFoundError(err) { | |
return c.JSON(http.StatusBadRequest, err.Error()) | |
} | |
if user.UUID != "" { | |
return c.JSON(http.StatusBadRequest, "The user already registered") | |
} | |
// Create a new user when not registered | |
user = params | |
err = db.Create(&user).Error | |
if !errors.Is(err, nil) { | |
return err | |
} | |
return c.JSON(http.StatusCreated, user) | |
}) | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment