gin funn and testing
This commit is contained in:
29
lib/app/form.go
Normal file
29
lib/app/form.go
Normal file
@ -0,0 +1,29 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"gin-server/lib/messages"
|
||||
"github.com/astaxie/beego/validation"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// BindAndValid binds and validates data
|
||||
func BindAndValid(c *gin.Context, form interface{}) (int, int) {
|
||||
err := c.Bind(form)
|
||||
if err != nil {
|
||||
return http.StatusBadRequest, messages.INVALID_PARAMS
|
||||
}
|
||||
|
||||
valid := validation.Validation{}
|
||||
check, err := valid.Valid(form)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, messages.ERROR
|
||||
}
|
||||
if !check {
|
||||
MarkErrors(valid.Errors)
|
||||
return http.StatusBadRequest, messages.INVALID_PARAMS
|
||||
}
|
||||
|
||||
return http.StatusOK, messages.SUCCESS
|
||||
}
|
||||
|
15
lib/app/request.go
Normal file
15
lib/app/request.go
Normal file
@ -0,0 +1,15 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego/validation"
|
||||
"gin-server/lib/logging"
|
||||
)
|
||||
|
||||
// MarkErrors logs error logs
|
||||
func MarkErrors(errors []*validation.Error) {
|
||||
for _, err := range errors {
|
||||
logging.Info(err.Key, err.Message)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
26
lib/app/response.go
Normal file
26
lib/app/response.go
Normal file
@ -0,0 +1,26 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"gin-server/lib/messages"
|
||||
)
|
||||
|
||||
type Gin struct {
|
||||
C *gin.Context
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
// Response setting gin.JSON
|
||||
func (g *Gin) Response(httpCode, errCode int, data interface{}) {
|
||||
g.C.JSON(httpCode, Response{
|
||||
Code: errCode,
|
||||
Msg: messages.GetMsg(errCode),
|
||||
Data: data,
|
||||
})
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user