Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. func Register(c *gin.Context) {
  2. signUp := SignUp{}
  3. if c.Bind(&signUp) != nil {
  4. c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
  5. return
  6. }
  7.  
  8. if len(signUp.Username) < 6 || len(signUp.Password) < 8 {
  9. c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
  10. return
  11. }
  12.  
  13. stmt, err := db.Prepare("INSERT User SET username=?,password=?")
  14. if err != nil {
  15. log.Panic(err)
  16. }
  17. res, err := stmt.Exec(signUp.Username, signUp.Password)
  18. if err != nil {
  19. mysqlerr, ok := err.(*mysql.MySQLError)
  20. if ok && mysqlerr.Number == 1062 {
  21. c.JSON(http.StatusConflict, gin.H{"status": http.StatusConflict})
  22. }
  23. return
  24. }
  25. rows, err := res.RowsAffected()
  26. if err != nil {
  27. c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
  28. return
  29. }
  30. if rows != 1 {
  31. c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
  32. return
  33. }
  34.  
  35. c.JSON(http.StatusCreated, gin.H{"status": http.StatusCreated})
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement