Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func Register(c *gin.Context) {
- signUp := SignUp{}
- if c.Bind(&signUp) != nil {
- c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
- return
- }
- if len(signUp.Username) < 6 || len(signUp.Password) < 8 {
- c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
- return
- }
- stmt, err := db.Prepare("INSERT User SET username=?,password=?")
- if err != nil {
- log.Panic(err)
- }
- res, err := stmt.Exec(signUp.Username, signUp.Password)
- if err != nil {
- mysqlerr, ok := err.(*mysql.MySQLError)
- if ok && mysqlerr.Number == 1062 {
- c.JSON(http.StatusConflict, gin.H{"status": http.StatusConflict})
- }
- return
- }
- rows, err := res.RowsAffected()
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
- return
- }
- if rows != 1 {
- c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
- return
- }
- c.JSON(http.StatusCreated, gin.H{"status": http.StatusCreated})
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement