package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"time"
"github.com/gin-gonic/gin"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type User struct {
gorm.Model
Name string `gorm:"type:varchar(20);not null"`
Telephone string `gorm:"varchar(11);not null;unique"`
Password string `gorm:"size:255;not null"`
}
func main() {
db := InitDB()
fmt.Println("hello world")
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.POST("/api/auth/register", func(ctx *gin.Context) {
// get parameters
name := ctx.PostForm("name")
telephone := ctx.PostForm("telephone")
password := ctx.PostForm("password")
// data validation
// gin.H == map[string]interface{}
// log.Println(name, telephone, password)
if len(telephone) != 11 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{
"code": 422,
"msg": "Mobile phone number must be 11 digits",
})
return
}
if len(password) < 6 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{
"code": 422,
"msg": "The password cannot be less than 6 digits",
})
return
}
// if there is no name, pass a random 10 bit string.
if len(name) == 0 {
name = RandomString(10)
}
log.Println(name, telephone, password)
//determine whether the mobile-phone nember exists
if isTelephoneExist(db, telephone) {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{
"code": 422,
"msg": "user is exist",
})
return
}
//if user is not exist ,then create user
newUser := User{
Name: name,
Telephone: telephone,
Password: password,
}
db.Create(&newUser)
// return results
ctx.JSON(200, gin.H{
"message": "success",
})
})
panic(r.Run())
}
func RandomString(n int) string {
var letters = []byte("QWERRTUIIOOPPDFGGHHJJKKL")
result := make([]byte, n)
rand.Seed(time.Now().Unix())
for i := range result {
result[i] = letters[rand.Intn(len(letters))]
}
return string(result)
}
func InitDB() *gorm.DB {
// driverName := "mysql"
host := "your host"
port := "3306"
database := "gin_essential"
username := "gin_essential"
password := "yourpassword"
charset := "utf8"
args := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=true&loc=Local",
username, password, host, port, database, charset)
db, err := gorm.Open(mysql.Open(args), &gorm.Config{})
if err != nil {
panic("failed to connect database,err: " + err.Error())
}
// migration schema
db.AutoMigrate(&User{})
return db
}
func isTelephoneExist(db *gorm.DB, telephone string) bool {
var user User
db.Where("telephone=?", telephone).First(&user)
if user.ID != 0 {
return true
}
return false
}