Browse Source

:construction: add validator demo

Jeremy Zheng 4 years ago
parent
commit
b44429e94b
5 changed files with 38 additions and 6 deletions
  1. 6 5
      api/README.md
  2. 1 0
      api/go.mod
  3. 2 0
      api/go.sum
  4. 1 0
      api/main.go
  5. 28 1
      api/mint/demo.go

+ 6 - 5
api/README.md

@@ -2,7 +2,7 @@
 
 ```bash
 go mod tidy
-go run ./cmd/mint
+go run .
 ```
 
 ## Documents
@@ -12,8 +12,9 @@ go run ./cmd/mint
 
 ## SOP
 
-- HTTP Router(GET/POST/PUT/PATCH/DELETE)
-- HTTP Request with JSON body
-- HTTP Response with JSON body
-- Gorm (Create Table): mint/db.go
+- Gin: Using GET, POST, PUT, PATCH, DELETE
+- Gin: Parameters in path
+- Gin: Querystring parameters
+- Gin: Model binding and validation
+- Gorm (Create Table with raw sql): mint/db.go
 - Gorm insert/update/delete/select

+ 1 - 0
api/go.mod

@@ -4,6 +4,7 @@ go 1.16
 
 require (
 	github.com/gin-gonic/gin v1.7.2 // indirect
+	github.com/go-playground/validator v9.31.0+incompatible // indirect
 	github.com/go-playground/validator/v10 v10.6.1 // indirect
 	github.com/golang/protobuf v1.5.2 // indirect
 	github.com/jackc/pgproto3/v2 v2.1.0 // indirect

+ 2 - 0
api/go.sum

@@ -72,6 +72,8 @@ github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8c
 github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
 github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
 github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
+github.com/go-playground/validator v9.31.0+incompatible h1:UA72EPEogEnq76ehGdEDp4Mit+3FDh548oRqwVgNsHA=
+github.com/go-playground/validator v9.31.0+incompatible/go.mod h1:yrEkQXlcI+PugkyDjY2bRrL/UBU4f3rvrgkN3V8JEig=
 github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
 github.com/go-playground/validator/v10 v10.6.1 h1:W6TRDXt4WcWp4c4nf/G+6BkGdhiIo0k417gfr+V6u4I=
 github.com/go-playground/validator/v10 v10.6.1/go.mod h1:xm76BBt941f7yWdGnI2DVPFFg1UK3YY04qifoXU3lOk=

+ 1 - 0
api/main.go

@@ -15,6 +15,7 @@ func main() {
 
 	// TODO 在这里进行http mount
 	rt.GET("/demo/get", mint.GetDemo(db))
+	rt.POST("/demo/sign-in", mint.LoginDemo(db))
 
 	rt.Run()
 }

+ 28 - 1
api/mint/demo.go

@@ -1,14 +1,41 @@
 package mint
 
 import (
+	"net/http"
+
 	"github.com/gin-gonic/gin"
 	"gorm.io/gorm"
 )
 
+type Login struct {
+	User     string `form:"user" json:"user" xml:"user"  binding:"required"`
+	Password string `form:"password" json:"password" xml:"password" binding:"required"`
+}
+
+func LoginDemo(_db *gorm.DB) gin.HandlerFunc {
+	return func(c *gin.Context) {
+		// TODO 在这里进行db操作
+		var form Login
+		if err := c.ShouldBindJSON(&form); err != nil {
+			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
+			return
+		}
+
+		if form.User != "manu" || form.Password != "123" {
+			c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
+			return
+		}
+
+		c.JSON(http.StatusOK, gin.H{
+			"message": "pong",
+		})
+	}
+}
+
 func GetDemo(_db *gorm.DB) gin.HandlerFunc {
 	return func(c *gin.Context) {
 		// TODO 在这里进行db操作
-		c.JSON(200, gin.H{
+		c.JSON(http.StatusOK, gin.H{
 			"message": "pong",
 		})
 	}